学习笔记之JavaSE(36)--JavaAPI详解11

来源:互联网 发布:淘宝蚂蚁花呗是什么 编辑:程序博客网 时间:2024/06/01 10:48

今天学习的内容是Collections类和Arrays类


一、Collections类

通过对集合类的学习,我们已经了解各个集合不同特点和使用场景。但要对集合进行某些操作还是比较麻烦,比如对ArrayList进行自然排序或者比较器排序,或者将不同步的集合转换为同步集合(面试要点。为了解决诸如此类的问题,JavaAPI提供了一个集合框架的工具类--Collections,并在此类中定义了许多操作集合类的静态方法。使用这些方法的示例程序如下:

public class Test63 {public static void main(String[] args) {// 为List进行自然排序List<String> list_1 = new ArrayList<>();list_1.add("asd");list_1.add("asd");list_1.add("qesd");list_1.add("zsd");list_1.add("fssdfgd");list_1.add("zz");System.out.println(list_1);// [asd, asd, qesd, zsd, fssdfgd, zz]Collections.sort(list_1);System.out.println(list_1);// [asd, asd, fssdfgd, qesd, zsd, zz]// 为List进行比较器排序List<String> list_2 = new ArrayList<>();list_2.add("asd");list_2.add("asd");list_2.add("qesd");list_2.add("zsd");list_2.add("fssdfgd");list_2.add("zz");System.out.println(list_2);// [asd, asd, qesd, zsd, fssdfgd, zz]Collections.sort(list_2, new ComparatorByStringLength());System.out.println(list_2);// [zz, asd, asd, zsd, qesd, fssdfgd]// 二分法查找int index_1 = Collections.binarySearch(list_1, "zz");int index_2 = Collections.binarySearch(list_2, "zz", new ComparatorByStringLength());System.out.println(index_1);// 5System.out.println(index_2);// 0// 获取最值System.out.println(Collections.max(list_1));// zzSystem.out.println(Collections.min(list_1));// asd// 逆序Set<String> ts_1 = new TreeSet<>(Collections.reverseOrder());// 自然排序的逆序ts_1.add("asc");ts_1.add("zz");ts_1.add("fgjdas");ts_1.add("x");ts_1.add("bsfc");System.out.println(ts_1);// [zz, x, fgjdas, bsfc, asc]Set<String> ts_2 = new TreeSet<>(Collections.reverseOrder(new ComparatorByStringLength()));// 比较器排序的逆序ts_2.add("asc");ts_2.add("zz");ts_2.add("fgjdas");ts_2.add("x");ts_2.add("bsfc");System.out.println(ts_2);// [fgjdas, bsfc, asc, zz, x]// 替换Collections.replaceAll(list_1, "asd", "add");System.out.println(list_1);// [add, add, fssdfgd, qesd, zsd, zz]// 填充Collections.fill(list_1, "hehe");System.out.println(list_1);// [hehe, hehe, hehe, hehe, hehe, hehe]//随机对列表进行置换Collections.shuffle(list_2);System.out.println(list_2);// 随机置换元素位置/* * 将不同步集合转为同步集合 * synchronizedXXX,比如synchronizedList(List<T> list) 等等 */}}// 按照字符串长度进行排序,如果长度相同,按照字符串的自然顺序进行排序class ComparatorByStringLength implements Comparator<String> {@Overridepublic int compare(String o1, String o2) {int temp = o1.length() - o2.length();return temp == 0 ? o1.compareTo(o2) : temp;}}


二、Arrays类

对于数组的工具类Arrays,除了之前学习的方法,还有一个重要方法--asList(T... a),可以将数组转换为列表(注意这里使用了可变参数,它接收的是任意数量的T类型对象,并自动将其封装为数组,简化了调用者的书写)。那为什么要将数组转换为列表呢?这样做的好处是可以使用集合的方法操作数组。同样地,使用Collection接口中的toArray()或toArray(T[] a)方法也可以将列表或集转换为数组,这样做的好处是可以限定对列表或集中元素的操作,不允许对其进行增删。asList()和toArray()方法可以视作数组与集合之间的“桥梁”,它们的具体使用方法和小知识点如下程序示例:

public class Test64 {public static void main(String[] args) {// 将数组转换为列表Integer[] arr = { 2, 2, 3, 16, 23, 6, 12 };boolean b = myContains(arr, 2);System.out.println(b);// true// 将列表或集转换为数组Set<String> set = new HashSet<>();List<String> list = new ArrayList<>();set.add("a");set.add("b");set.add("c");list.add("a");list.add("b");list.add("c");String[] arr_set = set.toArray(new String[set.size()]);// 若传入的指定元素类型的数组的长度小于列表或集的size,该方法会再创建一个同类型长度等于size的数组String[] arr_list = list.toArray(new String[list.size()]);// 若传入的指定元素类型的数组的长度大于列表或集的size,该方法会使用该数组,其他位置为默认值。所以建议使用列表或集的size创建数组System.out.println(Arrays.toString(arr_set));//[b, c, a]System.out.println(Arrays.toString(arr_list));//[a, b, c]}// 判断数组中是否包含某元素public static boolean myContains(Integer[] arr, int i) {/* * 如果数组中的元素是对象,那么将其转换为列表时,会直接将数组中的元素作为集合中的元素进行存储 * 如果数组中的元素是基本数据类型,那么转换为列表时,会将该数组为集合中的元素进行存储 * 所以如果想要将存储基本数据类型的数组转为列表,最好使用包装类定义数组 */List<Integer> list = Arrays.asList(arr);// !list.add(2); UnsupportedOperationException 不能使用列表的增删方法,因为数组的长度是固定的// !list.remove(2); UnsupportedOperationException 不能使用列表的增删方法,数组的长度是固定的if (list.contains(i)) {return true;}return false;}}


0 0