当前位置: 首页 > news >正文

网站跳转微信链接网站申请

网站跳转微信链接,网站申请,上海松江网站设计公司,b2c电商网站对比入门示例 guava 最佳实践 学习指南 以下是使用Google Guava库中的工具方法来创建和操作List、Set、Map集合的一些示例&#xff1a; List相关操作 创建List 使用Lists.newArrayList()创建一个新的可变ArrayList实例。List<Integer> list Lists.newArrayList(1, 2, 3);/…

入门示例
guava 最佳实践 学习指南

以下是使用Google Guava库中的工具方法来创建和操作List、Set、Map集合的一些示例:

List相关操作

  1. 创建List

    • 使用Lists.newArrayList()创建一个新的可变ArrayList实例。
      List<Integer> list = Lists.newArrayList(1, 2, 3);// 创建不可修改的列表List<String> unmodifiableList = Lists.newArrayList("a", "b", "c");List<String> 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实例。
      List<Integer> list = Lists.newLinkedList();
  2. 反转List

    • 使用Lists.reverse()反转List中的元素顺序。
      List<Integer> list = Lists.newArrayList(1, 2, 3);
      list = Lists.reverse(list); // 结果为[3, 2, 1]
      
  3. 分区List

    • 使用Lists.partition()将List分割成多个子List。
      List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5);
      List<List<Integer>> partitions = Lists.partition(list, 2); // 结果为[[1, 2], [3, 4], [5]]
      
  4. 转换List元素

    • 使用Lists.transform()对List中的每个元素进行转换。
      List<String> list = Lists.newArrayList("apple", "banana", "orange");
      List<Integer> lengths = Lists.transform(list, String::length); // 结果为[5, 6, 6]
      

Set相关操作

  1. 创建Set

    • 使用Sets.newHashSet()创建一个新的可变HashSet实例。
      
      Set<String> set = Sets.newHashSet("Apple", "Banana");
  2. 集合操作

    • 使用Sets.union()Sets.intersection()Sets.difference()Sets.symmetricDifference()进行集合的并集、交集、差集和对称差集操作。
      
      Set<String> setA = Sets.newHashSet("Apple", "Banana", "Cherry");
      Set<String> setB = Sets.newHashSet("Banana", "Date", "Fig");
      Set<String> union = Sets.union(setA, setB); // 并集
      Set<String> intersection = Sets.intersection(setA, setB); // 交集
      Set<String> difference = Sets.difference(setA, setB); // 差集
      Set<String> symmetricDifference = Sets.symmetricDifference(setA, setB); // 对称差集

Map相关操作

  1. 创建Map

    • 使用Maps.newHashMap()创建一个新的可变HashMap实例。
      Map<String, Integer> map = Maps.newHashMap();
  2. 过滤Map键

    • 使用Maps.filterKeys()根据给定的谓词过滤Map的键。
      
      Map<String, Integer> scores = Maps.newHashMap();
      scores.put("Alice", 85);
      scores.put("Bob", 90);
      Map<String, Integer> highScores = Maps.filterKeys(scores, name -> name.startsWith("A") || name.startsWith("B"));
  3. 转换Map值

    • 使用Maps.transformValues()对Map中的值进行转换。
      Map<String, Integer> scores = Maps.newHashMap();
      scores.put("Alice", 85);
      scores.put("Bob", 90);
      Map<String, String> scoreStrings = Maps.transformValues(scores, score -> score + " points");
      

4. 计算Map的交集

Map的交集是指两个Map中都有的键值对。Guava提供了Maps.difference()方法来计算两个Map的差异,通过该方法我们可以获取交集。

HashMap<String, Integer> mapA = Maps.newHashMap();
mapA.put("a", 1);
mapA.put("b", 2);
mapA.put("c", 3);HashMap<String, Integer> mapB = Maps.newHashMap();
mapB.put("b", 20);
mapB.put("c", 3);
mapB.put("d", 4);MapDifference<String, Integer> mapDifference = Maps.difference(mapA, mapB);
Map<String, Integer> entriesInCommon = mapDifference.entriesInCommon();
System.out.println(entriesInCommon); // 输出两个Map的交集

5. 计算Map的差集

Map的差集是指在一个Map中存在而在另一个Map中不存在的键值对。同样使用Maps.difference()方法,我们可以获取差集。

Map<String, Integer> entriesOnlyOnLeft = mapDifference.entriesOnlyOnLeft(); // 只存在于mapA中的entry
Map<String, Integer> entriesOnlyOnRight = mapDifference.entriesOnlyOnRight(); // 只存在于mapB中的entry
System.out.println(entriesOnlyOnLeft); // 输出只存在于mapA中的entry
System.out.println(entriesOnlyOnRight); // 输出只存在于mapB中的entryMap<String, Integer> mapA = new HashMap<>();mapA.put("a", 1);mapA.put("b", 2);mapA.put("c", 3);Map<String, Integer> mapB = new HashMap<>();mapB.put("b", 20);mapB.put("c", 3);mapB.put("d", 4);MapDifference<String, Integer> difference = Maps.difference(mapA, mapB);Map<String, Integer> symmetricDifference = new HashMap<>();symmetricDifference.putAll(difference.entriesOnlyOnLeft());symmetricDifference.putAll(difference.entriesOnlyOnRight());System.out.println("对称差集: " + symmetricDifference);

6. 计算Map的并集

Map的并集是指两个Map中所有的键值对,包括重复的键,重复的键将以第二个Map中的值为准。

Map<String, Integer> combaMap = new HashMap<>(mapA); // 将mapA作为基础
combaMap.putAll(mapB); // 将mapB中的所有entry添加到合并Map中
System.out.println(combaMap); // 输出两个Map的并集

这些示例展示了Guava库在集合操作方面的一些基本用法,可以帮助简化代码并提高效率。更多详细信息和高级用法可以参考Guava官方文档和相关技术博客。

http://www.15wanjia.com/news/33238.html

相关文章:

  • 网站内容管理系统 下载学seo优化
  • 仿卢松松博客wordpress长沙网站seo服务
  • 南宁网站定制公司网店seo是什么意思
  • 诸暨市政府门户网站网络营销团队
  • 域名申请到网站上传全过程惠州网络推广平台
  • wordpress 优惠券整站优化系统厂家
  • 做交易网站什么开发语言seo有哪些网站
  • 网站网络推广排名前50名免费的网站
  • 网站如何做抖音推广志鸿优化设计
  • 有规范上海关键词优化报价
  • 广州网络公司建站seo是指
  • wordpress主题去谷歌字体淄博seo
  • 网站建设公司源码网络推广最好的网站有哪些
  • 杭州建站供应商什么是网站推广策略
  • java企业门库网站开发互联网项目推广
  • 网站制作费用入什么科目查关键词的排名工具
  • cms wordpress 国内湖南网站建设seo
  • 国家反诈中心app下载怎么注册简述网站内容如何优化
  • 在韩国用什么地图导航安卓优化大师老版本
  • 网站建设网站开发seo快速排名优化
  • 做旅游销售网站平台ppt品牌运营中心
  • wordpress 4.5 多站点不同数据百度客户端下载安装
  • 南昌市网站建设推广武汉企业seo推广
  • 大连建网站需要多少钱福州百度网站排名优化
  • 做美食哪些类型网站网络推广主要是做什么工作
  • 个人网站开发用什么语言微信小程序开发教程
  • 顶顶呱网站建设定制开发公司
  • web网站设计的重庆网站seo多少钱
  • 网站更新和维护怎么做广告推广方案怎么写
  • 网站结构合理免费外链网站seo发布