Tuesday, May 7, 2013

Java Collections

In Java, group of data handle using java collection framework. java array can hold same type of data but collection can be used for different type of object. There are basic interfaces we should learn first.

collection - any data structures in which object are stored and iterated over.

Collections - utility class with static methods for use with collections.
Collection - actual interface java.util.Collection in which other sub interfaces implemented. 
  • Collection - root interface of the java collection framework
  • Set            -sub interface
  • List            -sub interface
  • Queue       -sub interface
We should learn basic methods inherit to sub interfaces and classes. 
  1. add(Object o)- add object to collection
  2. remove(Object o)-remove object from collection
  3. contains(Object o)-find object existence
  4. iterator()-iterate elements in the collections
  5. size()-get size of collections(number of object in the collections
In additions to above key methods there are more methods relevant to Collection interface.
Map- Map is other data structure in java but not sub interface of Collection framework. 


          Main Interfaces and implementations in java collection framework

















Collection all collection implements  this interface except Map
Set implementations class -> HashSet, LinkedHashSet,TreeSet
List implementations class -> ArrayList, Vector, LinkedList
Queue implementation class -> priorityQueue, LinkedList

Map is a other data structure in java but not implementation of Collection like Set,List,Queue
Map implementations classes - HashMap, HashaTable, LinkedHashMap, TreeMap

Usages:-

Set no duplicates, so object we store in set must correctly implements the hashCode(), and equals() method correctly,
we can add different objects in java collection, at this time check duplicate object for same type object.check for following examples.

HashSet : Not ordered, Not sorted. This is implementation of Set

class TestHashSet{
public static void main(String []args){
  Set<String>set = new HashSet<String>();
  set.add("abc");
  set.add("bef");
  set.add("der");
  System.out.println(set);
  }
}
Output:[der, abc, bef]

LinkedHashSet : Ordered(insertion order), Not Sorted. This is subclass of HashSet

class TestLinkedHasSet{
public static void main(String []args){
  Set<String>set = new LinkedHashSet<String>();
  set.add("abc");
  set.add("bef");
  set.add("der");
  System.out.println(set);
  }
}
Output: [abc, bef, der]

TreeSet: Sorted(Natural Order), Ordered. This is implementation of SortedSet.
Order can be change using Comparator.
class TestTreeSet{
public static void main(String []args){
  Set<String>set = new TreeSet<String>();
  set.add("abc");
  set.add("bef");
  set.add("der");
  System.out.println(set);
  }
}
Output: [abc, bef, der]

Usages:-
List  Allowed duplicate objects. Like growing Array

ArrayList: Ordered(Indexing Order), Not Sorted, Fast Iteration and fast random access.

class TestArrayList{
public static void main(String []args){
  List<String>list = new ArrayList<String>();
  list.add("abc");
  list.add("bef");
  list.add("der");
  System.out.println(list);
  }
}
Output: [abc, bef, der]

Follow these links for more details.


* More About Collection Framework



18 comments:

  1. This blog is not completed yet, it's developing day to day

    ReplyDelete
  2. Really Helpfull .. Keep it up buddy :)

    ReplyDelete
  3. This is good one ....hope we can learn a lot after referring this....

    thanks for sharing your knowledge

    ReplyDelete
  4. Show us to how to implement this in practical way...!

    ReplyDelete
  5. Check for following URL:

    http://collectionsjava.blogspot.com/p/test.html

    ReplyDelete
  6. check this again friend.

    URL: http://collectionsjava.blogspot.com/p/test.html

    ReplyDelete
  7. Good work friend.. keep it up..

    ReplyDelete
  8. Thanks friend, keep in touch, it will help you improve...

    ReplyDelete
  9. Great work machan very usefull

    ReplyDelete
  10. Very helpful, thanks a lot.

    ReplyDelete
  11. Thank you, keep in touch, i'll update it everyday.

    ReplyDelete
  12. Good work Bro.. keep it continue

    ReplyDelete