原文链接:https://github.com/Snailclimb/JavaGuide
Collections 工具类和 Arrays 工具类常见方法 Collections Collections 工具类常用方法:
排序
查找,替换操作
同步控制(不推荐,需要线程安全的集合类型时请考虑使用 JUC 包下的并发集合)
排序操作 1 2 3 4 5 6 void reverse (List list) void shuffle (List list) void sort (List list) void sort (List list, Comparator c) void swap (List list, int i , int j) void rotate (List list, int distance)
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 ArrayList<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(-1 ); arrayList.add(3 ); arrayList.add(3 ); arrayList.add(-5 ); arrayList.add(7 ); arrayList.add(4 ); arrayList.add(-9 ); arrayList.add(-7 ); System.out.println("原始数组:" ); System.out.println(arrayList); Collections.reverse(arrayList); System.out.println("Collections.reverse(arrayList):" ); System.out.println(arrayList); Collections.rotate(arrayList, 4 ); System.out.println("Collections.rotate(arrayList, 4):" ); System.out.println(arrayList); Collections.sort(arrayList); System.out.println("Collections.sort(arrayList):" ); System.out.println(arrayList); Collections.shuffle(arrayList); System.out.println("Collections.shuffle(arrayList):" ); System.out.println(arrayList); Collections.swap(arrayList, 2 , 5 ); System.out.println("Collections.swap(arrayList, 2, 5):" ); System.out.println(arrayList); Collections.sort(arrayList, new Comparator<Integer>() { @Override public int compare (Integer o1, Integer o2) { return o2.compareTo(o1); } }); System.out.println("定制排序后:" ); System.out.println(arrayList);
查找,替换操作 1 2 3 4 5 6 7 int binarySearch (List list, Object key) int max (Collection coll) int max (Collection coll, Comparator c) void fill (List list, Object obj) int frequency (Collection c, Object o) int indexOfSubList (List list, List target) boolean replaceAll (List list, Object oldVal, Object newVal) , 用新元素替换旧元素
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ArrayList<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(-1 ); arrayList.add(3 ); arrayList.add(3 ); arrayList.add(-5 ); arrayList.add(7 ); arrayList.add(4 ); arrayList.add(-9 ); arrayList.add(-7 ); ArrayList<Integer> arrayList2 = new ArrayList<Integer>(); arrayList2.add(-3 ); arrayList2.add(-5 ); arrayList2.add(7 ); System.out.println("原始数组:" ); System.out.println(arrayList); System.out.println("Collections.max(arrayList):" ); System.out.println(Collections.max(arrayList)); System.out.println("Collections.min(arrayList):" ); System.out.println(Collections.min(arrayList)); System.out.println("Collections.replaceAll(arrayList, 3, -3):" ); Collections.replaceAll(arrayList, 3 , -3 ); System.out.println(arrayList); System.out.println("Collections.frequency(arrayList, -3):" ); System.out.println(Collections.frequency(arrayList, -3 )); System.out.println("Collections.indexOfSubList(arrayList, arrayList2):" ); System.out.println(Collections.indexOfSubList(arrayList, arrayList2)); System.out.println("Collections.binarySearch(arrayList, 7):" ); Collections.sort(arrayList); System.out.println(Collections.binarySearch(arrayList, 7 ));
同步控制 Collections提供了多个synchronizedXxx()
方法·,该方法可以将指定集合包装成线程同步的集合,从而解决多线程并发访问集合时的线程安全问题。
我们知道 HashSet,TreeSet,ArrayList,LinkedList,HashMap,TreeMap 都是线程不安全的。Collections提供了多个静态方法可以把他们包装成线程同步的集合。
最好不要用下面这些方法,效率非常低,需要线程安全的集合类型时请考虑使用 JUC 包下的并发集合。
方法如下:
1 2 3 4 synchronizedCollection(Collection<T> c) synchronizedList(List<T> list) synchronizedMap(Map<K,V> m) synchronizedSet(Set<T> s)
Collections还可以设置不可变集合,提供了如下三类方法: 1 2 3 4 emptyXxx(): 返回一个空的、不可变的集合对象,此处的集合既可以是List,也可以是Set,还可以是Map。 singletonXxx(): 返回一个只包含指定对象(只有一个或一个元素)的不可变的集合对象,此处的集合可以是:List,Set,Map。 unmodifiableXxx(): 返回指定集合对象的不可变视图,此处的集合可以是:List,Set,Map。 上面三类方法的参数是原有的集合对象,返回值是该集合的”只读“版本。
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 ArrayList<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(-1 ); arrayList.add(3 ); arrayList.add(3 ); arrayList.add(-5 ); arrayList.add(7 ); arrayList.add(4 ); arrayList.add(-9 ); arrayList.add(-7 ); HashSet<Integer> integers1 = new HashSet<>(); integers1.add(1 ); integers1.add(3 ); integers1.add(2 ); Map scores = new HashMap(); scores.put("语文" , 80 ); scores.put("Java" , 82 ); List<Object> list = Collections.emptyList(); System.out.println(list); Set<Object> objects = Collections.emptySet(); System.out.println(objects); Map<Object, Object> objectObjectMap = Collections.emptyMap(); System.out.println(objectObjectMap); List<ArrayList<Integer>> arrayLists = Collections.singletonList(arrayList); System.out.println(arrayLists); Set<ArrayList<Integer>> singleton = Collections.singleton(arrayList); System.out.println(singleton); Map<String, String> nihao = Collections.singletonMap("1" , "nihao" ); System.out.println(nihao); List<Integer> integers = Collections.unmodifiableList(arrayList); System.out.println(integers); Set<Integer> integers2 = Collections.unmodifiableSet(integers1); System.out.println(integers2); Map<Object, Object> objectObjectMap2 = Collections.unmodifiableMap(scores); System.out.println(objectObjectMap2);
Arrays类的常见操作
排序 : sort()
查找 : binarySearch()
比较: equals()
填充 : fill()
转列表: asList()
转字符串 : toString()
复制: copyOf()
排序 : sort()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 int a[] = { 1 , 3 , 2 , 7 , 6 , 5 , 4 , 9 };Arrays.sort(a); System.out.println("Arrays.sort(a):" ); for (int i : a) { System.out.print(i); } System.out.println(); int b[] = { 1 , 3 , 2 , 7 , 6 , 5 , 4 , 9 };Arrays.sort(b, 2 , 6 ); System.out.println("Arrays.sort(b, 2, 6):" ); for (int i : b) { System.out.print(i); } System.out.println(); int c[] = { 1 , 3 , 2 , 7 , 6 , 5 , 4 , 9 };Arrays.parallelSort(c); System.out.println("Arrays.parallelSort(c):" ); for (int i : c) { System.out.print(i); } System.out.println(); char d[] = { 'a' , 'f' , 'b' , 'c' , 'e' , 'A' , 'C' , 'B' };Arrays.parallelSort(d); System.out.println("Arrays.parallelSort(d):" ); for (char d2 : d) { System.out.print(d2); } System.out.println();
在做算法面试题的时候,我们还可能会经常遇到对字符串排序的情况,Arrays.sort()
对每个字符串的特定位置进行比较,然后按照升序排序。
1 2 3 String[] strs = { "abcdehg" , "abcdefg" , "abcdeag" }; Arrays.sort(strs); System.out.println(Arrays.toString(strs));
查找 : binarySearch()
1 2 3 4 5 6 7 8 char [] e = { 'a' , 'f' , 'b' , 'c' , 'e' , 'A' , 'C' , 'B' };Arrays.sort(e); System.out.println("Arrays.sort(e)" + Arrays.toString(e)); System.out.println("Arrays.binarySearch(e, 'c'):" ); int s = Arrays.binarySearch(e, 'c' );System.out.println("字符c在数组的位置:" + s);
比较: equals()
1 2 3 4 5 6 7 8 char [] e = { 'a' , 'f' , 'b' , 'c' , 'e' , 'A' , 'C' , 'B' };char [] f = { 'a' , 'f' , 'b' , 'c' , 'e' , 'A' , 'C' , 'B' };System.out.println("Arrays.equals(e, f):" + Arrays.equals(e, f));
填充 : fill()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 int [] g = { 1 , 2 , 3 , 3 , 3 , 3 , 6 , 6 , 6 };Arrays.fill(g, 3 ); System.out.println("Arrays.fill(g, 3):" ); for (int i : g) { System.out.print(i); } System.out.println(); int [] h = { 1 , 2 , 3 , 3 , 3 , 3 , 6 , 6 , 6 , };Arrays.fill(h, 0 , 2 , 9 ); System.out.println("Arrays.fill(h, 0, 2, 9);:" ); for (int i : h) { System.out.print(i); }
转列表 asList()
1 2 3 4 5 6 7 8 9 List<String> stooges = Arrays.asList("Larry" , "Moe" , "Curly" ); System.out.println(stooges);
转字符串 toString()
1 2 3 4 5 6 char [] k = { 'a' , 'f' , 'b' , 'c' , 'e' , 'A' , 'C' , 'B' };System.out.println(Arrays.toString(k));
复制 copyOf()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 int [] h = { 1 , 2 , 3 , 3 , 3 , 3 , 6 , 6 , 6 , };int i[] = Arrays.copyOf(h, 6 );System.out.println("Arrays.copyOf(h, 6);:" ); for (int j : i) { System.out.print(j); } System.out.println(); int j[] = Arrays.copyOfRange(h, 6 , 11 );System.out.println("Arrays.copyOfRange(h, 6, 11):" ); for (int j2 : j) { System.out.print(j2); } System.out.println();