一、操作集合的工具类:Collections
1.Collection是一个操作Set、List和Map等集合的工具类。
2.Collection中提供了大量方法对集合元素进行排序、查询和修改等操作,还提供了对集合对象设置不可变、对集合对象实现同步控制等方法。
3.排序操作:
①reverse(List):反转List中元素的顺序。
②shuffle(List):对List集合元素进行随机排序。
③sort(List):根据元素的自然排序对指定List集合元素按升序排序。
④sort(List、Compartor):根据指定的Comparator产生的顺序对List集合元素进行排序。
⑤swap(List、int、int):将指定list集合中的i处元素进行交换。
4.查找、替换
①Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素。(Object min(Collection)同理)
②Object max(Collection,Commparator):根据Commparator指定的顺序,返回给定集合中的最大元素。(Object min(Collection,Commparator)同理)
③int frequency(Collection,Objiect):返回指定集合中指定元素的出现次数。
④boolean replaceAll(List list, Object oldVal, Object newVal):使用新值替换List对象的所有旧值。
1 package day16; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.List; 7 import java.util.function.Function; 8 import java.util.function.ToDoubleFunction; 9 import java.util.function.ToIntFunction; 10 import java.util.function.ToLongFunction; 11 12 public class Test7 { 13 public static void main(String[] args){ 14 List<String> list = new ArrayList<String>(); 15 list.add("b"); 16 list.add("cd"); 17 list.add("ca"); 18 list.add("a"); 19 list.add("a"); 20 list.add("1"); 21 22 System.out.println(list); 23 24 Collections.reverse(list);//反转List中元素的顺序。 25 System.out.println(list); 26 27 Collections.shuffle(list);//对List集合元素进行随机排序。 28 System.out.println(list); 29 30 Collections.sort(list);//list集合字典升序排序 31 System.out.println(list); 32 33 System.out.println(Collections.frequency(list, "a"));//返回指定集合中指定元素的出现次数。 34 35 Collections.replaceAll(list, "b", "bb"); 36 System.out.println(list); 37 38 Collections.swap(list, 0, 4);//将指定list集合中的i处元素进行交换。 39 System.out.println(list); 40 41 System.out.println(Collections.max(list));//根据元素的自然顺序,返回给定集合中的最大元素。 42 System.out.println(Collections.min(list)); 43 44 45 46 Student s1 = new Student(14,"张三"); 47 Student s2 = new Student(12,"李四"); 48 Student s3 = new Student(13,"王五"); 49 Student s4 = new Student(11,"马六"); 50 51 List<Student> stus = new ArrayList<Student>(); 52 stus.add(s1); 53 stus.add(s2); 54 stus.add(s3); 55 stus.add(s4); 56 57 Student s = Collections.max(stus,new Student());//根据Commparator指定的顺序,返回给定集合中的最大元素。 58 System.out.println(s.name + "," +s.age); 59 60 Student ss = Collections.min(stus,new Student()); 61 System.out.println(ss.name + "," +ss.age); 62 63 // for(Student stu:stus){ 64 // System.out.println(stu.name + "," + stu.age); 65 // } 66 // 67 // System.out.println("-----------------"); 68 // 69 // Collections.sort(stus,new Student()); 70 // for(Student stu:stus){ 71 // System.out.println(stu.name + "," + stu.age); 72 // } 73 } 74 } 75 76 class Student implements Comparator<Student>{ 77 int age; 78 String name; 79 public Student(){ 80 81 } 82 83 public Student(int age,String name){ 84 this.age = age; 85 this.name = name; 86 } 87 88 89 public int compare(Student o1, Student o2) {//根据年龄升序排列对象 90 // TODO Auto-generated method stub 91 if(o1.age > o2.age){ 92 return 1; 93 }else if(o1.age < o2.age){ 94 return -1; 95 }else{ 96 return 0; 97 } 98 99 } 100 101 }
打印结果为:
二、同步控制(先了解)
Collections类中提供了多个synchronizedXxx()方法,该方法可使将指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时的线程安全问题。