第一组
1.addAll():添加元素:
2.sort():排序
3.binarySearch() :查找:二分查找,前提集合必须为有序
4.disjoint() :两个集合中是否有共同的元素
5.fill():填充集合中的元素
6.frequency():统计集合中要给元素出现的次数
package com.mgl.NewClass;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
/**
* Tony 的 java
* Collections工具类
**/
public class collectionsMethod01 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList("Tony", "Peter", "Jenny", "Anny"));
// //方法一:
// list.sort(new Comparator<String>() {
// @Override
// public int compare(String o1, String o2) {
// return o1.compareTo(o2);
// }
// });
//方法二:
//(1)添加元素:addAll()
Collections.addAll(list, "诺克萨斯", "弗雷尔卓德");
System.out.println(list);
//(2)排序:sort()
Collections.sort(list);
System.out.println(list);
//(3)查找:binarySearch() 二分查找,前提集合必须为有序
int index = Collections.binarySearch(list, "诺克萨斯");
System.out.println(index);
//(4)两个集合中是否有共同的元素:disjoint() 不相交的 返回boolean
ArrayList<String> list1 = new ArrayList<>(Arrays.asList("1","2","3"));
ArrayList<String> list2 = new ArrayList<>(Arrays.asList("4","5","6"));
boolean disjoint = Collections.disjoint(list1, list2);
System.out.println(disjoint);
//(5)集合中填充所有元素:fill
Collections.fill(list1,"NaN");
System.out.println(list1);
//(6)集合中要给元素出现的次数:frequency()
int result = Collections.frequency(list1, "NaN");
System.out.println(result);
}
}
第二组
7.shuffle():乱序 处理集合
8.reverse():反转集合,逆序排序集合
8.max() min() :获取集合的最大值,最小值
package com.mgl.NewClass;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/**
* Tony 的 java
**/
public class collectionsMethod02 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,5,4,7,11));
//(1)乱序
Collections.shuffle(list);
//(2)逆序
Collections.sort(list);
Collections.reverse(list);
//(3)最大值,最小值
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));
System.out.println(list);
}
}
第三组
9.swap:交换集合中的元素
package com.mgl.NewClass;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/**
* Tony 的 java
**/
public class collectionsMethod03 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList("Tony","Peter","Bob","Jenny"));
//交换
System.out.println("交换前:" + list);
Collections.swap(list,0,2);
System.out.println("交换后:" + list);
}
}