1.
package com.fish.util;import java.util.arraylist;import java.util.collections;/* Collection of tool classes (collections) Tell the difference between collection and collections? 1, collection is the root interface of a singleton collection, and collections is a tool class for manipulating objects. collections: Common method: 1, a binary search for list: If the set must be ordered. int binarysearch (List,key); //must sort the list by the natural order of the Elements int binarysearch (List,key, Comparator); //requires that the elements in the list collection are subclasses of comparable . &NBSP;&NBSP;2, sorts the list collection. sort (list); //sorts the list, actually uses the CompareTo method of the object in the list container sort (list,comaprator); //sort by the specified comparer 3, the maximum or minimum value for the collection. max (Collection) max (collection,comparator) min (Collection) min (collection,comparator) 4, reverses the list collection. reverse (list); 5, you can change the Unsynchronized collection to a synchronized collection. set synchronizedset (set<t> s) map synchronizedmap (Map<K,V> m) List synchronizedlist (list<t> list) */ public class demo1&nbsP { public static void main (String[] args) { ArrayList<Integer> list = new ArrayList<Integer> (); list.add (1); list.add ( list.add) (2); list.add (; ) // sort collections.sort (list); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("The index value at which the element is located is:" + collections.binarysearch (list, 12)); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Maximum Value:" + collections.max (list)); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Minimum Value:" + collections.min (list)); &Nbsp; collections.reverse (list); //Flip no need to sort system.out.println ("Elements of the collection:" + list); List= (arraylist<integer>) collections.synchronizedlist (list);//Convert list to Thread-safe method }}
package com.fish.util;import java.util.arraylist;import java.util.collections;import Java.util.comparator;class person{ string name; int age; public person (String name, int Age) { this.name = name; this.age = age; } @Override public string tostring () { return "{name:" +this.name+ " Age:" +this.age+ "}"; }}class AgeComparator implements Comparator<Person>{ @Override public int compare (PERSON&NBSP;O1,&NBSP;PERSON&NBSP;O2) { &nbSp;return o1.age-o2.age; }} public class demo2 { public static void main (String[] args) { ArrayList<Person> list = new ArrayList<Person> (); list.add (New person ("Dog Doll"); list.add (New person ("Zhang San")); list.add (New person ("John Doe", 2)); Collections.sort (List,new agecomparator ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("The index value where the element is located is:" +collections.binarysearch (list, new person ("Zhang San", (), new agecomparator ())); system.out.println (" Maximum value: "+collections.max (List,new agecomparator ())); system.out.println ("min:" +collections.min (List,new agecomparator ())); collections.reverse (list); //flipping does not need to be sorted, so incoming comparators are not required system.out.println ("Elements of the collection:" +list); }}
This article from "Small Fish Blog" blog, declined reprint!
Collections Collection Class Usage methods