陕西中交基础建设有限公司网站,网站站点地图,个人网站设计作业,wordpress调用二级分类以下是使用Google Guava库中的工具方法来创建和操作List、Set、Map集合的一些示例#xff1a;
List相关操作 创建List 使用Lists.newArrayList()创建一个新的可变ArrayList实例。ListInteger list Lists.newArrayList(1, 2, 3);// 创建不可修改的列表ListString…以下是使用Google Guava库中的工具方法来创建和操作List、Set、Map集合的一些示例
List相关操作 创建List 使用Lists.newArrayList()创建一个新的可变ArrayList实例。ListInteger list Lists.newArrayList(1, 2, 3);// 创建不可修改的列表ListString unmodifiableList Lists.newArrayList(a, b, c);ListString unmodifiableList2 ImmutableList.copyOf(Lists.newArrayList(d, e, f));System.out.println(unmodifiableList);/*Exception in thread main java.lang.UnsupportedOperationExceptioncom.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:269)cn.ucmed.adaptation.guava.preconditions.Example003.main(Example003.java:20)*/// unmodifiableList2.add(g);System.out.println(unmodifiableList2); 使用Lists.newLinkedList()创建一个新的LinkedList实例。ListInteger list Lists.newLinkedList(); 反转List 使用Lists.reverse()反转List中的元素顺序。ListInteger list Lists.newArrayList(1, 2, 3);
list Lists.reverse(list); // 结果为[3, 2, 1]分区List 使用Lists.partition()将List分割成多个子List。ListInteger list Lists.newArrayList(1, 2, 3, 4, 5);
ListListInteger partitions Lists.partition(list, 2); // 结果为[[1, 2], [3, 4], [5]]转换List元素 使用Lists.transform()对List中的每个元素进行转换。ListString list Lists.newArrayList(apple, banana, orange);
ListInteger lengths Lists.transform(list, String::length); // 结果为[5, 6, 6]Set相关操作 创建Set 使用Sets.newHashSet()创建一个新的可变HashSet实例。
SetString set Sets.newHashSet(Apple, Banana); 集合操作 使用Sets.union()、Sets.intersection()、Sets.difference()和Sets.symmetricDifference()进行集合的并集、交集、差集和对称差集操作。
SetString setA Sets.newHashSet(Apple, Banana, Cherry);
SetString setB Sets.newHashSet(Banana, Date, Fig);
SetString union Sets.union(setA, setB); // 并集
SetString intersection Sets.intersection(setA, setB); // 交集
SetString difference Sets.difference(setA, setB); // 差集
SetString symmetricDifference Sets.symmetricDifference(setA, setB); // 对称差集
Map相关操作 创建Map 使用Maps.newHashMap()创建一个新的可变HashMap实例。MapString, Integer map Maps.newHashMap(); 过滤Map键 使用Maps.filterKeys()根据给定的谓词过滤Map的键。
MapString, Integer scores Maps.newHashMap();
scores.put(Alice, 85);
scores.put(Bob, 90);
MapString, Integer highScores Maps.filterKeys(scores, name - name.startsWith(A) || name.startsWith(B)); 转换Map值 使用Maps.transformValues()对Map中的值进行转换。MapString, Integer scores Maps.newHashMap();
scores.put(Alice, 85);
scores.put(Bob, 90);
MapString, String scoreStrings Maps.transformValues(scores, score - score points);4. 计算Map的交集
Map的交集是指两个Map中都有的键值对。Guava提供了Maps.difference()方法来计算两个Map的差异通过该方法我们可以获取交集。
HashMapString, Integer mapA Maps.newHashMap();
mapA.put(a, 1);
mapA.put(b, 2);
mapA.put(c, 3);HashMapString, Integer mapB Maps.newHashMap();
mapB.put(b, 20);
mapB.put(c, 3);
mapB.put(d, 4);MapDifferenceString, Integer mapDifference Maps.difference(mapA, mapB);
MapString, Integer entriesInCommon mapDifference.entriesInCommon();
System.out.println(entriesInCommon); // 输出两个Map的交集5. 计算Map的差集
Map的差集是指在一个Map中存在而在另一个Map中不存在的键值对。同样使用Maps.difference()方法我们可以获取差集。
MapString, Integer entriesOnlyOnLeft mapDifference.entriesOnlyOnLeft(); // 只存在于mapA中的entry
MapString, Integer entriesOnlyOnRight mapDifference.entriesOnlyOnRight(); // 只存在于mapB中的entry
System.out.println(entriesOnlyOnLeft); // 输出只存在于mapA中的entry
System.out.println(entriesOnlyOnRight); // 输出只存在于mapB中的entryMapString, Integer mapA new HashMap();mapA.put(a, 1);mapA.put(b, 2);mapA.put(c, 3);MapString, Integer mapB new HashMap();mapB.put(b, 20);mapB.put(c, 3);mapB.put(d, 4);MapDifferenceString, Integer difference Maps.difference(mapA, mapB);MapString, Integer symmetricDifference new HashMap();symmetricDifference.putAll(difference.entriesOnlyOnLeft());symmetricDifference.putAll(difference.entriesOnlyOnRight());System.out.println(对称差集: symmetricDifference);6. 计算Map的并集
Map的并集是指两个Map中所有的键值对包括重复的键重复的键将以第二个Map中的值为准。
MapString, Integer combaMap new HashMap(mapA); // 将mapA作为基础
combaMap.putAll(mapB); // 将mapB中的所有entry添加到合并Map中
System.out.println(combaMap); // 输出两个Map的并集这些示例展示了Guava库在集合操作方面的一些基本用法可以帮助简化代码并提高效率。更多详细信息和高级用法可以参考Guava官方文档和相关技术博客。