集合框架-工具类-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         // TODO Auto-generated method stub
 15         /*
 16          * Collections:是集合框架的工具类。
 17          * 里面的方法都是静态的。
 18          */
 19 //        demo_1();//排序
 20 //        demo_2();//折半 最值
 21 //        demo_3();//逆序 
 22         demo_4();//替换
 23 
 24     }
 25     
 26     
 27 
 28     private static void demo_4() {
 29         // TODO Auto-generated method stub
 30         List<String> list = new ArrayList<String>();
 31         
 32         list.add("abcde");
 33         list.add("cba");
 34         list.add("zhangsan");
 35         list.add("zhaoliu");
 36         list.add("xiaoqiang");
 37         
 38         System.out.println(list);
 39 //        Collections.replaceAll(list, "cba", "nba");//replaceAll相当于set(indexOf"cba","nba");
 40 //        Collections.fill(list, "cc");//fill一次性将集合的所有值替换或重新初始化一次
 41         Collections.shuffle(list);//随机将元素安在任意位置
 42         System.out.println(list);
 43     }
 44 
 45     private static void demo_3() {
 46         // TODO Auto-generated method stub
 47         
 48 //        TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());//原理就是下面这个方法
 49     /*    TreeSet<String> ts = new TreeSet<String>(new Comparator<String>() {
 50             public int compare(String o1, String o2) {
 51                 int temp = o2.compareTo(o1);
 52                 return temp;
 53             }
 54         });
 55         */ //自己实现
 56         
 57         //reverseOrder(Comparator<T> cmp),将一个已有比较器逆转
 58         TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new ComparatorByLength()));
 59         ts.add("abc");
 60         ts.add("hahaha");
 61         ts.add("zzz");
 62         ts.add("aa");
 63         ts.add("cba");
 64         
 65         System.out.println(ts);
 66     }
 67 
 68     private static void demo_2() {
 69         // TODO Auto-generated method stub
 70         List<String> list = new ArrayList<String>();
 71         
 72         list.add("abcde");
 73         list.add("cba");
 74         list.add("aa");
 75         list.add("zzz");
 76         list.add("cba");
 77         list.add("nbaa");
 78         //折半要先排序
 79         Collections.sort(list);
 80         System.out.println(list);
 81         
 82         int index = Collections.binarySearch(list, "aaa");
 83         
 84         System.out.println("index="+index);
 85         
 86         //获取最大值。
 87 //        String max = Collections.max(list);//max=zzz
 88         String max = Collections.max(list,new ComparatorByLength());
 89         System.out.println("max="+max);
 90     }
 91 
 92     public static void demo_1() {
 93         
 94         List<String> list = new ArrayList<String>();
 95         
 96         list.add("abcde");
 97         list.add("cba");
 98         list.add("aa");
 99         list.add("zzz");
100         list.add("nbaa");
101         System.out.println(list);
102 
103         
104         
105         //对list集合进行指定顺序的排序。
106 //        Collections.sort(list);
107 //        mySort(list);
108 //        mySort(list, new ComparatorByLength());
109         System.out.println(list);
110         
111         
112     }
113     //下面方法相当于Collections.sort(list,new ComparatorByLength);
114     /*
115     public static <T> void mySort(List<T> list,Comparator<? super T> comp) {
116             
117             for (int i = 0; i < list.size()-1; i++) {
118                 for (int j = i+1; j < list.size(); j++) {
119                     if (comp.compare(list.get(i),list.get(j)) >0) {
120 //                        T temp = list.get(i);
121 //                        list.set(i, list.get(j));
122 //                        list.set(j, temp);
123                         Collections.swap(list, i, j);
124                     }
125                 }
126             }
127         }
128     //介绍Collections.swap交换方法
129     /*
130     public static <T extends Comparable<? super T>> void mySort(List<T> list) {
131         for (int i = 0; i < list.size()-1; i++) {
132             for (int j = i+1; j < list.size(); j++) {
133                 if (list.get(i).compareTo(list.get(j))> 0 ) {
134 //                    T temp = list.get(i);
135 //                    list.set(i, list.get(j));
136 //                    list.set(j, temp);
137                     Collections.swap(list, i, j);
138                 }
139             }
140         }
141     }*/
142     
143     //相当于按自然顺序方法升序排列Collections.sort
144     //public static <T extends Comparable<? super T>> void sort(List<T> list)
145     /*
146     public static <T extends Comparable<? super T>> void mySort(List<T> list) {
147         for (int i = 0; i < list.size()-1; i++) {
148             for (int j = i+1; j < list.size(); j++) {
149                 if (list.get(i).compareTo(list.get(j))> 0 ) {
150                     T temp = list.get(i);
151                     list.set(i, list.get(j));
152                     list.set(j, temp);
153                 }
154             }
155         }
156     }*/
157     
158     //传入String类型的集合
159  /*    public static void mySort(List<String> list) {
160         
161         for (int i = 0; i < list.size()-1; i++) {
162             for (int j = i+1; j < list.size(); j++) {
163                 if (list.get(i).compareTo(list.get(j))> 0 ) {
164                     String temp = list.get(i);
165                     list.set(i, list.get(j));
166                     list.set(j, temp);
167                 }
168             }
169         }
170     }*/
171 
172 }
CollectionsDemo

 

posted @ 2021-10-26 11:34  doremi429  阅读(23)  评论(0编辑  收藏  举报