• JavaSE基础之(十九)Collections类、Hutool类、Guava类


    19.1 Collections工具类

    Collections 是 JDK 提供的一个工具类,位于 java.util 包下,提供了一系列的静态方法,方便我们对集合进行各种操作。

    01、排序操作

    • reverse(List list):反转顺序。
    • shuffle(List list):洗牌,将顺序打乱。
    • sort(List list):自然升序。
    • sort(List list, Comparator c):按照自定义的比较器排序。
    • swap(List list, int i, int j):将 i 和 j 位置的元素交换位置。
    /**
     * @author QHJ
     * @date 2022/11/11  14:39
     * @description: CollectionsTest1
     */
    public class CollectionsTest1 {
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("青花椒1");
            list.add("青花椒2");
            list.add("青花椒3");
            list.add("青花椒4");
            list.add("青花椒5");
    
            System.out.println("原始顺序:" + list); // 原始顺序:[青花椒1, 青花椒2, 青花椒3, 青花椒4, 青花椒5]
    
            // 反转
            Collections.reverse(list);
            System.out.println("反转后:" + list); // 反转后:[青花椒5, 青花椒4, 青花椒3, 青花椒2, 青花椒1]
    
            // 洗牌
            Collections.shuffle(list);
            System.out.println("洗牌后:" + list); // 洗牌后:[青花椒2, 青花椒4, 青花椒5, 青花椒3, 青花椒1]
    
            // 自然升序
            Collections.sort(list);
            System.out.println("自然升序后:" + list); // 自然升序后:[青花椒1, 青花椒2, 青花椒3, 青花椒4, 青花椒5]
    
            // 交换
            Collections.swap(list, 2,4);
            System.out.println("交换后:" + list); // 交换后:[青花椒1, 青花椒2, 青花椒5, 青花椒4, 青花椒3]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    02、查找操作

    • binarySearch(List list, Object key):二分查找法,前提是 List 已经排序过了。
    • max(Collection coll):返回最大元素。
    • max(Collection coll, Comparator comp):根据自定义比较器,返回最大元素。
    • min(Collection coll):返回最小元素。
    • min(Collection coll, Comparator comp):根据自定义比较器,返回最小元素。
    • fill(List list, Object obj):使用指定对象填充。
    • frequency(Collection c, Object o):返回指定对象出现的次数。
    /**
     * @author QHJ
     * @date 2022/11/11  14:43
     * @description: CollectionsTest2
     */
    public class CollectionsTest2 {
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("青花椒1");
            list.add("青花椒2");
            list.add("青花椒3");
            list.add("青花椒4");
            list.add("青花椒5");
    
            System.out.println("最大元素:" + Collections.max(list)); // 最大元素:青花椒5
            System.out.println("最小元素:" + Collections.min(list)); // 最小元素:青花椒1
            System.out.println("出现的次数:" + Collections.frequency(list, "青花椒2")); // 出现的次数:1
    
            // 没有排序直接调用二分查找,结果是不确定的
            System.out.println("排序前的二分查找结果:" + Collections.binarySearch(list, "青花椒2")); // 排序前的二分查找结果:1
    
            // 排序后,查找结果和预期一致
            Collections.sort(list);
            System.out.println("排序后的二分查找结果:" + Collections.binarySearch(list, "青花椒2")); // 排序后的二分查找结果:1
    
            // 使用指定对象填充
            Collections.fill(list, "青花椒6");
            System.out.println("填充后的结果:" + list); // 填充后的结果:[青花椒6, 青花椒6, 青花椒6, 青花椒6, 青花椒6]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    03、同步控制

    HashMap 是线程不安全的,ArrayList 其实也是线程不安全的,没法在多线程环境下使用,那 Collections 工具类中提供了多个 synchronizedXxx 方法,这些方法会返回一个同步的对象,从而解决多线程中访问集合时的安全问题。
    在这里插入图片描述

    List<String> list = new ArrayList<>();
    list.add("青花椒1");
    list.add("青花椒2");
    list.add("青花椒3");
    list.add("青花椒4");
    list.add("青花椒5");
    
    
    List<String> synchronizedList = Collections.synchronizedList(list);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    扒一下 SynchronizedList 的源码:

    static class SynchronizedList<E>
        extends SynchronizedCollection<E>
        implements List<E> {
        private static final long serialVersionUID = -7754090372962971524L;
    
        final List<E> list;
    
        SynchronizedList(List<E> list) {
            super(list);
            this.list = list;
        }
    
        public E get(int index) {
            synchronized (mutex) {return list.get(index);}
        }
        
        public void add(int index, E element) {
            synchronized (mutex) {list.add(index, element);}
        }
        public E remove(int index) {
            synchronized (mutex) {return list.remove(index);}
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    其实就是在方法上加了 synchronized 关键字来加锁保证线程的安全。

    04、不可变集合

    • emptyXxx():制造一个空的不可变集合。
    • singletonXxx():制造一个只有一个元素的不可变集合。
    • unmodifiableXxx():为指定集合制作一个不可变集合。

    举个例子,我要在空的不可变集合中添加一个元素:

    List emptyList = Collections.emptyList();
    emptyList.add("非空");
    System.out.println(emptyList);
    
    • 1
    • 2
    • 3

    这段代码在执行的时候就会报错:
    在这里插入图片描述
    这是因为 Collections.emptyList() 会返回一个 Collections 的内部类 EmptyList,而EmptyList 并没有重写父类 AbstractList 的 add(int index, E element) 方法,所以执行的时候就抛出了不支持该操作的 UnsupportedOperationException 了。

    除此之外,emptyList 方法是 final 的,返回的 EMPTY_LIST 也是 final 的,这就表明这是个不可变对象,没办法进行增删改查。

    @SuppressWarnings("unchecked")
    public static final <T> List<T> emptyList() {
        return (List<T>) EMPTY_LIST;
    }
    
    public static final List EMPTY_LIST = new EmptyList<>();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    05、其他

    还有两个比较常用的方法:

    • addAll(Collection c, T… elements):往集合中添加元素。
    • disjoint(Collection c1, Collection c2):判断两个集合是否没有交集。
    /**
     * @author QHJ
     * @date 2022/11/11  15:08
     * @description:
     */
    public class CollectionsTest4 {
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("青花椒1");
            list.add("青花椒2");
            list.add("青花椒3");
            list.add("青花椒4");
            list.add("青花椒5");
    
            List<String> allList = new ArrayList<>();
            Collections.addAll(allList, "青花椒6", "青花椒7", "青花椒8");
            System.out.println("addAll 后:" + allList); // addAll 后:[青花椒6, 青花椒7, 青花椒8]
    
            System.out.println("是否没有交集:" + (Collections.disjoint(list, allList) ? "是" : "否")); // 是否没有交集:是
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    19.2 Hutool工具类

    推荐二哥的文章:https://tobebetterjavaer.com/common-tool/hutool.html

    19.3 Guava工具类

    推荐二哥的文章:https://tobebetterjavaer.com/common-tool/guava.html

  • 相关阅读:
    esp8266 Task任务创建与执行
    IO流学习
    npm yarn 一起使用报错
    JVM运行数据区深度解析
    Java中的正则表达式
    2023计算机毕业设计SSM最新选题之java二手母婴玩具交易平台mzjmh
    Kotlin 数据类(Data Class)
    如何使用vs code调试python程序
    【迭代器设计模式详解】C/Java/JS/Go/Python/TS不同语言实现
    Alien Skin Exposure2024免费版图片颜色滤镜插件
  • 原文地址:https://blog.csdn.net/qq_50994235/article/details/127805767