-3

私は次の地図を持っています..

Map<String, Collection<Integer>> multiValueMap = new HashMap<String, Collection<Integer>>();
         multiValueMap.put("a", new ArrayList<Integer>());
         multiValueMap.get("a").add(new Integer(10));
         multiValueMap.get("a").add(new Integer(20));
         multiValueMap.get("a").add(new Integer(30));

         System.out.println("There are "+multiValueMap.size()+" elements in the map.");
         System.out.println("Content of multiValueMap are...");
         Set s=multiValueMap.entrySet();
         Iterator itr=s.iterator();
         while(itr.hasNext())
         {
             Map.Entry m=(Map.Entry)itr.next();
             System.out.println(m.getKey()+"\t"+m.getValue()+"\t"+ m.hashCode());
          }

そして、これの出力は..

There are 1 elements in the map.
Content of multiValueMap are...
a   [10, 20, 30]    39954

Guava Multimap でも同じことを達成できることをアドバイスしてください。

4

2 に答える 2

1
 Multimap<String, Integer> multimap = ArrayListMultimap.create();
 multimap.putAll("a", ImmutableList.of(10, 20, 30));
 System.out.println("There are " + multimap.keySet().size() +
     " elements in the map.");
 System.out.println("Content of multimap are...");
 for (Map.Entry<String, Collection<Integer>> asMapEntry :
     multimap.asMap().entrySet()) {
   System.out.printf("%s\t%s\t%s%n", asMapEntry.getKey(), asMapEntry.getValue(),
       asMapEntry.hashCode());
 }
于 2012-12-05T17:43:52.843 に答える
0

使用できるキーの数を取得するにはMultiMapMultimap.keySet().size()

このセットを繰り返し使用Multimap.get(key) して、このキーに関連付けられたすべての値のコレクションを取得することもできます。

于 2012-12-05T14:18:11.833 に答える