Java Util (Collections)

Package java.util Collection elements Collections are useful for dealing with sets of data.

In general, a collection is an element used to store data.

 

In Java, a collection is an object used to store other objects.

Collection is new to Java 2.0.

  An Array is a collection. It can store primitives or objects.

Collection can only store objects in Java.

   
  There are three elements of collections:
  Interfaces Define the methods that each type of collection must implement.
  Implementations Implement actual class such as Hashtable and Vector.
  Algorithms Methods that store, retrieve and manipulate elements in a collection.
     
  Collection is collective framework of Java 2.0.
  Collections may possess the four properties:
  Sorted Ascending order. Use equals() of objects.
  Ordered/Unordered Keeps order where object is placed.
  Duplicates Duplicates can be added
  Use keys A key object is used to reference the stored object.
   
Collection Interfaces Collection interface is general in its handling of groups of objects.

 

It accepts any group of objects, regardless of whether duplicates are present, and stores them as unordered.

  By default collections are:
Properties Unsorted/No Duplicate Unordered Allow duplicates
  Set Collection Map
  SortedSet List SortedMap
       
  Map is not a subclass of Collection.
   
Abstract Classes Each of these interfaces have an abstract class that have implemented methods.

AbstractCollection

AbstractSet

     
Methods Adding removing elements
  boolean add(Object element) add elements to a collection
  boolean remove(Object element) remove elements to a collection
     
  Query methods
  int size() Returns the number of elements in the set.
  boolean isEmpty() Test if the set contains no element.
  boolean contains(Object element) Return true if this set contains specified element.
     
  Iterator Method
  Iterator allows programmer to iterate through the data of the element at a time.
  Iterator iterator() Returns a iterator method
  boolean hasNext() Test if the iterator has more element
  Object next() Get the next element in the iteration
  void remove() Remove the element returned by the iterator.
     
  Group
  Deals with the entire collection elements.
  boolean containsAll(Collection c) Returns true if the collection contains all the elements in the specified collection.
  boolean addAll(Collection c) Appends elements to the specified collection.
  void clear() Remove all elements of the collection.
  void removeAll(Collection c) Remove from a collection all of its elements contained in the specified collection.
  void retainAll(Collection c) Retains from a collection all of its elements contained in the specified collection.
     
Set A subclass of the collection class.
  A set does not allow duplicate objects to enter the collection of elements.
Properties Unsorted Unordered No duplicates
  The add() and addAll() methods check for duplication of objects using equals().
     
HashSet HashSet This a concrete class of the Set interface.
  If a new class will not use one of the predefine operations, you have an option of throwing an UnsupportedOperationException.
     
SortedSet SortedSet A subclass of the Set interface.
Properties Sorted Unordered No duplicates
  Object first() Return first (lowest) element of sorted set.
  Object last() Return last (highest) element of sorted set.
  SortedSet subset(Object fromElements, Object toElement) Returns a view of the portion of the sorted set.
  SortedSet headset(Object toElements) Returns a view of the portion of the sorted set whose element are strictly less than toElement
  SortedSet tailset(Object fromElements) Returns a view of the portion of the sorted set whose element are strictly greater than fromElement
     
TreeSet TreeSet This a concrete class of the SortedSet interface.
     
List A subclass of the collection class.
  A list maintains order as objects are added and removed from the collection. This is not the same as sorted.
Properties Unsorted Ordered Duplicates
  The added methods contain an index or index numbers that are used to specify a certain ordered object in the List.
  void add(int index, Object element) Insert element at the specified position
  boolean addAll(int index, Collection c) Appends all elements in the specified collection to the end of the List.
  Object get(int index) Get an element at the specified position
  int indexOf(Object element) Returns the first occurrence of the specified object
  int lastIndexOf(Object element) Returns the last occurrence of the specified object
  Object remove(int index) Remove an element at the specified position
  Object set(int index, Object element) Replaces the element at the specified position in the List with the specified elements.
  ListIterator listIterator() Returns a list iterator object
  ListIterator listIterator(int startindex) Returns a list iterator object at a specified index
  List subList(int fromindex, int toIndex) Returns a view of the portion of this list between the specifed fromIndex, inclusive and toIndex exclusive.
     
  List These are concrete class of the List interface:
