Java 如何将一组字符与地图进行比较哪些键是字符?

Java 如何将一组字符与地图进行比较哪些键是字符?,java,Java,如果我有一组名为characters的字符,并且包含以下字符(不必是SortedSet) 我有一个地图,它的键是字符集,值是字符串 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'

如果我有一组名为characters的字符,并且包含以下字符(不必是SortedSet)

我有一个地图,它的键是字符集,值是字符串

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");
map,aMap=newhashmap,();
aMap.put('a'h't','hat');
aMap.put('o'g'd','dog');
aMap.put('c'r'a't'e','react');
我将使用什么javdoc方法来比较字符,因为它们都在一个集合中,然后使用for循环遍历键集来比较字符,以仅查找由第一个集合中包含的字符生成的键。因此,在上面的示例中,第二个条目('o'g'd','dog')将被省略

谢谢


安迪

只需玩一下set.containsAll(…)。

示例:如果集合大小相等,且firstSet.containsAll(secondSet)为true,则有两个集合是相同的。

若要获得与集合调用map.keySet()类似的内容,请

公共类SetTest{
公共静态void main(字符串[]args){
Set=newhashset();
HashMap=newHashMap();
for(字符c:“Character”.toCharArray()){
增加(c);
(c,“某些值”);
}
System.out.println(set+“=”+map.keySet()+set.containsAll(map.keySet());
设置。删除('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]正确
[e,t,c,r,a,h]==[e,t,c,r,a,c,h]错误

请使用代码格式化工具格式化您的问题,并更好地解释问题。你想做什么?你能写下一些预期结果的例子吗?你能更详细地解释一下,为什么要省略第二项?因为它不包含来自“characters”的字母,或者因为它的字母都不包含在“characters”中?比较时,第二个条目与第一个集合没有任何相同的字符(即字符没有“d”、“o”或“g”)。大小无关紧要:如果此集合包含指定集合的所有元素,则返回true。如果指定的集合也是一个集合,则如果该集合是该集合的子集,则该方法返回true。看,我说过两套是一样的。如果其中一个是另一个的子集,并且它们的大小不同,那么它们就不完全相同。然而,也许我误解了作者的任务。
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");
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