collections的基本方法介绍
2020-01-15 11:54
274 查看
collections的基本方法
package com.collections; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Before; import org.junit.Test; public class CollectionsTest { List<Integer> list = new ArrayList<>(); @Before public void init() { list.add(123); list.add(456); list.add(12); list.add(78); list.add(456); System.out.println(list); } // 同步控制 @Test public void testSyncronized() { //synchronizedCollection(Collection<T> c) 返回指定线程安全的 Collection Collection<Integer> c = new ArrayList<>(); Collection<Integer> cc = Collections.synchronizedCollection(c); cc.add(1); cc.add(2); System.out.println(cc); //synchronizedList(List<T> list) 返回指定线程安全的 List List<Integer> synList = Collections.synchronizedList(new ArrayList<Integer>()); synList.add(1); synList.add(2); synList.add(3); System.out.println(synList); //synchronizedMap(Map<K,V> m) 返回由指定线程安全的 Map Map<String, String> hm = Collections.synchronizedMap(new HashMap<>()); hm.put("a", "aaa"); System.out.println(hm); //synchronizedSet(Set<T> s) 返回指定线程安全的 Set } // 查找、替换 @Test public void testQueryAndReplace() { //Object max(Collection) 根据元素的自然顺序,返回给定集合中的最大元素 //Integer max = Collections.max(list); //System.out.println(max); // Object min(Collection) 根据元素的自然顺序 返回给定 collection的最小元素 //Integer min = Collections.min(list); //System.out.println(min); //int frequency(Collectio, Object) 返回指定集合中指定元素的出现次数 //int time = Collections.frequency(list, 456); //System.out.println(time); //void copy(List dest,List src) 将 src 中的内容复制到 dest 中 List<Integer> dest = Arrays.asList(new Integer[list.size()]); Collections.copy(dest, list); System.out.println("dest" + dest); //boolean replaceAll(List list,Object oldVal,Object newVal)使用新值替换 List 对象的所有旧值 //Collections.replaceAll(list, 456, 654); //System.out.println(list); } // 排序操作 @Test public void testSortMethod() { //reverse(List) 反转 List 中元素的顺序 //Collections.reverse(list); //System.out.println(list); //shuffle(List) 对 List 集合元素进行随机排序 //Collections.shuffle(list); //System.out.println(list); //sort(List) 根据元素的自然顺序对指定 List 集合元素按升序排序 //Collections.sort(list); //System.out.println(list); //sort(List, Comparator) 根据指定的 Comparator 产生的顺序对List 集合元素进行排序 /*Collections.sort(list, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2.compareTo(o1); } }); System.out.println(list); */ //swap(List, int, int) 将指定 list 集合中的 i 处元素和 j 处元素进行交换 Collections.swap(list, 0, 3); System.out.println(list); } }
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 从变量到封装:一文带你为机器学习打下坚实的Python基础 By 机器之心2017年10月13日 10:43 本文整体梳理了 Python 的基本语法与使用方法,并重点介绍了对机器学习十分重要的且常
- TClientDataSet的基本属性和方法介绍
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
- 正則表達式基本使用方法简单介绍
- UITableView总结:基本使用方法介绍
- Python中__slots__属性介绍与基本使用方法
- Java数据结构 Collections的一些方法介绍
- lombok 介绍及基本使用方法
- lombok 介绍及基本使用方法
- 第11天,Tensorflow的基本函数介绍以及使用方法
- android Handler的基本使用方法和介绍(二)
- Java Servlet基本方法介绍
- lombok 介绍及基本使用方法
- linux中vim基本使用方法介绍
- 多重循环和方法的基本介绍
- JavaServlet基本方法介绍
- SpringMVC框架的基本配置和简单方法介绍
- .net应用程序的配置介绍及基本配置方法(一)
- DOM之--基本并且常用方法介绍
- PerformanceCounter 基本介绍以及示例方法