标签:pareto 错误 安全 not com 随机排序 list sys color
1.reverse反转
2.shuffle随机排序
3.sort自然排序
4.sort指定比较器排序
5.swap将下标位置为x和y的元素进行交换
6.max 最大值
7.min 最小值
8.frequency 计算元素个数
9.copy复制List
10.replaceAll 替换所有元素
import java.util.*; /** * Created by meicai on 2017/11/6. */ public class TestCollections { public static void main(String[] args){ test10(); } /*reverse反转 44 85 22 123 */ private static void test1() { List<Integer> numList= Arrays.asList(123,22,85,44); Collections.reverse(numList); numList.forEach(System.out::println); } /** * shuffle随机排序 *22 85 123 44 */ private static void test2() { List<Integer> numList= Arrays.asList(123,22,85,44); Collections.shuffle(numList); numList.forEach(System.out::println); } /**3.sort自然排序 * 22 44 85 123 */ private static void test3() { List<Integer> numList= Arrays.asList(123,22,85,44); Collections.sort(numList); numList.forEach(System.out::println); } /** * sort指定比较器排序,使用匿名内部类 * 123 85 44 22 */ private static void test4_1() { List<Integer> numList= Arrays.asList(123,22,85,44); Collections.sort(numList, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return -o1.compareTo(o2); } } ); numList.forEach(System.out::println); } /** * sort指定比较器排序,使用java8的lambda表达式 * 123 85 44 22 */ private static void test4_2() { List<Integer> numList= Arrays.asList(123,22,85,44); Collections.sort(numList,(t1,t2)->{return -t1.compareTo(t2);}); numList.forEach(System.out::println); } /** * swap将下标位置为x和y的元素进行交换 * 123 44 85 22 */ private static void test5() { List<Integer> numList= Arrays.asList(123,22,85,44); Collections.swap(numList,1,3); numList.forEach(System.out::println); } /** * max * max:123 */ private static void test6() { List<Integer> numList= Arrays.asList(123,22,85,44); Integer max= Collections.max(numList); System.out.println("max:"+max); } /** * frequency返回每个元素在集合中的个数 *count:2 */ private static void test7() { List<Integer> numList= Arrays.asList(123,22,85,44,22); int count= Collections.frequency(numList,22); System.out.println("count:"+count); } /** * copy 异常,错误使用方式 *Exception in thread "main" java.lang.IndexOutOfBoundsException: Source does not fit in dest */ private static void test8() { List<Integer> numList= Arrays.asList(123,22,85,44,22); List<Integer> numListCopy=new ArrayList<>(); Collections.copy(numListCopy,numList); numListCopy.forEach(System.out::println); } /**copy 正确使用方式 * 123 22 85 44 22 */ private static void test9() { List<Integer> numList= Arrays.asList(123,22,85,44,22); List<Integer> numListCopy=Arrays.asList(new Integer[numList.size()]); Collections.copy(numListCopy,numList); numListCopy.forEach(System.out::println); } /** * replaceAll 替换所有元素 * 123 222 85 44 222 */ private static void test10() { List<Integer> numList= Arrays.asList(123,22,85,44,22); Collections.replaceAll(numList,22,222); numList.forEach(System.out::println); } }
11.从线程不安全的list转成成线程安全的list
private static void test11() { List<Integer> numList= Arrays.asList(123,22,85,44); List<Integer> synchronizedList= Collections.synchronizedList(numList); synchronizedList.forEach(System.out::println); }
12.Enumeration的使用
/** * Enumeration的使用 * a1 a2 a3 */ private static void test12() { Enumeration e=new StringTokenizer("a1-a2-a3","-"); while (e.hasMoreElements()){ System.out.println(e.nextElement()); } }
标签:pareto 错误 安全 not com 随机排序 list sys color
原文地址:http://www.cnblogs.com/caohuimingfa/p/7794139.html