Collection Framework has been added in JDK 1.2 and has been expanded in 1.4 -1.6
Interface & Classes of Collection Framework
Collection API provides a group of the interface to choose from, but it also gives you some concrete classes to directly play with.
Core Interfaces
- Collection
- List
- Set
- SortedSet (Extends Set Interface)
- NavigableSet (Extends SortedSet)
- Map
- SortedMap (Extends Map Interface)
- Navigable Map (Extends SortedSet)
- Queue
Implemented Classes
1- Map
- HashMap
- HashTable
- TreeMap
- LinkedHashMap
2- Sets
- HashSet
- LinkedHashSet
- TreeSet
3- Lists
- ArrayList
- Vector
- LinkedList
4- Queue
- AbstractQueue
- ArrayBlockingQueue
- ConcurrentLinkedQueue
- DelayQueue
- LinkedBlockingDeque
- LinkedBlockingQueue
- LinkedTransferQueue
- PriorityBlockingQueue
- PriorityQueue
- SynchronousQueue
5- Deque
- ArrayDeque
- ConcurrentLinkedDeque
6- Arrays
Not all the classes in Collection Framework implement the Collection Interface.
None of the Map related classes & Interface extend from Collection.
Collections can also be divided based on sorting and ordering.
1- Ordered Collection
An ordered collection can be iterated in a specific order(not-random). E.G.: Array, HashTable
- LinkedHashSet (Iteration Order is predictable)
- Array
- HashTable
- ArrayList
2– Un-Ordered Collection
- HashSet
3- Sorted Collection
In sorted collections order of the collections is determined based on some rules. e.g. List
List Interface
Property:
Cares about the index
Has methods related to index(indexOf..)
Ordered by Index position
ArrayList
Syntax
List l = new ArrayList<E>();
Property:
- Growable array
- Fast Iteration
- Fast Random Access
- Ordered collection by index
- Unsorted
Vector
Same as an ArrayList but methods in Vector are synchronized.
LinkedList
Property
- Elements are doubly-linked to each other.
- Ordered by Index position
- Good for implementing Stack and Queue
- Fast Insertion and Deletion
Set Interface
Set is used when the uniqueness of the object is to be considered. It doesn’t allow duplicate object. To determine if two objects are equal or not Set uses equals() and hashcode() methods.
HashSet
Property
- Unsorted
- Unordered
- Uses hashcode of the objects
- No Duplicate object
- LinkedHashMap
- Ordered(Insertion)
- Uses hashcode of the objects
- No Duplicate object
TreeSet
- Sorted(Uses Tree Structure)
- Uses hashcode of the objects
- No Duplicate object
——————————————————————————————
Concurrent Collection Interface
- BlockingQueue extends Queue
- TransferQueue extends BlockingQueue
- BlockingDeque extends BlockingQueue
- ConcurrentMap extends Map
Concurrent Collection Implementation
- LinkedBlockingQueue extends AbstractQueue implements BlockingQueue
- ArrayBlockingQueue extends AbstractQueue implements BlockingQueue
- PriorityBlockingQueue extends AbstractQueue implements BlockingQueue
- DelayQueue extends AbstractQueue implements BlockingQueue
- SynchronousQueue extends AbstractQueue implements BlockingQueue
- LinkedBlockingDeque extends AbstractQueue implements BlockingDeque
- LinkedTransferQueue extends AbstractQueue implements TransferQueue
- CopyOnWriteArrayList implements List, RandomAccess
- CopyOnWriteArraySet extends AbstractSet
- ConcurrentSkipListSet extends AbstractSet implements NavigableSet
- ConcurrentHashMap extends AbstractMap implements ConcurrentMap
- ConcurrentSkipListMap extends AbstractMap implements ConcurrentNavigableMap
Factors that could help on deciding Collection Class
There are various factors that can be considered when selecting an appropriate collection for a particular problem. These factors are:
- Ordering – Some sort of ordering in the elements. For example, sorted order, insertion order or no specific ordering.
- Duplicates – May or may not want to allow duplicate elements in a collection.
- Thread Safe – Ensure the safety of the elements in collections in case there are multiple threads accessing it.
- Key-Value pair – Store in key-value pairs.
- Blocking operations – Wait for the collection to become non-empty when retrieving an element.
- Random Access – Instant retrieval of an element.
- Upper Bounds – To limit the maximum number of elements a collection can hold.
There are also other factors like the priority, delay etc..
Cheat-sheet
- A collection is a data structure in which Objects are stored.
- Objects can be Added, Deleted and can be traversed in Collection.
- There are 4 type of basic Collection
- List: Ordered, Duplicates are allowed, Indexed
- Sets: May or may not Be Ordered. Duplicates are not allowed.
- Maps: Duplicate keys are not allowed.
- Queue: Ordered by FIFO or priority.
- ArrayList: Fast Iteration & Fast Random Access.
- Vector: Synchronized Method.
- LinkedList: Good for implementing Stack and Queue.
- HashSet: Fast Access, No Duplicates, No Ordering.
- LinkedHashSet: No Duplicates, Iterates by insertion order.
- TreeSet: No Duplicates, Iterates in sorted order.
This is really nice tutorial about collection
thanks
THANK YOU
I THINK THIS IS BEST SITE FOR JAVA STUDY
can u send core and advanced java full tutorial materials pls.
You can buy eBook from top Menu.
Good Explanation !!!
lookie here
Hi,
Please explain what does synchronized (string.class) mean????
mam can please explain about setters and getters in java..? how and where should we use the setters&getters..?? if we doesn’t use them then what happens..??
Hi,
As name suggest setter and getters are used to set and get value of particular property in a class. These are methods inside a class.
Setter (Set): Set some value in property of class
Getter (Get): Get value of a property in class.
It’s upto you to use these method as per requirement. If youdon’t declare these methods you won’t find any way to set or get value of a property in class.
Hope it help but i will write a separate article on Java Bean which will cover all these aspects.
Regards
hii..your tutorials are very clear and easy to understand…
Explain More about java.util.concurrent packages
your tutorials are very clear when compared to other tutorials available over internet.. Thank u very much for this effort.
Please give some information about Map Interface. I have one problem also… i want know about “null interface” is really there is concept like null interface.
Hi Ruchita,
This is first time i am hearing about Null Interface. I will check if there is anything like Null interface in Java or not. But as for as my knowledge is concerned there is nothing like it in Java.
I will try to add Map Interface related details soon if i get time.
Thanks
Hi Sir,
I got some information about null interface in Java, it is as follows:-
1.Null interface does not contain any abstract method and it has a special meaning for jvm.
2.Null interfaces act as markers..they just tell the compiler that the objects of this class need to be treated differently.
3.It gives a message to the java compiler that it can add some special behaviour towards the class which implementing it.
Eg’s of Marker Interfaces are:-
– java,lang.Cloneable
– java,io.Serializable
– java.util.EventListener
For Eg:- java.io.Serializable is a marker interface, it doesn’t contain any method declarations.
When a java class is to be serialized , i.e when we want to store the state of an object or pass through the network, we should intimate the java compiler in some way that there is a possibility of serializing this java class. For this, marker interfaces are used.
The java class which needs to be serialized has to implement the java.io.Serializable marker interface and by doing this we are informing the java compiler to add that capability.
Hi Ruchika,
I must admit. I almost forgot about this. Yes i do know about Marker Interface in Java. But i forgot that Marker Interface is also known as Null Interface. Thank you for reminding me. It will be useful for others too.
Also details provide by you for Marker Interface is quite god. We would love to have you as Writer on our website. 🙂 Do let me know in case you are interested. Feel free to share Articles on different subject with us.
Thanks
i need core java,j2EE full tutorial materials. can you send my email? PLEASE…..
Unfortunatly author is replying to girls message only.
I felt bad about it so repling to your message.
Check other sites please
Hi Sandeepna,
I don’t know why u have this feeling that i only reply to girl. 😛
May be coz on given article i have replied to a girls msg. Normally i reply on mail and don’t put those messages on blog. But this time i will definitely add this reply on blog. So that no one have this kind of feeling. If i can i always reply to all mails.
Regards
true
Not True 😛
hee…u pepole has 35 % reservation .that y he giving proirty for u …u have to more happy for that :p