Monday, September 1, 2008

Collections class inside java.util

The utility class contains static methods that operate or return collections. Some of the useful static methods for developers daily use
  • sort – Collections.sort
  • binarySerach – List should be sorted first before invoking the search
  • reverse – Reverse the order of elements
  • swap – Swaps elements at the specified positions in the list
  • copy – Copies all the elements from one list to another
  • min – Returns the minimum element in the collection. Elements should implement Comparable (Overloaded method available which accepts a Comparator as the second argument)
  • max – Returns the maximum element in the collection. Elements should implement Comparable (Overloaded method available which accepts Comparator as the second argument)
  • replaceAll – Replaces all the occurrences of one specified value in a list with another
  • unmodifiableCollection – Returns an unmodifiable view of the collection. UnSupportedOperation exception will be thrown in any attempt to modify the returned collection. (UnModifiable List, Set and Map are also available)
  • synchronizedCollection – Returns a synchronized (thread-safe collection). (Synchronized Set, List and Map are also available) Its is very important to note that the end user who is doing the iteration over the collection has to manually do the synchronize.
  • checkedCollection – This is one of the useful method if programmers are not using java1.5. This will returns a dynamically typesafe view of the specified collection. An attempt to insert an element of the wrong type will result in a immediate ClassCastException (Checked List, Set and Map are also available)
  • singleton – Returns an immutable set containing only the specified object (Singleton List and Map are also available).
  • disjoint – Returns true if two specified collections have no elements in common
  • addAll – Adds all the specified elements to the specified collection

No comments: