本文介绍了Guava集合类Maps、BiMap、ArrayListMultimap相关的使用, 比如将具有唯一主键的对象快速存入Map、 键值对反转等...
一、 对Maps集合类使用案例:
package com.wenniuwuren.guava; import java.util.List; import java.util.Map; import java.util.NavigableMap; import java.util.Set; import com.google.common.base.Function; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; /** * * @ClassName: MapsTest * @Description: Maps集合类扩展使用 * @author wenniuwuren * @date 2015-6-1 下午5:23:22 * */ public class MapsTest { @SuppressWarnings("unchecked") public static void main(String[] args) { Person p1 = new Person("001", "zhang_san"); Person p2 = new Person("002", "li_si"); List<Person> personList = Lists.newArrayList(); personList.add(p1); personList.add(p2); // 将主键当作Map的Key Map<String, Person> personMap = Maps.uniqueIndex(personList.iterator(), new Function<Person, String>() { @Override public String apply(Person input) { return input.getId(); } }); System.out.println("将主键当作Map的Key:" + personMap); // 可以说是Maps.uniqueIndex相反的作用 Set<Person> personSet = Sets.newHashSet(p1, p2); @SuppressWarnings("unused") Map<Person, String> personAsMap= Maps.asMap(personSet, new Function() { @Override public Object apply(Object input) { return ((Person)input).getId(); } }); System.out.println(personAsMap); // 转换Map中的value值 Map<String, String> transformValuesMap = Maps.transformValues(personMap, new Function<Person, String>() { @Override public String apply(Person input) { return input.getName(); } }); System.out.println("转换Map中的value值" + transformValuesMap); } } class Person { private String Id; private String name; public Person(String Id, String name) { this.Id = Id; this.name = name; } public String getId() { return Id; } public void setId(String id) { Id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
二、 对ArrayListMultiMap的使用案例:
package com.wenniuwuren.guava; import java.util.List; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; /** * * @ClassName: ArrayListMultiMapTest * @Description: 一key对应多value * @author wenniuwuren * @date 2015-6-1 下午6:17:00 * */ public class ArrayListMultiMapTest { public static void main(String[] args) { /** * ArrayListMultimap List允许重复, 所以大小为4 */ ArrayListMultimap<String,String> multiMap = ArrayListMultimap.create(); multiMap.put("Foo","1"); multiMap.put("Foo","2"); multiMap.put("Foo","3"); multiMap.put("Foo","3"); System.out.println("内容:" + multiMap + " , 大小:" + multiMap.size()); /** * HashMultimap Hash不允许重复值, 大小为3 */ HashMultimap<String,String> hashMultiMap = HashMultimap.create(); hashMultiMap.put("Bar","1"); hashMultiMap.put("Bar","2"); hashMultiMap.put("Bar","3"); hashMultiMap.put("Bar","3"); hashMultiMap.put("Bar","3"); System.out.println("内容:" + hashMultiMap + " , 大小:" + hashMultiMap.size()); } }
三、 对BiMaps使用案例:
package com.wenniuwuren.collections; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; /** * 对BiMap工具类的使用: key和value都是唯一的 * @author wenniuwuren * */ public class BiMapTest { public static void main(String[] args) { BiMap<String,String> biMap = HashBiMap.create(); biMap.put("1","Tom"); biMap.put("2","Jerry"); // 放入重复值value 将会报错 // biMap.put("2","Tom"); // 相同Value覆盖前一个 //biMap.forcePut("2","Tom"); System.out.println(biMap); // key value反转 System.out.println(biMap.inverse()); } }
参考资料:
《Getting Started with Google Guava》