简述
Collections:操作Set、List和Map等集合的工具类,主要包含对元素排序、查询、修改、设置不可变和同步控制等方法
排序操作
import java.util.ArrayList;
import java.util.Collections;
public class sortedTest {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(0);
list.add(19);
list.add(23);
list.add(-2);
System.out.println("正常输出:"+list);
//反转指定list集合中元素的顺序
Collections.reverse(list);
System.out.println("反转输出:"+list);
//升序排序
Collections.sort(list);
System.out.println("升序输出:"+list);
//随机排序,洗牌
Collections.shuffle(list);
System.out.println("随机洗牌:"+list);
//将i处元素和j处元素进行交换
Collections.swap(list, 0, 3);
System.out.println("交换输出:"+list);
//n为正数,后n个元素移到前面,n为负数,前n个元素移到后面
Collections.rotate(list, 2);
System.out.println("前移输出:"+list);
//比较器排序,降序
Collections.sort(list, (o1,o2)->{
return (Integer)o1>(Integer)o2?-1:(Integer)o1<(Integer)o2?1:0;
});
System.out.println("降序输出:"+list);
}
}
查找、替换
import java.util.ArrayList;
import java.util.Collections;
public class searchTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(0);
numbers.add(2);
numbers.add(23);
numbers.add(-9);
numbers.add(2);
numbers.add(2);
System.out.println(numbers);
//按元素的自然排序
System.err.println("最大元素:" + Collections.max(numbers));
System.err.println("最小元素:" + Collections.min(numbers));
//集合中指定元素的出现次数
System.out.println("2在numbers中出现的次数:" + Collections.frequency(numbers, 2));
//使用12替换numbers中的所有2
Collections.replaceAll(numbers, 2, 12);
System.out.println("使用12替换numbers中的所有2: "+numbers);
//numbers中的元素处于有序状态才能使用二分查找法
Collections.sort(numbers);
System.out.println("排序: "+numbers);
System.out.println("23在numbers中的索引: "+Collections.binarySearch(numbers, 23));
}
}
同步控制
Collections类中提供了多个synchronizedXxx()方法,将指定集合包装成线程同步的集合,从而解决多线程并发访问集合时的线程安全问题
//线程安全的集合对象,直接将新创建的集合对象传给Collections的synchronizedXxx()方法
Collection c = Collections.synchronizedCollection(new ArrayList());
List list = Collections.synchronizedList(new arrayList());
Set s = Collections.synchronizedSet(new HashSet());
Map m = Collections.synchronizedMap(new HashMap());
与Vector类似,尽量少用Hashtable,即使需要创建线程安全的Map实现类,可通过Collections工具类把HashMap变成线程安全的
如何用Collections工具类把HashMap变成线程安全的?
Map m = Collections.synchronizedMap(new HashMap())
设置不可变集合
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*设置不可变集合,只能访问,不能修改
* Collections.emptyXxx():返回一个空的、不可变的集合,可为List、Map、SortedMap、Set、SortedSet
* singletonXxx():返回一个只包含指定对象的、不可变的集合,可为List、Map
* unmodifiableXxx():返回集合对象的不可变视图,可为List、Map、SortedMap、Set、SortedSet
* 程序可直接调用Set、List、Map的of()方法即可创建包含N个元素的不可变集合
* */
public class unmodifiableCollections {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
// TODO Auto-generated method stub
//返回一个空的、不可变的集合
List emptyList = Collections.EMPTY_LIST;
//返回一个只包含指定对象的、不可变的集合
Set singletonSet = Collections.singleton("你好");
//普通Map集合
Map scores = new HashMap();
scores.put("语文", 87);
scores.put("数学", 97);
//返回集合对象的不可变视图
Map unmodifiableMap = Collections.unmodifiableMap(scores);
//创建包含三个元素的Set集合
Set set = Set.of("Python","Java","Qt");
//创建包含4个元素的List
List list = List.of(1,2,3,4);
//创建包含三个key-value对的Map集合
Map map = Map.of("语文",98,"数学",97,"英语",90);
Map map1 = Map.ofEntries(Map.entry("语文", 98),Map.entry("数学", 97),Map.entry("英语", 97));
System.out.println(emptyList); //[]
System.out.println(unmodifiableMap); //{数学=97, 语文=87}
System.out.println(singletonSet); //[你好]
System.out.println(list); //[1, 2, 3, 4]
System.out.println(set); //[Python, Java, Qt]
System.out.println(map); //{英语=90, 数学=97, 语文=98}
System.out.println(map1); //{数学=97, 英语=97, 语文=98}
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。