转自:
下文笔者讲述guava中Multimap集合的简介说明,如下所示
我们都知道jdk自带的map结构只允许一个key对应一个map
当key重复时,map则会替换以前的value
guava之Multimap集合简介
Multimap集合: 一个key可以对应多个value 类似于redis的list结构 如: ArrayListMultimap, ForwardingListMultimap, ForwardingMultimap, ForwardingSetMultimap, ForwardingSortedSetMultimap, HashMultimap, ImmutableListMultimap, ImmutableMultimap, ImmutableSetMultimap, LinkedHashMultimap, LinkedListMultimap, TreeMultimap
例:
public static void main(String[] args) { /** * java265.com 示例程序 */ Multimap<String,String> multiMap= TreeMultimap.create(); multiMap.put("k1","java265.com-1"); multiMap.put("k1","java265.com-2"); multiMap.put("k2","java265.com-3"); multiMap.put("k3","java265.com-4"); multiMap.put("k0","java265.com-5"); for (String s : multiMap.keySet()) { Collection<String> lastNames = multiMap.get(s); System.out.println(lastNames); } }