欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【JavaSE_学习笔记】Collections集合工具类

程序员文章站 2024-01-14 08:22:22
...

【JavaSE_学习笔记】Collections集合工具类

Collection与Collections的区别:
  Collection是单列集合的根接口,Collections是操作集合对象的工具类
Collections的常用方法:

1.对list集合进行排序
  void sort(list):对元素进行排序
  void Sort(List list,Comparator c):元素不具备可比性时,传入比较器
举例:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo4 {
    public static void main(String[] args) {
        ArrayList <Integer>arr=new ArrayList<Integer>();
        arr.add(14);
        arr.add(7);
        arr.add(21);
        arr.add(18);
        arr.add(3);
        Collections.sort(arr);
        System.out.println(arr);

        ArrayList <Student>arr2=new ArrayList<Student>();
        MyCompartor compartor = new MyCompartor();

        arr2.add(new Student("张三",15));
        arr2.add(new Student("李四",42));
        arr2.add(new Student("王五",35));
        arr2.add(new Student("赵六",24));
        Collections.sort(arr2, compartor);
        System.out.println(arr2);
    }
}

class MyCompartor implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        // TODO Auto-generated method stub
        return o1.getAge()-o2.getAge();
    }

}

结果:

[3, 7, 14, 18, 21]
[Student [name=张三, age=15], Student [name=赵六, age=24], Student [name=王五, age=35], Student [name=李四, age=42]]

2.对list进行二分查找(必须排好序)
  int binarySearch(list,key)
  int binarySearch(list,key,Comparator)
3.对集合取最大值或最小值
  T max(Collection)
  T max(Collection,Comparator)
  T min(Collection)
  T min(Collection,Comparator)
举例:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

//测试类
public class Demo4 {
    public static void main(String[] args) {
        ArrayList <Integer>arr=new ArrayList<Integer>();
        arr.add(14);
        arr.add(7);
        arr.add(21);
        arr.add(18);
        arr.add(3);
        Collections.sort(arr);
        System.out.println("排好序为:"+arr);
        System.out.println("18的索引为:"+Collections.binarySearch(arr, 18));
        System.out.println("最大值为:"+Collections.max(arr));
        System.out.println("最小值为:"+Collections.min(arr));

        ArrayList <Student>arr2=new ArrayList<Student>();
        MyCompartor compartor = new MyCompartor();
        arr2.add(new Student("张三",15));
        arr2.add(new Student("李四",42));
        arr2.add(new Student("王五",35));
        arr2.add(new Student("赵六",24));
        Collections.sort(arr2, compartor);
        System.out.println("排好序为:"+arr2);
        System.out.println("student(\"王五\",35)索引为:"+Collections.binarySearch(arr2,new Student("王五",35), compartor));
        System.out.println("最大值为:"+Collections.max(arr2, compartor));
        System.out.println("最小值为:"+Collections.min(arr2, compartor));
    }
}

//比较器
class MyCompartor implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        // TODO Auto-generated method stub
        return o1.getAge()-o2.getAge();
    }

}

结果:

排好序为:[3, 7, 14, 18, 21]
18的索引为:3
最大值为:21
最小值为:3
排好序为:[Student [name=张三, age=15], Student [name=赵六, age=24], Student [name=王五, age=35], Student [name=李四, age=42]]
student("王五",35)索引为:2
最大值为:Student [name=李四, age=42]
最小值为:Student [name=张三, age=15]

4.对list集合进行反转
  void reverse(list)
5.对list集合的元素进行位置交换
  void swap(list,x,y)
6.打乱集合中元素的顺序
  shuffle()
7.对list集合进行元素的替换,如果被替换的元素不存在,则原集合不变
  boolean replaceAll(list,old,new)
举例:

import java.util.ArrayList;
import java.util.Collections;


//测试类
public class Demo4 {
    public static void main(String[] args) {
        ArrayList <Integer>arr=new ArrayList<Integer>();
        arr.add(14);
        arr.add(7);
        arr.add(21);
        arr.add(18);
        arr.add(3);
        System.out.println(arr);//[14, 7, 21, 18, 3]
        Collections.reverse(arr);
        System.out.println(arr);//[3, 18, 21, 7, 14]
        Collections.swap(arr, 0, 3);
        System.out.println(arr);//[7, 18, 21, 3, 14]
        Collections.shuffle(arr);
        System.out.println(arr);//[21, 7, 14, 3, 18] 每次都不一样

    }
}

8.将不同步的集合变为同步的集合
  Set synchronizedSet(Set < T> s)
  Map synchronizedMap(Map < K,V> m)
  List synchronizedList(List < T> list)