zoukankan      html  css  js  c++  java
  • 十一(三)、集合之Collections工具类

    一、概述:

    • Collections是一个操作Set List Map的工具类
    • 提供了一系列静态的方法对集合元素进行排序、查询和修改等操作,
    • 还提供了对计划对象设置不可变、对计划对象实现同步控制等方法

    二、Collections常用方法

    1、排序方法:(均为Static)

    • reverse(List),反转List中元素的顺序;
    • shuffle(List):对List重的元素随机排序
    • sort(List) 根据元素的自然顺序对指定的List集合元素升序排序;
    • sort(List,Comparator):根据指定的Comparator产生的顺序对List集合元素进行排序
    • Swap(List,int i,int j)将指定List集合中的i处元素和j处元素进行交换;

    2、取最值方法: 

     

    Object max(Collection):根据元素的自然顺、返回给定集合中最大元素;

    Object max(Collection,Comparator):根据Comparator指定的顺序,返回给定集合中最大的元素

    Object min(Collection)

    Object min(Collection,Comparator):

    3、其他方法

    • int frequnency(Collection,Object),返回集合中元素出现的次数;
    • void copy(List dest,List src),将src中的内容复制到dest中
    • boolean replaceAll(List list,Object oldVal,Object newVal)使用新值,替换老的值
    • Collections 类中提供了多个synchronizedXxx()方法;

        该方法可以使得指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合是的线程安全问题

    4、测试方法代码:

      1 @Test
      2     public void test() {
      3         ArrayList arrayList = new ArrayList();
      4         arrayList.add(123);
      5         arrayList.add(456);
      6         arrayList.add("AA");
      7         arrayList.add(new Person("Tom",1));
      8         arrayList.add(456);
      9         System.out.println(arrayList.toString());
     10         
     11         Collections.reverse(arrayList);
     12         System.out.println("Collections的 reverse方法"+arrayList);
     13         
     14         
     15     }
     16     @Test
     17     public void test1() {
     18         ArrayList arrayList = new ArrayList();
     19         arrayList.add(123);
     20         arrayList.add(456);
     21         arrayList.add("AA");
     22         arrayList.add(new Person("Tom",1));
     23         arrayList.add(456);
     24         System.out.println(arrayList.toString());
     25         
     26         Collections.shuffle(arrayList);
     27         System.out.println("Collections的 shuffle方法"+arrayList);
     28         
     29         
     30     }
     31     
     32     @Test
     33     public void test2() {
     34         ArrayList arrayList = new ArrayList();
     35         arrayList.add(new Person("Tom",12));
     36         arrayList.add(new Person("joy",13));
     37         arrayList.add(new Person("jerry",15));
     38         arrayList.add(new Person("lucy",1));
     39         System.out.println(arrayList.toString());
     40         
     41         Collections.sort(arrayList);
     42         System.out.println("Collections的 sort的自然排序方法"+arrayList);
     43         
     44         Collections.sort(arrayList,new Comparator<Person>() {
     45 
     46             @Override
     47             public int compare(Person o1, Person o2) {
     48                 if(o1.getAge()-o2.getAge()>1) {
     49                     return 1;
     50                 }
     51                 return -1;
     52             }
     53         });
     54         System.out.println("Collections的 sort和 comparator方法"+arrayList);
     55         
     56         
     57     }
     58     
     59     @Test
     60     public void test3() {
     61         ArrayList arrayList = new ArrayList();
     62         arrayList.add(new Person("Tom",12));
     63         arrayList.add(new Person("joy",13));
     64         arrayList.add(new Person("jerry",15));
     65         arrayList.add(new Person("lucy",1));
     66         System.out.println(arrayList.toString());
     67         Collections.swap(arrayList, 0, 1);
     68         System.out.println("Collections的 swap方法"+arrayList);
     69     }
     70     
     71     @Test
     72     public void test4() {
     73         ArrayList arrayList = new ArrayList();
     74         arrayList.add(new Person("Tom",12));
     75         arrayList.add(new Person("joy",13));
     76         arrayList.add(new Person("jerry",15));
     77         arrayList.add(new Person("lucy",1));
     78         System.out.println(arrayList.toString());
     79         Person person = Collections.max(arrayList,new Comparator<Person>() {
     80 
     81             @Override
     82             public int compare(Person o1, Person o2) {
     83                 if(o1.getAge()-o2.getAge()>1) {
     84                     return 1;
     85                 }
     86                 return -1;            }
     87         });
     88         System.out.println("Collections的 max方法"+person);
     89     }
     90     
     91     @Test
     92     public void test5() {
     93         ArrayList arrayList = new ArrayList();
     94         arrayList.add(new Person("Tom",12));
     95         arrayList.add(new Person("joy",13));
     96         arrayList.add(new Person("jerry",15));
     97         arrayList.add(new Person("lucy",1));
     98         arrayList.add(new Person("lucy",1));
     99         System.out.println(arrayList.toString());
    100         int times = Collections.frequency(arrayList, new Person("lucy",1));
    101         System.out.println("Collections的 frequency方法"+times);
    102     }
    103     
    104     @Test
    105     public void test6() {
    106         ArrayList arrayList = new ArrayList();
    107         arrayList.add(new Person("Tom",12));
    108         arrayList.add(new Person("joy",13));
    109         arrayList.add(new Person("jerry",15));
    110         arrayList.add(new Person("lucy",1));
    111         arrayList.add(new Person("lucy",1));
    112         
    113         //错误
    114 //        ArrayList arrayList1 = new ArrayList();
    115 //        Collections.copy(arrayList1, arrayList);;
    116         List arrayList1 = Arrays.asList(new Object[arrayList.size()]);
    117         Collections.copy(arrayList1, arrayList);;
    118         System.out.println("Collections的 copy方法"+arrayList1);
    119     }
    120     
    121     @Test
    122     public void test7() {
    123         ArrayList arrayList = new ArrayList();
    124         arrayList.add(new Person("Tom",12));
    125         arrayList.add(new Person("joy",13));
    126         arrayList.add(new Person("jerry",15));
    127         arrayList.add(new Person("lucy",1));
    128         arrayList.add(new Person("lucy",1));
    129         
    130         Collections.replaceAll(arrayList, new Person("lucy",1), new Person("lucy",3));
    131         System.out.println("Collections的 replaceAll方法"+arrayList);
    132     }
    133     
    134     @Test
    135     public void test8() {
    136         ArrayList arrayList = new ArrayList();
    137         arrayList.add(new Person("Tom",12));
    138         arrayList.add(new Person("joy",13));
    139         arrayList.add(new Person("jerry",15));
    140         arrayList.add(new Person("lucy",1));
    141         arrayList.add(new Person("lucy",1));
    142         
    143         //返回线程安全的 list
    144         List list = Collections.synchronizedList(arrayList);
    145         System.out.println("Collections的 synchronizedList方法"+list);
    146     }
    View Code
    我从来不相信什么懒洋洋的自由。我向往的自由是通过勤奋和努力实现的更广阔的人生。 我要做一个自由又自律的人,靠势必实现的决心认真地活着。
  • 相关阅读:
    查准率(precision)和查全率(recall)
    数据集大全:25个深度学习的开放数据集
    利用贝叶斯算法实现手写体识别(Python)
    KNN算法识别手写数字
    判断点在直线的左右哪一侧
    多节点bigchaindb集群部署
    java 多线程 3 synchronized 同步
    java 多线程 1 “常用的实现多线程的2种方式”:Thread 和 Runnable
    java 字符串
    java 关键字static
  • 原文地址:https://www.cnblogs.com/lixiuming521125/p/14627765.html
Copyright © 2011-2022 走看看