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.
Map- Map is other data structure in java but not sub interface of 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
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
- add(Object o)- add object to collection
- remove(Object o)-remove object from collection
- contains(Object o)-find object existence
- iterator()-iterate elements in the collections
- size()-get size of collections(number of object in the collections
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
This blog is not completed yet, it's developing day to day
ReplyDeleteReally Helpfull .. Keep it up buddy :)
ReplyDeleteYea s.ure, Thank you.
DeleteThis is good one ....hope we can learn a lot after referring this....
ReplyDeletethanks for sharing your knowledge
Thanks, sure, it's been updated every day.
DeleteShow us to how to implement this in practical way...!
ReplyDeleteCheck for following URL:
ReplyDeletehttp://collectionsjava.blogspot.com/p/test.html
cannot see the page
ReplyDeletecheck this again friend.
ReplyDeleteURL: http://collectionsjava.blogspot.com/p/test.html
Good work friend.. keep it up..
ReplyDeleteThanks friend,
ReplyDeletegood works....
ReplyDeleteThanks friend, keep in touch, it will help you improve...
ReplyDeleteGreat work machan very usefull
ReplyDeleteThanks chathu................
ReplyDeleteVery helpful, thanks a lot.
ReplyDeleteThank you, keep in touch, i'll update it everyday.
ReplyDeleteGood work Bro.. keep it continue
ReplyDelete