package com.yang.ex17;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.TreeSet;
import com.yang.exspe01.Treeset;
/*collections专门对集合进行操作的类
*
*
*
* */
public class Demo {
public static void main(String args[]) {
// sortDemo();
// maxDemo();
// binarySearchDemo();
// fillDemo();
// replaceAll();
// orderDemo();
shuffledemo();
}
public static void sortDemo() {
List<String> list = new ArrayList<String>();
list.add("abcd");
list.add("aaaaa");
list.add("dddd");
list.add("ppppp");
list.add("rrrr");
list.add("llll");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
class srtLenComparator implements Comparator<String> {
public int compare(String s1, String s2) {
if (s1.length() > s2.length()) {
return 1;
}
if (s1.length() < s2.length()) {
return -1;
}
return 0;
}
}
public static void binarySearchDemo() {
List<String> list = new ArrayList<String>();
list.add("abcd");
list.add("aaaaa");
list.add("dddd");
list.add("ppppp");
list.add("qrrrr");
list.add("fdsfsllll");
Collections.sort(list);
System.out.println(list);
int index = Collections.binarySearch(list, "qrrrr");
System.out.println(index);
}
public static void maxDemo() {
List<String> list = new ArrayList<String>();
list.add("abcd");
list.add("aaaaa");
list.add("dddd");
list.add("ppppp");
list.add("qrrrr");
list.add("fdsfsllll");
Collections.sort(list);
System.out.println(list);
String max=Collections.max(list);
System.out.println(max);
Collections.max(list);
System.out.println(list);
}
public static void fillDemo() {
List<String> list = new ArrayList<String>();
list.add("abcd");
list.add("aaaaa");
list.add("dddd");
list.add("ppppp");
list.add("qrrrr");
list.add("fdsfsllll");
System.out.println(list);
Collections.fill(list, "pp");//将集合中的元素全部替换
System.out.println(list);
}
public static void replaceAll() {
List<String> list = new ArrayList<String>();
list.add("abcd");
list.add("aaaaa");
list.add("dddd");
list.add("ppppp");
list.add("qrrrr");
list.add("fdsfsllll");
System.out.println(list);
Collections.replaceAll(list, "aaaaa", "kk");
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
}
public static void orderDemo() {
TreeSet<String> treeset = new TreeSet<String>(Collections.reverseOrder());
//反转
treeset.add("abcd");
treeset.add("aaaaa");
treeset.add("dddd");
treeset.add("ppppp");
treeset.add("qrrrr");
treeset.add("fdsfsllll");
Iterator<String> iterator=treeset.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
class strComparator implements Comparator<String>{
public int compare(String s1,String s2){
// int num=s1.compareTo(s1);
// if (num>0) {
// return -1;
// }
// if (num<0) {
// return 1;
return s2.compareTo(s1);
}
}
public static void shuffledemo() {
//按照随机的方式进行排序
List<String> list = new ArrayList<String>();
list.add("abcd");
list.add("aaaaa");
list.add("dddd");
list.add("ppppp");
list.add("qrrrr");
list.add("fdsfsllll");
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
}
//
// public static<t extends Comparable<? extends t>> void sort(List<t> list){
//
// }
// }
//Arrays:用于操作数组
}