1. 程式人生 > 其它 >集合框架-工具類-Collections-逆序替換

集合框架-工具類-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 private static void demo_4() { 26 // TODO Auto-generated method stub 27 List<String> list = new ArrayList<String>(); 28 29 list.add("abcde"); 30 list.add("cba"); 31 list.add("aa"); 32 33 System.out.println(list);
34 Collections.replaceAll(list, "cba", "nba");//replaceAll相當於set(indexOf"cba","nba"); 35 System.out.println(list); 36 } 37 38 private static void demo_3() { 39 // TODO Auto-generated method stub 40 41 // TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());//原理就是下面這個方法 42 /* TreeSet<String> ts = new TreeSet<String>(new Comparator<String>() { 43 public int compare(String o1, String o2) { 44 int temp = o2.compareTo(o1); 45 return temp; 46 } 47 }); 48 */ //自己實現 49 50 //reverseOrder(Comparator<T> cmp),將一個已有比較器逆轉 51 TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new ComparatorByLength())); 52 ts.add("abc"); 53 ts.add("hahaha"); 54 ts.add("zzz"); 55 ts.add("aa"); 56 ts.add("cba"); 57 58 System.out.println(ts); 59 } 60 61 private static void demo_2() { 62 // TODO Auto-generated method stub 63 List<String> list = new ArrayList<String>(); 64 65 list.add("abcde"); 66 list.add("cba"); 67 list.add("aa"); 68 list.add("zzz"); 69 list.add("cba"); 70 list.add("nbaa"); 71 //折半要先排序 72 Collections.sort(list); 73 System.out.println(list); 74 75 int index = Collections.binarySearch(list, "aaa"); 76 77 System.out.println("index="+index); 78 79 //獲取最大值。 80 // String max = Collections.max(list);//max=zzz 81 String max = Collections.max(list,new ComparatorByLength()); 82 System.out.println("max="+max); 83 } 84 85 public static void demo_1() { 86 87 List<String> list = new ArrayList<String>(); 88 89 list.add("abcde"); 90 list.add("cba"); 91 list.add("aa"); 92 list.add("zzz"); 93 list.add("nbaa"); 94 System.out.println(list); 95 96 97 98 //對list集合進行指定順序的排序。 99 // Collections.sort(list); 100 // mySort(list); 101 // mySort(list, new ComparatorByLength()); 102 System.out.println(list); 103 104 105 } 106 //下面方法相當於Collections.sort(list,new ComparatorByLength); 107 /* 108 public static <T> void mySort(List<T> list,Comparator<? super T> comp) { 109 110 for (int i = 0; i < list.size()-1; i++) { 111 for (int j = i+1; j < list.size(); j++) { 112 if (comp.compare(list.get(i),list.get(j)) >0) { 113 // T temp = list.get(i); 114 // list.set(i, list.get(j)); 115 // list.set(j, temp); 116 Collections.swap(list, i, j); 117 } 118 } 119 } 120 } 121 //介紹Collections.swap交換方法 122 /* 123 public static <T extends Comparable<? super T>> void mySort(List<T> list) { 124 for (int i = 0; i < list.size()-1; i++) { 125 for (int j = i+1; j < list.size(); j++) { 126 if (list.get(i).compareTo(list.get(j))> 0 ) { 127 // T temp = list.get(i); 128 // list.set(i, list.get(j)); 129 // list.set(j, temp); 130 Collections.swap(list, i, j); 131 } 132 } 133 } 134 }*/ 135 136 //相當於按自然順序方法升序排列Collections.sort 137 //public static <T extends Comparable<? super T>> void sort(List<T> list) 138 /* 139 public static <T extends Comparable<? super T>> void mySort(List<T> list) { 140 for (int i = 0; i < list.size()-1; i++) { 141 for (int j = i+1; j < list.size(); j++) { 142 if (list.get(i).compareTo(list.get(j))> 0 ) { 143 T temp = list.get(i); 144 list.set(i, list.get(j)); 145 list.set(j, temp); 146 } 147 } 148 } 149 }*/ 150 151 //傳入String型別的集合 152 /* public static void mySort(List<String> list) { 153 154 for (int i = 0; i < list.size()-1; i++) { 155 for (int j = i+1; j < list.size(); j++) { 156 if (list.get(i).compareTo(list.get(j))> 0 ) { 157 String temp = list.get(i); 158 list.set(i, list.get(j)); 159 list.set(j, temp); 160 } 161 } 162 } 163 }*/ 164 165 }
CollectionsDemo