参考笔记:java Collections类 详解-CSDN博客
目录
一、Collections类简介
二、Collection类常用方法
1. 排序
① static void reverse(List list)
② static void shuffle(List list)
④ static void sort(List list, Comparator c)
⑤ static void swap(List list, int i, int j)
2. 查找
① static T max/min(Collection coll)
② static T max/min(Collection coll,Comparator <? super T> comp)
③ static int frequency(Collection c, Object o)
3. 复制、替换
① static void copy(List dest, List src)
② static boolean replaceAll(List list, T oldVal, T newVal)
4. 添加
① static boolean addAll(Collection c,T ... elements)
一、Collections类简介
① Collections 类是 java 提供的一个操作 List、Set、Map 等集合的工具类。Collections 类中提供了一系列操作集合的静态方法,使用这些方法可以实现对集合元素的排序、查询、修改等操作
② Collections 位于 java.util.Collections 下,其类定义如下:
二、Collection类常用方法
1. 排序
① static void reverse(List<?> list)
对 List 集合中的元素进行反转,颠倒集合中元素的顺序
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : //1.static void reverse(List<?> list) : 反转List集合中元素的顺序List list = new ArrayList();list.add(141);list.add(233);list.add(5);System.out.println("当前集合 = " + list);//反转Collections.reverse(list);System.out.println("反转后的集合 = " + list);}
}
运行结果:
② static void shuffle(List<?> list)
对 List 集合进行 "洗牌" ,即对集合中的元素进行随机排序,且每次 "洗牌" 都是随机的
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 2.static void shuffle(List<?> list) : 对List集合中的元素进行随机"洗牌"List list = new ArrayList();list.add("张小凡");list.add("陆雪琪");list.add("林惊羽");list.add("万剑一");System.out.println("当前集合 = " + list);//第1次洗牌Collections.shuffle(list);System.out.println("第一次洗牌后的集合 = " + list);//第2次洗牌Collections.shuffle(list);System.out.println("第二次洗牌后的集合 = " + list);}
}
运行结果:
③ static void sort(List<T> list)
根据 List 集合中的元素实现的 Comparable 接口的 compareTo 方法进行排序
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 3.static void sort(List<T> list) : 根据List集合中的元素实现的Comparable接口的compareTo方法进行排序List list = new ArrayList();list.add(new Person("张小凡", 25));list.add(new Person("陆雪琪", 23));list.add(new Person("林惊羽", 24));list.add(new Person("万剑一", 100));System.out.println("----------------未使用sort-----------------");list.forEach(item -> System.out.println(item));System.out.println("----------------使用sort(按年龄降序排序)-----------------");Collections.sort(list);list.forEach(item -> System.out.println(item));}
}class Person implements Comparable<Person> {String name;int age;public Person(String name, int age) {this.name = name;this.age = age;}//根据年龄从大到小排序@Overridepublic int compareTo(Person p) {return p.age - this.age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}
}
运行结果:
④ static <T> void sort(List<T> list, Comparator<? super T> c)
根据指定的 Comparator 比较器对 List 集合中的元素进行排序
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 4.static void sort(List<T> list,Comparator <? super T> c) : 根据指定的Comparator比较器对List集合中的元素进行排序List list = new ArrayList();list.add(256);list.add(1024);list.add(512);list.add(2048);System.out.println("当前集合 = "+list);Collections.sort(list, new Comparator<Integer>() {@Override//降序排序public int compare(Integer o1, Integer o2) {return o2 - o1;}});System.out.println("降序排序后的集合 = "+list);}
}
运行结果:
⑤ static void swap(List<?> list, int i, int j)
将 List 集合中的索引 i、j 处的元素进行交换
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 5.static void swap(List<?> list, int i, int j):将List集合中的索引i、j处的元素进行交换List list = new ArrayList();list.add("索引0处元素");list.add("索引1处元素");list.add("索引2处元素");System.out.println("当前集合 = "+list);//索引1、2的元素交换Collections.swap(list,1,2);System.out.println("索引1、2的元素交换,集合 = "+list);}
}
运行结果:
2. 查找
① static T max/min(Collection<? extends T> coll)
max:根据集合中元素实现的 Comparable 接口的 conpareTo 方法,取集合中的元素最大值
min:根据集合中元素实现的 Comparable 接口的 conpareTo 方法,取集合中的元素最小值
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 1.static T max(Collection<? extends T> coll) 、static T min(Collection<? extends T> coll)List list = new ArrayList();list.add(new Person("张小凡", 25));list.add(new Person("陆雪琪", 23));list.add(new Person("林惊羽", 24));list.add(new Person("万剑一", 100));//取出年龄最大的Person对象Person p1 = (Person) Collections.max(list);//取出年龄最小的Person对象Person p2 = (Person) Collections.min(list);System.out.println("年龄最大的Person对象:"+p1);System.out.println("年龄最小的Person对象:"+p2);}
}class Person implements Comparable<Person> {String name;int age;public Person(String name, int age) {this.name = name;this.age = age;}//根据年龄从小到大排序@Overridepublic int compareTo(Person p) {return this.age - p.age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}
}
运行结果:
② static T max/min(Collection<? extends T> coll,Comparator <? super T> comp)
max:根据指定的 Comparator 比较器,取集合中的元素最大值
min:根据指定的 Comparator 比较器,取集合中的元素最小值
演示代码
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 2.static T max/min(Collection<? extends T> coll,Comparator <? super T> comp)List list = new ArrayList();list.add(new Person("张小凡", 25));list.add(new Person("陆雪琪", 23));list.add(new Person("林惊羽", 24));list.add(new Person("万剑一", 100));//取出年龄最大的Person对象Person p1 = (Person) Collections.max(list,new Comparator<Person>() {@Overridepublic int compare(Person p1, Person p2) {return p1.age - p2.age;}});//取出年龄最小的Person对象Person p2 = (Person) Collections.min(list,new Comparator<Person>() {@Overridepublic int compare(Person p1, Person p2) {return p1.age - p2.age;}});System.out.println("年龄最大的Person对象:"+p1);System.out.println("年龄最小的Person对象:"+p2);}
}class Person{String name;int age;public Person(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}
}
运行结果:
③ static int frequency(Collection<?> c, Object o)
返回指定元素 o 在集合 c 中一共出现的次数
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 3.static int frequency(Collection<?> c, Object o)List list = new ArrayList();list.add(1);list.add(1);list.add(1);list.add(999);//获取元素1出现的次数int frequency = Collections.frequency(list, 1);System.out.println("元素1出现的次数:"+frequency);}
}
运行结果:
3. 复制、替换
① static <T> void copy(List<? super T> dest, List<? extends T> src)
将旧集合 src 中的元素拷贝到新集合 dest 中
PS:说是拷贝,其实是用 src 中的元素覆盖掉 dest 中相同位置的元素,所以 dest 的元素个数不能小于 src 的元素个数,否则会抛出下标越界异常 IndexOutBoundsException
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 1.static <T> void copy(List<? super T> dest, List<? extends T> src) : 将旧集合中的内容拷贝到新集合中List list = new ArrayList();list.add(141);list.add(233);list.add(666);list.add(5);System.out.println("当前集合list = " + list);List list2 = new ArrayList();try {Collections.copy(list2, list);} catch (Exception e) {System.out.print("新集合的元素个数小于旧集合时不能直接拷贝,否则抛出以下异常:");System.out.println(e.toString());}list2.add(null);list2.add(null);list2.add(null);list2.add(null);list2.add(null);Collections.copy(list2, list);System.out.println("拷贝后的新集合list2 = " + list2);}
}
运行结果:
② static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)
将 list 集合中所有 oldVal 元素值替换为 newVal
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 2.static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) List list = new ArrayList();list.add(985);list.add(985);list.add(985);list.add(211);System.out.println("当前集合list = " + list);//将所有的985替换为211Collections.replaceAll(list,985,211);System.out.println("替换后的集合list = " + list);}
}
运行结果:
4. 添加
① static <T> boolean addAll(Collection <? super T> c,T ... elements)
将所有指定的元素 T...elments 添加到集合 c 中
案例演示
import java.util.*;
public class demo {public static void main(String[] args) {//演示 : 1.static <T> boolean addAll(Collection <? super T> c,T ... elements)List list = new ArrayList();list.add(985);list.add(985);list.add(985);System.out.println("当前集合list = " + list);//加入3个211Collections.addAll(list,211,211,211);System.out.println("执行addAll之后的集合list = " + list);}
}
运行结果:
🆗,以上就是本文的所有内容了