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 }
  • 相关阅读:
    女白领在家玩打地鼠游戏,无意间学会python编程,还有教程有源码
    Python入门小游戏,炫酷打地鼠教程第二部分,都是干货
    从python入门开始,玩这个炸弹超人小游戏,打通关就可以掌握编程
    Python如何入门?直接按这个方式玩炸弹超人小游戏,就能掌握编程
    Python如何入门?搭配这些游戏,学习高效还有趣
    资深程序员教你,利用python预测NBA比赛结果,太精彩了
    Python入门小迷宫,走完这个迷宫,就能掌握python编程基础
    从零基础开始,用python手把手教你玩跳一跳小游戏,直接打出高分
    戏精程序员,用python开发了一个女朋友,天天秀恩爱
    Python入门小游戏之坦克大战,不懂编程都能做出来,附所有源码
  • 原文地址:https://www.cnblogs.com/Newbie-Cai/p/5842765.html
Copyright © 2011-2022 走看看