五、集合——8-Collections
8-Collections
1.概述
Collections是个工具类,该工具类提供了对集合操作的许多方法,如对集合进行排序、修改、查询等操作,还提供了将集合对象设置为不可变、对集合对象实现同步控制等方法。
2.排序操作
Collections中提供了许多方法用于对List集合中元素进行排序:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class CollectionsTest1 { public static void main(String[] args) { //Collections工具类中有许多方法,用来对List集合中的元素进行排序 List list = new ArrayList(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); System.out.println(list); //反转指定list集合中的元素 Collections.reverse(list); System.out.println(list); //对list集合进行随机排序 Collections.shuffle(list); System.out.println(list); //根据元素的自然顺序对指定List集合的元素按升序进行排序 Collections.sort(list); System.out.println(list); //根据指定的Comparator规则来定制list元素的排列顺序 Collections.sort(list, new Comparator(){ @Override public int compare(Object o1, Object o2) { // TODO Auto-generated method stub Integer i1 = (Integer)o1; Integer i2 = (Integer)o2; return i1-i2; }}); System.out.println(list); //Lambda表达式 Collections.sort(list, (o1,o2)->((Integer)o1-(Integer)o2)); System.out.println(list); //指定list集合i索引处的元素与j索引处的元素进行替换 Collections.swap(list, 0, 4); System.out.println(list); } }
3.查找、替换操作
Collections提供许多方法用于查找、替换集合中的元素:
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; public class CollectionsTest2 { public static void main(String[] args) { List list = new ArrayList(); //list集合 list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); System.out.println(list); //使用二分法搜索指定的List集合,获得指定对象在集合中的索引 System.out.println(Collections.binarySearch(list,3)); //根据元素的自然排序,返回集合中的最大元素 System.out.println(Collections.max(list)); //根据Comparator指定的顺序返回集合中的最大元素 System.out.println(Collections.max(list, (o1,o2)->((Integer)o2)-(Integer)o1)); //根据元素的自然排序,返回子集合中的最小元素 System.out.println(Collections.min(list)); //根据Comparator的定制排序返回集合中的最小元素 System.out.println(Collections.min(list, (o1,o2)->((Integer)o2-(Integer)o1))); //使用指定元素替换集合中的所有另一个元素 Collections.fill(list, 2); System.out.println(list); //返回集合中指定元素出现的次数 System.out.println(Collections.frequency(list, 2)); //用新的指定元素替换集合中所有旧的指定元素 Collections.replaceAll(list, 2, 1); System.out.println(list); } }
4.同步控制
在Java的集合中只有老旧的Vecter、Hashtable等集合实现了线程安全,对于其他的集合来说,是没有实现线程安全的,通过Collections工具类中的synchronizedXxx()方法,可以将指定的集合包装成为线程安全的集合:
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class CollectionsTest3 { public static void main(String[] args) { List list = Collections.synchronizedList(new ArrayList()); Set set = Collections.synchronizedSet(new HashSet()); Map map = Collections.synchronizedMap(new HashMap()); } }
5.设置不可变集合
通过Collections工具类的一些方法可以获得指定集合的“只读”集合,即不可修改的集合:
import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; public class CollectionsTest4 { public static void main(String[] args) { //有关Collections工具类不可变集合的方法常用的三类: //1.emptyXxx():返回一个空的,不可变集合对象 List list = Collections.emptyList(); //2.singletonXxx():返回一个只包含指定对象(只有一个元素)的、不可变集合对象 Set set = Collections.singleton("张三"); //3.unmodifiableXxx():返回指定集合的不可变的视图(只读) Map map = new HashMap(); map.put("语文",88); map.put("数学",89); map.put("英语",97); Map unmodifiableMap = Collections.unmodifiableMap(map); } }