Java , . , Multi-Map (, ).
The most popular versions can be found in two open source versions, Google Guava and Apache Commons / Collections .
Guava example:
final Multimap<Integer, String> mmap =
Multimaps.newSetMultimap(
Maps.<Integer, Collection<String>> newHashMap(),
new Supplier<Set<String>>(){
@Override
public Set<String> get(){
return Sets.newHashSet();
}
});
mmap.put(1, "foo");
mmap.put(1, "bar");
System.out.println(mmap.get(1));
Conclusion:
[foo, bar]
Example Commons Collections:
final MultiMap mmap = new MultiHashMap();
mmap.put(1, "foo");
mmap.put(1, "bar");
System.out.println(mmap.get(1));
Conclusion:
[foo, bar]
As you can see, the version of Commons Collections is much simpler, but it is also less efficient, and in the current version it does not support Java 1.5 generators. Therefore, I will go with Guava.
source
share