Joh*_*590 0 java collections scala hashmap
我正在尝试将我用Java编写的算法转换为Scala,但是我遇到了containsValue()Java中存在的方法的问题.我想做类似的事情,if (hashMap.containsValue(value))但我查看了scala文档,并且只找到了一个contains(key)方法.你如何在Scala中实现或使用hashmap.containsValue(value)我仍然是Scala的新手,但这是我迄今为止在Scala中所拥有的:
def retString(s: String)
{
val map = new mutable.HashMap[Int, Char]
for (c <- s.toCharArray)
{
//if(!map.containsValue(c)) goes here
}
}
Run Code Online (Sandbox Code Playgroud)
`我试图转换的完整算法是我用Java编写的removeDuplicates:
public static String removeDuplicates(char[] s)
{
HashMap<Integer, Character> hashMap = new HashMap<Integer, Character>();
int current = 0;
int last = 0;
for(; current < s.length; current++)
{
if (!(hashMap.containsValue(s[current])))
{
s[last++] = s[current];
hashMap.put(current, s[current]);
}
}
s[last] = '\0';
//iterate over the keys and find the values
String result = "";
for (Integer key: hashMap.keySet()) {
result += hashMap.get(key);
}
return result;
}
Run Code Online (Sandbox Code Playgroud)