Thursday, May 15, 2008

Java Collections

Framework consisits of three parts
  1. interfaces -
  2. implementation - of above interfaces
  3. algorithms - general applicable to either interfaces / implementation
Interfaces
  1. Collection - A collection represents a group of objects, known as its elements. may or may not have duplicate elements .. defines no order. JDK has no direct implementation of this interface.
  2. List - defines order , may have dup
  3. Set - no dup
  4. SortedSet - set with order
  5. Map - not part of hierarchy , just store key value pairs

Implementations
Keeping the two concepts ( interface & implementation) seperate can help you write code like
List l = new ....{ }

You can change implementation when your business condition require you to do so

<>

LinkedList is gr8 if u want to add/remove data from middle , if you want indexed access then ArrayList is better

  • All implementations are unsynchronised
  • All implementations offer fail fast iterators
  • All implementations are serializable and cloneable
  • All implementations support having null elements

Algorithm
the Collections class has methods that allow you to perform action on these data
This class consists exclusively of static methods that operate on or return collections.
Has static variables like EMPTY_LIST , EMPTY_MAP , EMPTY_SET
methods like binarySearch , min , max , reverse

java.util.Collection
All general-purpose Collection implementation classes (which typically implement Collection indirectly through one of its subinterfaces) should provide two "standard" constructors: a void (no arguments) constructor, which creates an empty collection, and a constructor with a single argument of type Collection, which creates a new collection with the same elements as its argument.

Adding Elements Framework consisits of three parts
(i) interfaces -
(ii) implementation - of above interfaces
(iii) algorithms - general applicable to either interfaces / implementation

Interfaces
  • Collection - A collection represents a group of objects, known as its elements. may or may not have duplicate elements .. defines no order. JDK has no direct implementation of this interface.
  • List - defines order , may have dup
  • Set - no dup
  • SortedSet - set with order
  • Map - not part of hierarchy , just store key value pairs

Implementations
Keeping the two concepts ( interface & implementation) seperate can help you write code like
List l = new ....{ }
You can change implementation when your business condition require you to do so



















































INTERFACE

HASH TABLE

RESIZABLE ARRAY

BALANCED TREE

LINKED LIST

HISTORICAL

SET
Hah Set
TreeSet


SORTEDSET


TreeSet


LIST

ArrayList
LinkedList

MAP


HashMap



WeakHashMap



TreeMap


Vector



Stack



HashTable



Properties



SORTEDMAP


TreeMap



LinkedList is gr8 if u want to add/remove data from middle , if you want indexed access then ArrayList is better

  • All implementations are unsynchronised
  • All implementations offer fail fast iterators
  • All implementations are serializable and cloneable
  • All implementations support having null elements

Algorithm
the Collections class has methods that allow you to perform action on these data
This class consists exclusively of static methods that operate on or return collections.
Has static variables like EMPTY_LIST , EMPTY_MAP , EMPTY_SET
methods like binarySearch , min , max , reverse

java.util.Collection
All general-purpose Collection implementation classes (which typically implement Collection indirectly through one of its subinterfaces) should provide two "standard" constructors: a void (no arguments) constructor, which creates an empty collection, and a constructor with a single argument of type Collection, which creates a new collection with the same elements as its argument.

Adding Elements
public boolean add(Object a) -- can only throw UnsupportedOperation exception , not nullpointerExcep
public boolean addAll(Collection c)

Removing Elements
public void clear()
public boolean remove(Object elem)
public boolean removeAll(Collection c)
if original was {1,5,3,8,1,8,2,4,1,3,7,6}
and passed list is {1,7}
every instance of 1 nad 7 will be removed
you get {5,3,8,8,2,4,3,6}

public boolean retainAll(Collection c)
opposite of remove

public Iterator iterator()

finding elements
public boolean contains(Object elem)
public boolean containsAll(Collection c)

checking size
public int size()
public boolean isEmpty()

copy and cloning collection
public Object[] toArray( )
public Object[] toArray(Object[] a)
consults the passed collection in terms of return type and size
if collections.siz() <= a.size() ... elements are returned in array and returned

Collection c = ....
String array[] = new String[c.size()];
array = (String[])c.toArray(array);


check for equality
public boolean equals(Object o)
Note besides Object.equals() interface also has this method to imphasize the fact that need to override Its not necessary to override the Object.equals( ) The general contract for the Object.equals method states that equals must be symmetric (in other words, a.equals(b) if and only if b.equals(a)). The contracts for List.equals and Set.equals state that lists are only equal to other lists, and sets to other sets. Thus, a custom equals method for a collection class that implements neither the List nor Set interface must return false when this collection is compared to any list or set. -- dont try to add colection to itself

List l = new ArrayList(6);
l.add("1");
System.out.println("added 1");
l.add("2");
System.out.println("added 2");
l.add(l);
System.out.println("KABOOM !!");
Iterator listItr = l.iterator();
while(listItr.hasNext())
System.out.println("val is "+listItr.next());

I got the output

added 1
added 2
KABOOM !!
val is 1
val is 2
val is [1, 2, (this Collection)]


collection VS Collection VS Collections

No comments: