// Collections, tool class public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); // Collections, add content to List Collections.addAll(list, 10, 1, 2, 34, 5, 6, 653, 211, 23); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { Integer integer = iterator.next(); System.out.print(integer + "."); } System.out.println("=============================="); Collections.reverse(list);// reverse the content Iterator<Integer> iterator2 = list.iterator(); while (iterator2.hasNext()) { Integer integer = iterator2.next(); System.out.print(integer + "."); } System.out.println("=============================="); // retrieve content, output location int i = Collections.binarySearch(list, 5); System.out.println("Location is: " + i); System.out.println("=============================="); // replace the content if (Collections.replaceAll(list, 23, 30)) { System.out.println("Replacement succeeded"); } System.out.println("=============================="); //sort //The object to be sorted implements the Comparable interface List<String> arrayList = new ArrayList<String>(); Collections.addAll(arrayList, "123","qwe","asd","34"); Collections.addAll(arrayList, "we"); Collections.addAll(arrayList, "look", "accidentally"); Collections.sort(arrayList); Iterator<String> iterator3 = arrayList.iterator(); while(iterator3.hasNext()){ String next = iterator3.next(); System.out.print(next+"."); } System.out.println("=============================="); // swap the element at the specified position List<Integer> arrayList2 = new ArrayList<Integer>(); Collections.addAll(arrayList2, 1,2,3); Collections.swap(arrayList2, 0, 2);//Swap positions 1 and 3 Iterator<Integer> iterator4 = arrayList2.iterator(); while (iterator4.hasNext()) { Integer next = iterator4.next(); System.out.print(next+" "); }
Java.Collections, tool class
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326985303&siteId=291194637
Recommended
Ranking