场景
Java工具库Guava的不可变集合和新集合类型Multiset、Multimap、BiMap、RangeSet、RangeMap等的使用示例:
Java工具库Guava的不可变集合和新集合类型Multiset、Multimap、BiMap、RangeSet、RangeMap等的使用示例_霸道流氓气质的博客-CSDN博客
Guava提供了一些集合的静态方法。比如下面这样可以很方便的在初始化时指定初始元素。
List<String> list = Lists.newArrayList("badao","de","chengxvyuan");
注:
博客:
霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
Iterables
Iterable,在可能的情况下,Guava提供的工具方法更偏向于接受Iterable而不是Collection类型
1、concat(Iterable<Iterable>) 串联多个iterables的懒视图
Iterable<Integer> concat = Iterables.concat(Ints.asList(1, 2, 3), Ints.asList(4, 5, 6));
System.out.println(concat);//[1, 2, 3, 4, 5, 6]
2、frequency(Iterable,Object)返回对象在iterable中出现的次数
int frequency = Iterables.frequency(Ints.asList(1, 1, 2, 3, 4, 5), 1);
System.out.println(frequency);//2
3、partition(Iterable,int) 把iterable按指定大小分割,得到的子集都不能进行修改操作
Iterable<List<Integer>> partition = Iterables.partition(Ints.asList(1, 2, 3, 3, 4, 5, 6), 2);
System.out.println(partition);//[[1, 2], [3, 3], [4, 5], [6]]
4、getFirst(Iterable,T default)返回iterable的第一个元素,若iterable为空则返回默认值default
Integer first = Iterables.getFirst(Ints.asList(1, 2, 3), 0);
System.out.println(first);//1
Integer first1 = Iterables.getFirst(Ints.asList(), 0);
System.out.println(first1);//0
5、getLast(Iterable)返回iterable的最后一个元素,若iterable为空则抛出NoSuchElementException
Integer last = Iterables.getLast(Ints.asList(1, 2, 3));
System.out.println(last);//3
//Object last1 = Iterables.getLast(Ints.asList());//java.util.NoSuchElementException
//System.out.println(last1);
6、elementsEqual(Iterable,Iterable)如果两个iterable中的所有元素相等且顺序一致,返回true
boolean b = Iterables.elementsEqual(Ints.asList(1, 2, 3), Ints.asList(1, 2, 3));
System.out.println(b);//true
boolean b1 = Iterables.elementsEqual(Ints.asList(1, 3, 2), Ints.asList(1, 2, 3));
System.out.println(b1);//false
7、unmodifiableIterable(Iterable)返回iterable的不可变视图
Iterable<Integer> integers = Iterables.unmodifiableIterable(Ints.asList(1, 2, 3));
System.out.println(integers);//[1, 2, 3]
8、limit(Iterable.int)限制iterable的元素个数限制给定值
Iterable<Integer> limit = Iterables.limit(Ints.asList(1, 2, 3, 4), 2);
System.out.println(limit);//[1, 2]
9、getOnlyElement(Iterable)获取iterable中唯一的元素,如果iterable为空或者有多个元素,则快速失败
//getOnlyElement(Iterable)获取iterable中唯一的元素,如果iterable为空或者有多个元素,则快速失败
//Integer onlyElement = Iterables.getOnlyElement(Ints.asList(1, 2, 3));//java.lang.IllegalArgumentException: expected one element but was: <1, 2, 3>
//System.out.println(onlyElement);
Integer onlyElement1 = Iterables.getOnlyElement(Ints.asList(1));
System.out.println(onlyElement1);//1
// Object onlyElement2 = Iterables.getOnlyElement(Ints.asList());//java.util.NoSuchElementException
// System.out.println(onlyElement2);
Lists
1、reverse(List)返回给定List的反转视图
List<Integer> integers = Ints.asList(1, 2, 3, 4, 5);
List<Integer> reverse = Lists.reverse(integers);
System.out.println(reverse);//[5, 4, 3, 2, 1]
2、partition(List,int) 把List按指定大小分割
List<List<Integer>> partition = Lists.partition(reverse, 2);
System.out.println(partition);//[[5, 4], [3, 2], [1]]
Sets
1、union(Set,Set)求并集
Sets.SetView<Integer> union = Sets.union(Sets.newHashSet(1, 2, 3), Sets.newHashSet(3, 4));
System.out.println(union);//[1, 2, 3, 4]
2、intersection(Set,Set)求交集
Sets.SetView<Integer> intersection = Sets.intersection(Sets.newHashSet(1, 2, 3), Sets.newHashSet(2, 3));
System.out.println(intersection);//[2, 3]
3、difference(Set,Set)属于A但不属于B的差集
Sets.SetView<Integer> difference = Sets.difference(Sets.newHashSet(1, 2, 3), Sets.newHashSet(1, 4, 5));
System.out.println(difference);//[2, 3]
4、symmetricDifference(Set,Set)对称差集
Sets.SetView<Integer> integers = Sets.symmetricDifference(Sets.newHashSet(1, 2, 3, 7, 8), Sets.newHashSet(1, 2, 3, 5, 6));
System.out.println(integers);//[8, 7, 5, 6]
5、cartesianProduct(List<Set>)返回所有集合的笛卡尔积
HashSet<Integer> integers1 = Sets.newHashSet(1, 2, 3);
HashSet<Integer> integers2 = Sets.newHashSet(4, 5);
Set<List<Integer>> lists = Sets.cartesianProduct(integers1, integers2);
System.out.println(lists);//[[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]
6、powerSet(Set)返回给定集合的所有子集
Set<Set<Integer>> sets = Sets.powerSet(Sets.newHashSet(1, 2));
sets.forEach(System.out::println);
// []
// [1]
// [2]
// [1, 2]
Maps
1、Maps.uniqueIndex(Iterable,Function)
针对的场景是,有一组对象,它们在某个属性上分别有独一无二的值,
而我们希望能够按照这个属性值查找对象,比如,有一堆字符串,这些字符串的长度都是独一无二的,希望按照特定长度查找字符串,
注意这里长度必须是独一无二,如果有两个字符串长度一样则不行
ArrayList<String> strings = Lists.newArrayList("a","bd","ddd");
ImmutableMap<Integer, String> integerStringImmutableMap = Maps.uniqueIndex(strings, new Function<String, Integer>() {
@Override
public @Nullable Integer apply(@Nullable String s) {
return s.length();
}
});
System.out.println(integerStringImmutableMap);//{1=a, 2=bd, 3=ddd}
2、diffenence(Map,Map)用来比较两个Map以获取所有不同点
ImmutableMap<Integer, String> of = ImmutableMap.of(1, "a", 2, "b",3,"d");
ImmutableMap<Integer, String> of1 = ImmutableMap.of(1, "c", 2, "b",5,"g");
MapDifference<Integer, String> difference = Maps.difference(of, of1);
System.out.println(difference);//not equal: only on left={3=d}: only on right={5=g}: value differences={1=(a, c)}
//entriesInCommon()两个Map中都有的映射项,包括匹配的键与值
System.out.println(difference.entriesInCommon());//{2=b}
//entriesDiffering()键相同但是值不同值映射项
System.out.println(difference.entriesDiffering());//{1=(a, c)}
//entriesOnlyOnLeft()键只存在于左边Map的映射项
System.out.println(difference.entriesOnlyOnLeft());//{3=d}
//entriesOnlyOnRight()键只存在于右边Map的映射项
System.out.println(difference.entriesOnlyOnRight());//{5=g}
Multisets
1、containsOccurrences(Multiset sup,Multiset sub) 如果sub.count(o)<=sup.conunt(o)返回true
HashMultiset<Object> multiset = HashMultiset.create();
multiset.add("a",2);
HashMultiset<Object> multiset1 = HashMultiset.create();
multiset1.add("a",5);
//multiset包含2个a,multiset1包含5个a,返回true
System.out.println(multiset1.containsAll(multiset));//true
//containsOccurrences(Multiset sup,Multiset sub) 如果sub.count(o)<=sup.conunt(o)返回true
boolean b = Multisets.containsOccurrences(multiset, multiset1);
System.out.println(b);//false
Multisets.removeOccurrences(multiset1,multiset);
System.out.println(multiset1);//[a x 3]
2、retainOccurrences(Multiset removeFrom,Multiset toRetain)修改removeFrom,以保证任意o都符合removeFrom.count(o)<=toRetain.count(o)
System.out.println(multiset);//[a x 2]
System.out.println(multiset1);//[a x 3]
Multisets.retainOccurrences(multiset1,multiset);
System.out.println(multiset);//[a x 2]
System.out.println(multiset1);//[a x 2]
3、intersection(Multiset,Multiset)返回两个multiset的交集
Multiset<Object> intersection = Multisets.intersection(multiset1, multiset);
System.out.println(intersection);//[a x 2]
Multimaps
1、index 通常针对的场景是:有一组对象,它们有共同的特定的属性,我们希望能按照这个属性的值查询对象
ImmutableSet<String> of = ImmutableSet.of("a", "bb", "cc", "ddd");
ImmutableListMultimap<Integer, String> index = Multimaps.index(of, new Function<String, Integer>() {
@Override
public @Nullable Integer apply(@Nullable String s) {
return s.length();
}
});
System.out.println(index);//{1=[a], 2=[bb, cc], 3=[ddd]}
2、invertFrom,Multimap可以把多个键映射到同一个值,也可以把一个键映射到多个值,反转Multimap时可以供invertFrom,并且可以自由选择反转后的Multimap的实现
ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
multimap.putAll("a",Ints.asList(2,4,6));
multimap.putAll("b",Ints.asList(1,2,6));
multimap.putAll("c",Ints.asList(3,4,6));
TreeMultimap<Integer, String> invertFrom = Multimaps.invertFrom(multimap, TreeMultimap.create());
System.out.println(invertFrom);//{1=[b], 2=[a, b], 3=[c], 4=[a, c], 6=[a, b, c]}
3、想在Map对象上使用Multimap的方法吗,forMap(Map)把Map包装成SetMultimap
例如,与Multimaps.invertFrom结合使用,可以把多对一的Map反转成一对多的Multimap
Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 1, "c", 2);
SetMultimap<String, Integer> multimap1 = Multimaps.forMap(map);
System.out.println(multimap1);//{a=[1], b=[1], c=[2]}
HashMultimap<Integer, String> invertFrom1 = Multimaps.invertFrom(multimap1, HashMultimap.create());
System.out.println(invertFrom1);//{1=[a, b], 2=[c]}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135863.html