Collections class

 

Collections is a class of handy static utility methods for Lists, Sets, and Maps. 

 

■ More utility methods for Collections are in the Collection Interface.

 

 

int  binarySearch( List,  lookforObj  )  method

 

  Note that the collection must be sorted before any seasrching here!

 

  If the item is found, it returns a positive number which is its index. It returns a negative number if not found. The not found return is one less than the index valuewhere the not-found value would be inserted into the list if it were present, minus one more, and as a negative number.  i.e. If the not-found element would go at index 3 if present (4th position), you will get back  -4.  i.e. This snippet returns -4 because the  33  is not found.

 

String[ ] myArray = {"55", "22", "11", "44", "xx", "77","66","99","00","88"};

ArrayList  myList = new  ArrayList (Arrays.asList(myArray) );

Collections.sort(myList);                                                                    // Must sort list before searching!

System.out.println("List now contains: " + myList);

System.out.println(Collections.binarySearch(myList, "33"));

 

 

Object  max( Coll ) and  min( Coll )  methods

 

  These find and return the maximum or minimum values in the collection.  i.e. This code will find and return the 99.

 

String[ ] myArray = {"55", "22", "11", "44", "33", "77","66","99","00","88"};

ArrayList  myList = new  ArrayList (Arrays.asList(myArray) );

System.out.println(Collections.max(myList));

 

 

void  shuffle( List )  method

 

  Shuffling a List.   Performs random shuffling.  The same shuffle(...), method, used with asList(...), can shuffle a String’s contents.   i.e

 

import java.util.*;

 

String[ ] myArray = {"00", "11", "22", "33", "44", "55","66","77","88","99"};

ArrayList  myList = new  ArrayList (Arrays.asList(myArray) );

System.out.println("Unshuffled:  " + myList);

Collections.shuffle(myList);

System.out.println("Shuffled:  " + myList);

 

 

void  copy( tolist , fromlist )  method

 

■ Copies in all elements from the source list.

 

■ Throws IndexOutOfBoundsException if the target List is too small

 

■ Ignores any remaining existing elements in the target, if the target List is too big.  i.e Here the last five entries in the destination list are unaffected.  The first five are overlaid.

 

String[ ] s1 = {"00", "11", "22", "33", "44", "55","66","77","88","99"};

String[ ] s2 = {"AA", "BB", "CC", "DD", "EE"};

ArrayList  l1 = new  ArrayList (Arrays.asList(s1) );

ArrayList  l2 = new  ArrayList (Arrays.asList(s2) );

Collections.copy(l1, l2);

System.out.println("After copy:  " + l1);

 

 

void  fill( List, Obj )  method

 

■ Fills an existing List with elements of the specified object.  Thus every element becomes the same.  i.e.  This example forces “aa” into all elements.

 

String[ ] s1 = {"00", "11", "22", "33", "44", "55","66","77","88","99"};

ArrayList  l1 = new  ArrayList (Arrays.asList(s1) );

Collections.fill(l1, "aa");

System.out.println("Ater fill:  " + l1);

 

 

List  ncopies( )  method

 

■ Just for Lists, you can create entire Lists with multiple duplicate elements. i.e.

 

List lotsofAs = Collections.nCopies(10, “A”);

 

List lotsofNulls = Collections.nCopies(10, null);

 

■ Note that results are immutable, so be careful with your constuction.  i.e.

 

List l = new ArrayList(Collections.nCopies(10, "aa"));    // This one is not immutable, because of the “new” creating another object

System.out.println("Before adds: " + l);

l.add("bb");

System.out.println("After adds: " + l);

 

List l = new ArrayList( );

l = (Collections.nCopies(10, "aa"));                                   // This one is immutable

System.out.println("Before adds: " + l);

l.add("bb");

System.out.println("After add: " + l);