如果我有一组称为字符的字符并包含以下字符(不必是 SortedSet)
'c''h''a''r''a''c''t''e''r'
我有一个 map ,例如,它以字符集作为键,以字符串作为值
map<Set<Character>>,<String> aMap = new HashMap<Set<Character>>,<String>();
aMap.put('a''h''t', "hat");
aMap.put('o''g''d', "dog");
aMap.put('c''r''a''t''e', "react");
我将使用什么 javdoc 方法来比较字符,因为它们都在一个 Set 中,然后使用 for 循环遍历 keySet 来比较字符以仅查找由包含在第一的。因此,在上面的示例中,第二个条目 ('o''g''d', "dog") 将被省略。
谢谢
安迪
最佳答案
要获得与您设置的调用 map.keySet() 相当的东西
public class SetTest {
public static void main(String[] args) {
Set<Character> set = new HashSet<Character>();
HashMap<Character, String> map = new HashMap<Character, String>();
for (char c : "Character".toCharArray()) {
set.add(c);
map.put(c, "some value");
}
System.out.println( set + " == " + map.keySet() + set.containsAll( map.keySet() ));
set.remove('C');
System.out.println( set + " == " + map.keySet() + set.containsAll( map.keySet() ));
}
}
[e, t, c, r, a, C, h] == [e, t, c, r, a, C, h]true
[e, t, c, r, a, h] == [e, t, c, r, a, C, h]false
关于java - 如何将一组字符与哪些键是字符的映射进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3288926/