2010-07-20 12 views
2

如果我有一組被稱爲字符和包含下列字符的字符(不必是SortedSet的)如何比較一組字符與地圖哪些鍵是字符?

'c''h''a''r''a''c''t''e''r' 

和我有一個地圖,其具有套charcters作爲其鍵和字符串作爲的例如值

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方法來比較的人物,因爲他們都在一個組,然後用一個for循環比較字符只查找由密鑰通過密鑰集迭代來自第一個包含的字符。所以在上面的例子中,第二項('o''g''d',「狗」)將被省略。

感謝

安迪

+0

使用代碼格式化工具,請格式化你的問題,解釋問題的更好。您想做什麼?你能用預期的結果寫下一些例子嗎? – pakore 2010-07-20 10:16:11

+0

你能否詳細解釋一下,爲什麼第二項應該省略?因爲它包含來自「字符」的** no **字母,或者因爲它們中沒有字母被包含在「字符」中? – Groo 2010-07-20 10:23:42

+0

當比較的第二項沒有任何與第一組相同的字符(即字符沒有'd','o'或'g') – user386537 2010-07-20 11:21:15

回答

0

只是set.containsAll(...)玩。
示例:如果sets的大小相同並且firstSet.containsAll(secondSet)爲true,則2組相同。

+0

大小無關緊要:如果此集合包含所有的指定集合的​​元素。如果指定的集合也是一個集合,則此方法返回true,如果它是此集合的子集。請參閱http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/util/Set.html#containsAll%28java.util.Collection%29 – Redlab 2010-07-20 10:25:50

+0

我已經講了2套相同。如果其中一個是另一個的子集,並且它們具有不同的大小 - 並不相同。不過,也許我誤解了作者的任務。 – foret 2010-07-20 10:29:15

1

要得到的東西比得上你的集合調用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 
相關問題