guava set,map 的交集, 并集, 差集

 

 

 

复制代码
{// set的交集, 并集, 差集
    HashSet<Integer> setA = Sets.newHashSet(1, 2, 3, 4, 5);
    HashSet<Integer> setB = Sets.newHashSet(4, 5, 6, 7, 8);
    //并集
    Sets.SetView<Integer> union = Sets.union(setA, setB);
    log.info("union:" + union);
    //差集 setA-setB
    Sets.SetView<Integer> difference = Sets.difference(setA, setB);
    log.info("difference:" + difference);
    //交集
    Sets.SetView<Integer> intersection = Sets.intersection(setA, setB);
    log.info("intersection:" + intersection);
}

{//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);
    //mapA 和 mapB 相同的 entry
    System.out.println(mapDifference.entriesInCommon());
    //mapA 和 mapB key相同的value不同的 entry
    System.out.println(mapDifference.entriesDiffering());
    //只存在 mapA 的 entry
    System.out.println(mapDifference.entriesOnlyOnLeft());
    //只存在 mapB 的 entry
    System.out.println(mapDifference.entriesOnlyOnRight());
}
复制代码

 

posted @   草木物语  阅读(1009)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2019-07-14 Spring Cloud Ribbon 客户端负载均衡 4.3
2019-07-14 Spring Cloud 如何实现服务间的调用 4.2.3
2018-07-14 mysql json 使用 类型 查询 函数
2018-07-14 java 子类强转父类 父类强转子类
2018-07-14 java UUID
点击右上角即可分享
微信分享提示