zoukankan      html  css  js  c++  java
  • 类Collections的静态方法的使用(代码)

      1 package cn.itcast.p2.toolclass.collections.demo;
      2 
      3 import java.util.ArrayList;
      4 import java.util.Collections;
      5 import java.util.Comparator;
      6 import java.util.List;
      7 import java.util.TreeSet;
      8 
      9 import cn.itcast.p2.comparator.ComparatorByLength;
     10 
     11 public class CollectionsDemo {
     12 
     13     public static void main(String[] args) {
     14         /*
     15          * Collections:是集合框架的工具类。
     16          * 里面的方法都是静态的。
     17          */
     18         
     19         demo_4();
     20 
     21     }
     22 
     23     public static void demo_4() {
     24         List<String> list = new ArrayList<String>();
     25         
     26         list.add("abcde");
     27         list.add("cba");
     28         list.add("aa");
     29         list.add("zz");
     30         list.add("cba");
     31         list.add("nbaa");
     32         Collections.sort(list);
     33         System.out.println(list);
     34         
     35         /*
     36          * 替换
     37          * static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)
     38          * 使用另一个值替换列表中出现的所有某一指定值。
     39          *
     40          * 随机置换
     41          * static void shuffle(List<?> list)
     42          * 使用默认随机源对指定列表进行置换。
     43          * static void shuffle(List<?> list, Random rnd) 
     44          * 使用指定的随机源对指定列表进行置换。
     45          * 
     46          * 替换
     47          * static <T> void fill(List<? super T> list, T obj)
     48          *  使用指定元素替换指定列表中的所有元素。
     49          */
     50         Collections.replaceAll(list, "cba", "nba");
     51         System.out.println(list);
     52         
     53         Collections.shuffle(list);
     54         System.out.println(list);
     55         
     56         Collections.fill(list, "ccc");
     57         System.out.println(list);
     58         
     59     }
     60     /*
     61      * demo_4结果:[aa, abcde, cba, cba, nbaa, zz]
     62                  [aa, abcde, nba, nba, nbaa, zz]
     63                  [abcde, zz, nba, aa, nbaa, nba]  每次运行结果都不同
     64                   [ccc, ccc, ccc, ccc, ccc, ccc]
     65 
     66      */
     67 
     68     public static void demo_3() {
     69         /*
     70          * 反转顺序
     71          * static <T> Comparator<T> reverseOrder() 
     72          * 返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的自然顺序。
     73          * static <T> Comparator<T> reverseOrder(Comparator<T> cmp) 
     74          * 返回一个比较器,它强行逆转指定比较器的顺序。
     75          */
     76         TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new ComparatorByLength()));
     77         
     78         ts.add("abc");
     79         ts.add("cba");
     80         ts.add("hahaha");
     81         ts.add("aa");
     82         ts.add("zzz");
     83         
     84         System.out.println(ts);
     85     }
     86     /*
     87      * demo_3运行结果:  [hahaha, zzz, cba, abc, aa]
     88      */
     89 
     90     public static void demo_2() {
     91         List<String> list = new ArrayList<String>();
     92         
     93         list.add("abcde");
     94         list.add("cba");
     95         list.add("aa");
     96         list.add("zz");
     97         list.add("cba");
     98         list.add("nbaa");
     99         Collections.sort(list);
    100         System.out.println(list);
    101         
    102         /*
    103          * 二分搜索法
    104          * static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
    105          *  static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
    106          */
    107         int index = Collections.binarySearch(list, "cba");
    108         System.out.println("index:"+index);  //2
    109         int index2 = Collections.binarySearch(list, "aaa");
    110         System.out.println("index:"+index2);  //-2    如果没有所要搜寻的key存在,则返回(-(插入点)-1)
    111         
    112         /*
    113          * 获取最大值
    114          * static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
    115          *  根据元素的自然顺序,返回给定 collection 的最大元素。
    116          * static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
    117          * 根据指定比较器产生的顺序,返回给定 collection 的最大元素。
    118          * 
    119          * 获取最小值  类似上面
    120          */
    121         String max = Collections.max(list, new ComparatorByLength());
    122         System.out.println("max:"+max);
    123         
    124     }
    125 
    126     public static void demo_1() {
    127         List<String> list = new ArrayList<String>();
    128         
    129         list.add("abcde");
    130         list.add("cba");
    131         list.add("aa");
    132         list.add("zz");
    133         list.add("cba");
    134         list.add("nbaa");
    135         
    136         System.out.println(list);
    137         
    138         /*
    139          * 两种比较方式
    140          */
    141         
    142         //对list集合进行指定顺序的排序。
    143         //static <T extends Comparable<? super T>> void sort(List<T> list)
    144         Collections.sort(list);
    145 //        mySort(list);               //sort的执行原理
    146         System.out.println(list);
    147         
    148         //static <T> void sort(List<T> list, Comparator<? super T> c)
    149         Collections.sort(list, new ComparatorByLength());
    150 //        mySort(list , new ComparatorByLength());  // 上一条代码的执行原理
    151         System.out.println(list);
    152     }
    153 
    154     //sort(List<T> list, Comparator<? super T> c)  执行原理
    155     public static <T> void mySort(List<T> list, Comparator<? super T> comp) {
    156         for (int i=0; i<list.size()-1; i++)
    157             for (int j=i+1; j<list.size(); j++)
    158             {
    159                 if (comp.compare(list.get(i), list.get(j)) > 0)
    160                     Collections.swap(list, i, j);
    161             }
    162     }
    163 
    164     //sort(List<T> list)的执行原理
    165     public static <T extends Comparable<? super T>> void mySort(List<T> list) {
    166         
    167         for (int i=0; i<list.size()-1; i++)
    168             for (int j=i+1; j<list.size(); j++)
    169             {
    170                 if (list.get(i).compareTo(list.get(j)) > 0)
    171                     Collections.swap(list, i, j);   //交换两个元素
    172             }
    173     }
    174 
    175 }
  • 相关阅读:
    2017年3月10号课堂笔记
    2017年3月8号课堂笔记
    2017年3月6号课堂笔记
    2017年3月3号课堂笔记
    第7讲:设计PE型病毒2
    第6讲:设计PE型病毒1
    第5讲:HOOK 任务管理器 第2种方法注入
    第4讲:HOOK 任务管理器 无法结束进程
    第3讲:导入表的定位和读取操作
    第2讲:搜索PEB结构获取kernel32.dll的基址暴力搜索内存空间获得 Api 的线性地址
  • 原文地址:https://www.cnblogs.com/Newbie-Cai/p/5842765.html
Copyright © 2011-2022 走看看