最近经常使用到guava工具的东西,每次都要百度查询使用方法,现统一记录一下方便自己使用。
参考文章: Google guava工具类的介绍和使用 、 Guava工具类学习
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.0-jre</version> </dependency>
<<最新依赖版本号点击这里获取>>
List<String> list = Lists.newArrayList(); Set<String> set = Sets.newHashSet(); Map<String, String> map = Maps.newHashMap();
ImmutableList<String> iList = ImmutableList.of("a", "b", "c"); ImmutableSet<String> iSet = ImmutableSet.of("e1", "e2"); ImmutableMap<String, String> iMap = ImmutableMap.of("k1", "v1", "k2", "v2");
它的业务场景:当你需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比较复杂的集合类型的数据结构,来做相应的业务逻辑处理。那Multimap在合适不过。
传统做法:
//Item就是封装的对象 Map<String,List<Item>> map = new HashMap<>(); for (Item item : list){ List<Item> tmp = map.get(item.getName()); if (null == tmp){ tmp = new ArrayList<>(); map.put(item.getName(),tmp); } tmp.add(item); }
现在:
Multimap<String,Item> multiMap = ArrayListMultimap.create(); for (Item item : list){ multiMap.put(item.getName(),item); }
类似的还有:
MultiSet: 无序+可重复 count()方法获取单词的次数 增强了可读性+操作简单 创建方式: Multiset<String> set = HashMultiset.create(); Multimap: key-value key可以重复 创建方式: Multimap<String, String> teachers = ArrayListMultimap.create(); BiMap: 双向Map(Bidirectional Map) 键与值都不能重复 创建方式: BiMap<String, String> biMap = HashBiMap.create(); Table: 双键的Map Map--> Table-->rowKey+columnKey+value //和sql中的联合主键有点像 创建方式: Table<String, String, Integer> tables = HashBasedTable.create();
List<String> list = new ArrayList<String>(); list.add("aa"); list.add("bb"); list.add("cc"); String result = Joiner.on("-").join(list); //result为 aa-bb-cc
Map<String, Integer> map = Maps.newHashMap(); map.put("xiaoming", 12); map.put("xiaohong",13); String result = Joiner.on(",").withKeyValueSeparator("=").join(map); // result为 xiaoming=12,xiaohong=13
//use guava String str = "1-2-3-4-5-6"; List<String> list = Splitter.on("-").splitToList(str); //list为 [1, 2, 3, 4, 5, 6] String str = "1-2-3-4- 5- 6 "; List<String> list = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(str); System.out.println(list); //[1, 2, 3, 4, 5, 6] 可以忽略中间的空格
String str = "xiaoming=11,xiaohong=23"; Map<String,String> map = Splitter.on(",").withKeyValueSeparator("=").split(str);
HashSet setA = newHashSet(1, 2, 3, 4, 5); HashSet setB = newHashSet(4, 5, 6, 7, 8); SetView union = Sets.union(setA, setB); System.out.println("union:"); for (Integer integer : union) System.out.println(integer); //union:12345867 SetView difference = Sets.difference(setA, setB); System.out.println("difference:"); for (Integer integer : difference) System.out.println(integer); //difference:123 SetView intersection = Sets.intersection(setA, setB); System.out.println("intersection:"); for (Integer integer : intersection) System.out.println(integer); //intersection:45
MapDifference differenceMap = Maps.difference(mapA, mapB); differenceMap.areEqual(); Map entriesDiffering = differenceMap.entriesDiffering(); Map entriesOnlyOnLeft = differenceMap.entriesOnlyOnLeft(); Map entriesOnlyOnRight = differenceMap.entriesOnlyOnRight(); Map entriesInCommon = differenceMap.entriesInCommon();
Person person = new Person("aa",11); String str = MoreObjects.toStringHelper("Person").add("age", person.getAge()).toString(); System.out.println(str); //输出Person{age=11}
StringBuilder stringBuilder = new StringBuilder("hello"); // 字符串连接器,以|为分隔符,同时去掉null元素 Joiner joiner1 = Joiner.on("|").skipNulls(); // 构成一个字符串foo|bar|baz并添加到stringBuilder stringBuilder = joiner1.appendTo(stringBuilder, "foo", "bar", null, "baz"); System.out.println(stringBuilder); // hellofoo|bar|baz