guava入门学习3(集合工具)

3.1 Iterables

3.1.1concat() 拼接集合
List<String> l1 = Lists. newArrayList("1");
List<String> l2 = Lists. newArrayList("2");
Iterable<String> concat = Iterables. concat(l1, l2);
concat.forEach(System. out::println);

3.1.2元素在指定集合中出现的次数
int frequency = Iterables. frequency(l1, "1”);

3.1.3Partition() 按指定元素个数拆分集合
List<String> l1 = Lists. newArrayList("1","2","3","4","5");
Iterable<List<String>> partition = Iterables. partition(l1, 2);
partition.forEach(System. out::println);

3.1.4比较两个集合,当两个集合中的元素值都相等且顺序一样时,返回true
List<String> l1 = Lists. newArrayList("1","2","3","4","51");
List<String> l2 = Lists. newArrayList("1","2","3","4",new String("5")+"1");
boolean b = Iterables. elementsEqual(l1, l2);

3.1.5截取集合的前指定位数作为视图遍历
List<String> l1 = Lists. newArrayList("1","2","3","4",”5”,”6”,”7");
Iterable<String> limit = Iterables. limit(l1, 6);


3.2Sets

3.2.1powerSet() 返回所有的子集,包含空集和自身全集
Set<String> set1 = Sets.newHashSet("1","2");
Set<Set<String>> sets = Sets.powerSet(set1);
sets.forEach(System.out::println);

3.2.2Sets.cartesianProduct(Set ...) 返回集合的笛卡尔积 集合个数不限
Set<String> set1 = Sets.newHashSet("1","2");
Set<String> set2 = Sets.newHashSet("a","b");
Set<List<String>> lists = Sets.cartesianProduct(Lists.newArrayList(set1, set2));
lists.forEach(System.out::println);


3.3Maps

3.3.1difference.entriesInCommon 返回两个map中健值对都相等
Map<String,String> m1 = Maps.newHashMapWithExpectedSize(16);
Map<String,String> m2 = Maps.newHashMapWithExpectedSize(16);
m1.put("a","a1");
m2.put("a","a1");
m1.put("b","b1");
m2.put("b","b2");
m1.put("c","c1");
MapDifference<String, String> difference = Maps.difference(m1, m2);
Map<String, String> stringStringMap = difference.entriesInCommon();
System.out.println(stringStringMap);

3.3.2difference.entriesDiffering()  返回不相等的键值对
Map<String, MapDifference.ValueDifference<String>> stringValueDifferenceMap = difference.entriesDiffering();
System.out.println(stringValueDifferenceMap);

3.3.3只存在于左边的键值对
Map<String, String> onlyOnLeftMap = difference.entriesOnlyOnLeft();
System.out.println(onlyOnLeftMap);

3.3.4只存在于右边的键值对
Map<String, String> onlyOnRightMap = difference.entriesOnlyOnRight();
System. out.println(onlyOnRightMap);


3.4Tables

3.4.1转置二维表
Table<String ,String,String> table = HashBasedTable.create();
table.put( "r1","c1","r1c1");
table.put( "r1","c2","r1c2");
table.put( "r2","c1","r2c1");
System. out.println(table);
Table<String , String, String> transpose = Tables.transpose(table);
System. out.println(transpose);
—输出:
{r1={c1=r1c1, c2=r1c2}, r2={c1=r2c1}}
{c1={r1=r1c1, r2=r2c1}, c2={r1=r1c2}}
 
 

转载于:https://www.cnblogs.com/input4hua/p/7859559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值