LinkList LinkList Used as a stack, queue, or double-ended queue.
Vector Vector A growable array of elements
ArrayList ArrayList This class is roughly equivalent to Vector, except that is unsynchronized.
     
Map This is not a subclass of the collection class.
  A map stores objects that are identified by unique keys.

 

It may not store duplicate keys. But it may store duplicate objects.

Properties Unsorted Unordered No duplicates keys.

Duplicate objects.

    These are concrete class of the Map interface:
Exam Tip When adding duplicate keys, the compiler will not generate an error. The last key value pair added is stored.
Exam Tip The keys can be null
Hashtable Hashtable It has synchonized methods.
HashMap HashMap It has unsynchonized methods.
    This class is roughly equivalent to Vector, except that is unsynchronized.
  void clear() Remove all elements of the map.
  boolean containsKeys(Object key) Return true if this map contains specified key.
  boolean containsValue(Object key) Return true if this map contains specified value.
  Set entrySet() Returns a set view of the mapping contains in this map.
  boolean equals(Object o) Compares specifed object with this map for equality.
  Object get(Object key) Return the value to which this map maps the specified kye.
  int hashCode() Return the hash code for this map.
  boolean isEmpty() Return true if this map contains no key-value.
  Set keySet() Returns a set view of the keys in this map.
  Object put(Object key, Object value) Places the specified key and value in the map
  void putAll(Map t) Copies specified map value into current map.
  Object remove(Object key) Remove an element from the map.
  int size() Returns the number of key-value mappings in this map.
  Collection values() Returns a collection view of the values contained in this map.
     
  Map  
SortedMap SortedMap This a subclass of the Map interface. Allows sorting.
Properties Sorted Unordered No duplicates keys.

Duplicate objects.

  The map is sorted in ascending order by the keys.
Exam Tip The keys can not be null
   
  SortedMap  
TreeMap TreeMap This a concrete class of the SortedMap interface.
Properties Sorted Unordered No duplicates keys.

Duplicate objects.

  Comparator comparator() Returns a comparator associated with the sorted map.
  Object firstKey() Returns the first (lowest) key currently in the sorted map.
  SortedMap headMap(Object toKey) Returns a view of the portion of this sorted map whose key are strictly less than toKey.
  Object lastKey() Returns the last (highest) key currently in the sorted map.
  SortedMap subMap(Object fromKey, Object toKey) Returns a view of the portion of this sorted map whose key range from fromKey, inclusive, to toKey, exclusive.
  SortedMap tailMap(Object fromKey) Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey.

 

     
Collections Class

Static Methods

The Collection class contains static methods that perform operation on the various collections.
  They perform some type of sorting.
  static void shuffle(List l)

static void shuffle(List l, Random rnd)

Take a List, such as a Vector, and shuffles the contents randomly.
  static int binarySearch(List list, Object key) Searches the list for the specifed object using the binary search algoritm .
  static int binarySearch(List list, Object key, Comparator c) Searches the list for the specifed object using the binary search algoritm and collector.
  static void copy(List dest, List src) Copies all the elements from one list into another.
  static Enumeration enumartion(Collection c) Returns an enurmation over the specified collection.
  Other methods  
  static void fill(List list, Object o)  
  static Object max(Collection col)

static Object max(Collection col, Comparator c)

 
  static Object

min(Collection col)

static Object min(Collection col, Comparator c)

 
  static List nCopies(int n, Object o)  
  static void reverse(List l) Reverse the order of the elements
  static Comparator reverseOrder()  
  static void sort(List l)

static void sort(List l, Comparator c)

 
     
Exam Tip There is a difference between the Collection interface and the Collections class:
  Collection is an interface.

Collections class has static classes used to manipulate the Collection interface concrete classes.