java中Collection和Collections的区别

351 阅读2分钟

1.java.util.Collection

  1. Collection是一个集合框架的父接口。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是为各种具体的集合提供了最大化的统一操作方式。
  2. Collection接口的继承树,i 代表接口,c 代表实现类

在这里插入图片描述

3.Collection接口是Set、List和Queue接口的父接口,Collection通常情况下不被直接使用。
因此Collection 接口定义了一些通用的方法,List 接口和 Set 接口继承自 Collection 接口,所以也可以调用这些方法。

2.java.util.Collections

Collections 是一个包装类。它包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,服务于Java的Collection框架。他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。
2.Collections常用方法

代码演示:

public static void main(String[] args) {     
        List<Integer> list1 = new ArrayList<Integer>();
        list1.add(4);
        list1.add(5);
        list1.add(9);
        list1.add(2);
        list1.add(3);
        System.out.println(list1);

        //对集合进行排序
        Collections.sort(list1);
        System.out.println(list1);

        //反转集合中的元素的顺序
        Collections.reverse(list1);
        System.out.println(list1);

        //对集合进行随机排序
        Collections.shuffle(list1);
        System.out.println(list1);

        //获取集合最大值、最小值
        int max = Collections.max(list1);
        int min = Collections.min(list1);
        System.out.println("最大值:" + max + " 最小值: " + min);

        List<String> list2 = Arrays.asList("one,two,three,four,five,six,seven".split(","));
        System.out.println(list2);

        //二分查找查找集合指定元素,返回元素所在索引,若元素不存在,n表示该元素最有可能存在的位置索引
        int index1 = Collections.binarySearch(list2, "three");
        int index2 = Collections.binarySearch(list2, "ffff");
        System.out.println(index1);
        System.out.println(index2);

        //查找子串在集合中首次出现的位置
        List<String> subList = Arrays.asList("three,four".split(","));
        int index3 = Collections.indexOfSubList(list2, subList);
        System.out.println(index3);
        //从后往前找
        int index4 = Collections.lastIndexOfSubList(list2, subList);
        System.out.println(index4);

        //替换集合中指定的元素,若元素存在返回true,否则返回false
        boolean flag = Collections.replaceAll(list2, "seven", "替换");
        System.out.println(flag);


        //集合中的元素向后移动k位置,后面的元素出现在集合开始的位置
        Collections.rotate(list2, 3);
        System.out.println(list2);

        //将集合list3中的元素复制到list2中,并覆盖相应索引位置的元素
        List<String> list3 = Arrays.asList("1,2,3".split(","));
        Collections.copy(list2, list3);
        System.out.println(list2);

        //交换集合中指定元素的位置
        Collections.swap(list2, 0, 3);
        System.out.println(list2);

        //替换集合中的所有元素,用对象object
        Collections.fill(list2, "****");
        System.out.println(list2);

        //生成一个指定大小与内容的集合
        List<String> list4 = Collections.nCopies(5, "00000");
        System.out.println(list4);

        //为集合生成一个枚举
        List<String> list5 = Arrays.asList("I am a student".split(" "));
        Enumeration<String> e = Collections.enumeration(list5);
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
    }

运行结果:

在这里插入图片描述

参考文章:
Java:集合,Collection接口框架图
Java集合类Collections常用方法总结