一、集合工具类Collections
Java集合框架中提供了一个操作Set、List和Map等集合的工具类Collections
在Collections中提供了很多static静态方法,帮我们去做:排序,……
package com.woniuxy.java22.study;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ColleactionsStudy {
public static void main(String[] args) {
// TODO Auto-generated method stub
// study01();
study02();
}
private static void study02() {
// TODO Auto-generated method stub
List<Integer> datas = new ArrayList<Integer>();
Collections.addAll(datas, 56,78,45,23,89,45,78,89);
System.out.println(datas);
//升序(默认)
Collections.sort(datas);
System.out.println(datas);
//降序(方式一) Collections.reverseOrder();返回的是当前集合默认采用的比较器的 反比较器
// Comparator<Integer> ro = Collections.reverseOrder();
// Collections.sort(datas, ro);
// System.out.println(datas);
//降序(方式二)
Collections.reverse(datas);
System.out.println(datas);
//用于集合中 交换元素的位置(4,5代表元素的下标)
Collections.swap(datas, 4, 5);
System.out.println(datas);
//返回一个新的不可改变内容的集合
// List<Integer> newDatas = Collections.unmodifiableList(datas);
// newDatas.add(89);//如果改变这行代码,将抛出UnsupportedOperationException
// System.out.println(newDatas);
//返回一个线程安全的集合(将不安全变为安全)
List<Integer> newDatas02 = Collections.synchronizedList(datas);
System.out.println(newDatas02);
}
private static void study01() {
// TODO Auto-generated method stub
List<String> datas = new ArrayList<String>();
// datas.add("a");
// datas.add("b");
// datas.add("c");
// datas.add("d");
// datas.add("e");
// datas.add("f");
//
//将元素添加到集合中去
Collections.addAll(datas, "a","b","c","b","e","f");
System.out.println(datas.size());
//从集合中查找某一个元素,所在的下标
System.out.println(Collections.binarySearch(datas, "e"));
//返回一个线程安全的列表
System.out.println(Collections.checkedList(datas, String.class));
//Collections.checkedMap 同理
//Collections.checkedSet 同理
List<String> newList = new ArrayList<String>();
Collections.addAll(newList, "wq","as","ui","io","po","nm","rt","er");
//完成集合之间的元素覆盖(目标集合元素的个数 > 源集合中元素的个数)
Collections.copy(newList,datas);
System.out.println(newList);
//判断2个集合中,是否存在相同元素
System.out.println(Collections.disjoint(newList,datas));
//将集合中所有的元素,使用ABCD进行替换
// Collections.fill(newList, "ABCD");
System.out.println(newList);
//返回集合中,某一个元素在集合中出现的次数
System.out.println(Collections.frequency(newList, "ABCD"));
//判断一个集合,在例外一个一个集合中首次出现的位置
System.out.println(Collections.indexOfSubList(newList, datas));
//返回集合中 最大的元素
System.out.println(Collections.max(datas));
//返回集合中 最小的元素
System.out.println(Collections.min(datas));
//将集合中 中b 全部替换为 大波
System.out.println(Collections.replaceAll(datas, "b", "大波"));
System.out.println("反转之前:" + datas);
//将集合中的元素,进行顺序反转
Collections.reverse(datas);
System.out.println("反转之后:" + datas);
}
}
二、数组工具类Arrays
Array 叫数组,Arrays叫数组的工具类,它同样提供很多的static方法
package com.woniuxy.java22.study;
import java.util.Arrays;
import java.util.List;
public class ArraysStudy {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] strs = {"张三","李四","王五","赵六","田七","王八","啤酒"};
//将数组转换为List集合(不可改变内容)
List<String> datas = Arrays.asList(strs);
System.out.println(datas);
// datas.add("赵六");// 抛出UnsupportedOperationException
// System.out.println(datas);
//从数组中检索元素所在的下标(二分查找法)
System.out.println(Arrays.binarySearch(strs, "李四"));
//将一个数组复制成一个新的数组
String[] newArr = Arrays.copyOf(strs, 10);
//打印输出数组的具体内容
System.out.println(Arrays.toString(newArr));
//将某一个数组,按照指定范围进行复制[2 5)
String[] newArr02 = Arrays.copyOfRange(strs, 2, 5);
System.out.println(Arrays.toString(newArr02));
//用来比较2个数组,是否内容相同
System.out.println(Arrays.equals(newArr, newArr02));
//将指定数组的所有元素,使用 张老师 进行替换
Arrays.fill(newArr02, "张老师");
System.out.println(Arrays.toString(newArr02));
//对数组进行升序排序(默认)
Integer[] nums = {12,56,23,12,456,223,12,1243,78};
Arrays.parallelSort(nums);//Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
//自定义排序规则,完成降序排列
Arrays.parallelSort(nums,new ArrayComparator());
System.out.println(Arrays.toString(nums));
}
}