1. 程式人生 > >java Collections 工具類

java Collections 工具類

ofb read int 交換 個數 frequency sta alt 工具

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); } }
基本方式使用demo

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);
    }
從線程不安全的list轉成成線程安全的list

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());
        }
    }
Enumeration的使用

java Collections 工具類