1.简单排序Collections.sort():
1 //简单排序 2 List<String> staff= new LinkedList<>(); 3 staff.add("aty"); 4 staff.add("world"); 5 staff.add("hello"); 6 Collections.sort(staff);//升序 7 staff.sort(Comparator.reverseOrder());//降序 8 System.out.println(staff.toString());
2.混排Collections.shuffle():
1 //混排 2 List<Integer> numbers = new ArrayList<>(); 3 for (int i = 0; i < 49; i++) { 4 numbers.add(i); 5 } 6 Collections.shuffle(numbers); 7 List<Integer> combining = numbers.subList(0, 6); 8 Collections.sort(combining); 9 System.out.println(combining);
3.二分Collections.binarySearch():
1 //二分 2 List<Integer> list = new ArrayList<>(); 3 for (int i = 0; i < 10; i++) { 4 list.add(i); 5 } 6 System.out.println(list.toString()); 7 System.out.println(Collections.binarySearch(list, 8));
4.一些简单的算法实现:
1 //简单算法实现 2 ArrayList<Integer> numbers = new ArrayList<>(); 3 for (int i = 0; i < 49; i++) { 4 numbers.add(i); 5 } 6 System.out.println(Collections.min(numbers));//最小 7 System.out.println(Collections.max(numbers));//最大 8 9 ArrayList<Integer> numbers2 = new ArrayList<>(); 10 numbers2 = (ArrayList<Integer>) numbers.clone(); 11 System.out.println(numbers.toString()); //复制 12 13 14 Collections.fill(numbers, 1000); 15 System.out.println(numbers.toString());//所有值换成1000 16 17 Collections.addAll(numbers, 300,800,100); 18 System.out.println(numbers.toString());//添加所有值进去 19 20 Collections.replaceAll(numbers, 3, 900);//将所有3提传成900 21 22 ArrayList<Integer> sublist =new ArrayList<>(); 23 for(int i=0;i<4;i++){ 24 sublist.add(i); 25 } 26 System.out.println(Collections.indexOfSubList(numbers,sublist)); 27 28 29 Collections.swap(numbers, 2, 3); 30 System.out.println(numbers.toString());//交换俩元素位置 31 32 Collections.reverse(numbers); 33 System.out.println(numbers);//倒置列表元素 34 35 Collections.rotate(numbers, 2); 36 System.out.println(numbers.toString());//旋转列表元素 37 38 // Collections.frequency(c, o);//返回c中与o相同的元素个数 39 // Collections.disjoint(c1, c2);//如果两个集合没有相同的元素,则返回true 40
5.栈:
1 //栈 2 Stack<Integer> stack = new Stack<>(); 3 stack.push(5);//将5压入栈并返回5 4 stack.pop();//弹出并返回栈顶的元素,栈为空不能调用 5 stack.peek();//返回栈顶元素,但不弹出