zoukankan      html  css  js  c++  java
  • Commons-Collections(二)之set

    MultiSet

    set我们都知道,它是无序的,并且是不允许出现重复元素的。
    但有些场景我们不需要顺序,但是我们需要知道指定key出现的个数(比如每样产品ID对应的剩余数量这种统计信息),那么用MultiSet统计是一个很好的方案

    HashMultiSet

    底层实现原理为HashMap和MutableInteger

        public static void main(String[] args) {
            MultiSet<String> set = new HashMultiSet<>();
    
            set.add("fang");
            set.add("fang");
            set.add("shi");
            set.add("xiang");
            set.add("xiang");
            set.add("xiang");
    
            //我们发现此set是无序的,但是允许了重复元素的进入 并且记录了总数
            System.out.println(set); //[shi:1, xiang:3, fang:2]
            System.out.println(set.size()); //6 = 1+3+2
    
            //批量添加  一些字就添加N个
            set.add("test",5);
            System.out.println(set); //[test:5, shi:1, xiang:3, fang:2]
    
            //移除方法
            System.out.println(set.getCount("fang")); //2
            set.remove("fang");
            //此移除 一次性只会移除一个
            System.out.println(set.getCount("fang")); //1
            //一次性全部移除 N个
            set.remove("xiang", set.getCount("xiang"));
            System.out.println(set.getCount("xiang")); //0  已经被全部移除了
    
            //removeAll 吧指定的key,全部移除
            set.removeAll(Arrays.asList("fang","shi","xiang","test"));
            System.out.println(set); //[]
        }

    PredicatedMultiSet 使用较少,不做讲解

    SetUtils

    difference:找到两个set之间的不同元素

    返回的是第一个set里有的,但是第二个set里没有的元素们

        public static void main(String[] args) {
            Set<String> set1 = new HashSet<String>(){{
                add("a");
                add("b");
                add("c");
            }};
            Set<String> set2 = new HashSet<String>(){{
                add("c");
                add("d");
                add("e");
            }};
    
            SetUtils.SetView<String> difference = SetUtils.difference(set1, set2);
            System.out.println(difference); //[a,b]
    
            Set<String> strings = difference.toSet();
            System.out.println(strings); //[a,b]
        }
    disjunction:和上面方法类似,但是属于加强版

    会返回第一个set和第二个有差异的所有元素们

        public static void main(String[] args) {
            Set<String> set1 = new HashSet<String>(){{
                add("a");
                add("b");
                add("c");
            }};
            Set<String> set2 = new HashSet<String>(){{
                add("c");
                add("d");
                add("e");
            }};
    
            SetUtils.SetView<String> difference = SetUtils.disjunction(set1, set2);
            System.out.println(difference); //[a, b, d, e]
    
            Set<String> strings = difference.toSet();
            System.out.println(strings); //[a, b, d, e]
        }
    emptyIfNull:见上MapUtils类似方法
    newIdentityHashSet:可以实例化出一个newIdentityHashSet
    isEqualSet:

    两个set里面的元素是否都一样(长度一样、元素一样),有时候判断还是非常有用的

    union:合并两个set,生成一个新的set
        public static void main(String[] args) {
            Set<String> set1 = new HashSet<String>(){{
                add("a");
                add("b");
                add("c");
            }};
            Set<String> set2 = new HashSet<String>(){{
                add("c");
                add("d");
                add("e");
            }};
    
            SetUtils.SetView<String> union = SetUtils.union(set1, set2);
    
            System.out.println(union); //[a, b, c, d, e]
        }

    类似于addAll的效果,但是它的好处是生成了一个新的set,对原来的set没有污染。

    MultiSetset我们都知道,它是无序的,并且是不允许出现重复元素的。但有些场景我们不需要顺序,但是我们需要知道指定key出现的个数(比如每样产品ID对应的剩余数量这种统计信息),那么用MultiSet统计是一个很好的方案
    HashMultiSet底层实现原理为HashMap和MutableInteger————————————————版权声明:本文为CSDN博主「_YourBatman」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/f641385712/article/details/84109098

  • 相关阅读:
    bitcoin PoW原理及区块创建过程
    Hyperledger Fabric(v1.1.0)编译时遇到的问题
    Hyperledger Fabic中的Transaction流程
    mint linux 18.3 遇到“已安装的 post-installation 脚本 返回了错误号 127 ”问题的解决
    redis--解析字符串
    golang 统计uint64 数字二进制存储中1的数量
    c++ std 最小堆的使用 (用于实现top100之类的功能)
    Linux 信号signal处理函数
    Linux 信号signal处理机制
    LinuxMint 下 B站 番 blv 缓存 转 mp4
  • 原文地址:https://www.cnblogs.com/deityjian/p/11452115.html
Copyright © 2011-2022 走看看