java基础之Collections类

目录

一:Collections类概述

二:Collections成员方法

1:对集合排序

 2:二分查找指定集合

3:查找最大值

4:反转集合

5:随机源随机排列


一:Collections类概述

针对集合操作 的工具类

二:Collections成员方法

1:对集合排序

public static <T> void sort(List<T> list) 根据其元素的natural ordering对指定的列表进行排序

public class Test1 {
    public static void main(String[] args) {
        //创建 ArrayList对象指定泛型为String
        ArrayList<String> strings = new ArrayList<>();
        //向其中添加元素
        strings.add("hello");
        strings.add("world");
        strings.add("java");
        strings.add("hadoop");
        strings.add("hive");
        //增强for循环变量输出
        for (String string : strings) {
            System.out.println(string);
        }
        System.out.println("==================================");
        //利用Collections对集合排序
        Collections.sort(strings);
        //增强for循环变量排序后的集合
        for (String string : strings) {
            System.out.println(string);
        }

    }
}

 2:二分查找指定集合

public static <T> int binarySearch(List<?> list,T key)使用二分查找算法搜索指定对象的指定列表。 

public class Test1 {
    public static void main(String[] args) {
        //创建 ArrayList对象指定泛型为String
        ArrayList<String> strings = new ArrayList<>();
        //向其中添加元素
        strings.add("hello");
        strings.add("world");
        strings.add("java");
        strings.add("hadoop");
        strings.add("hive");
       //利用 Collections的二分查找查找hello元素
        int i = Collections.binarySearch(strings, "hello");
        System.out.println(i);
     

    }
}

0

3:查找最大值

public static <T> T max(Collection<?> coll)根据其元素的 自然顺序返回给定集合的最大元素。

public class Test1 {
    public static void main(String[] args) {
        //创建 ArrayList对象指定泛型为String
        ArrayList<String> strings = new ArrayList<>();
        //向其中添加元素
        strings.add("hello");
        strings.add("world");
        strings.add("java");
        strings.add("hadoop");
        strings.add("hive");
       //利用 Collections输出集合最大值
        String max = Collections.max(strings);
        System.out.println(max);


    }
}

world

Process finished with exit code 0

4:反转集合

public static void reverse(List<?> list)反转指定列表中元素的顺序

public class Test1 {
    public static void main(String[] args) {
        //创建 ArrayList对象指定泛型为String
        ArrayList<String> strings = new ArrayList<>();
        //向其中添加元素
        strings.add("hello");
        strings.add("world");
        strings.add("java");
        strings.add("hadoop");
        strings.add("hive");
        //增强for循环输出集合元素
        for (String string : strings) {
            System.out.println(string);
        }
        System.out.println("==============================");
       //利用 Collections反转集合
        Collections.reverse(strings);
        //增强for循环输出反转后的集合
        for (String string : strings) {
            System.out.println(string);
        }




    }
}

hello
world
java
hadoop
hive
==============================
hive
hadoop
java
world
hello

Process finished with exit code 0
 

5:随机源随机排列

public static void shuffle(List<?> list)使用默认的随机源随机排列(每次排序结果都不一样)指定的列表。

public class Test1 {
    public static void main(String[] args) {
        //创建 ArrayList对象指定泛型为String
        ArrayList<String> strings = new ArrayList<>();
        //向其中添加元素
        strings.add("hello");
        strings.add("world");
        strings.add("java");
        strings.add("hadoop");
        strings.add("hive");
        //增强for循环输出集合元素
        for (String string : strings) {
            System.out.println(string);
        }
        System.out.println("==============================");
       //利用 Collections随机源随机排列指定的列表。
        Collections.shuffle(strings);
        //增强for循环输出反转后的集合
        for (String string : strings) {
            System.out.println(string);
        }

hello
world
java
hadoop
hive
==============================
java
hello
hadoop
world
hive

Process finished with exit code 0
 

Guess you like

Origin blog.csdn.net/weixin_50691399/article/details/121057194