07 March, 2015

Know About Collection Framework




Hello Friends,

We are using many collection frameworks any daily routine works bit do we really know the collection fundamentals and how it works. Here I try to put this Information with basic detail.

Collections actually groups multiple elements into single unit which allows us to add,retrieve, update or delete set of objects.
It's actually using various data structures in back end and providing us API to play with this data structures like trees,lists,sets and maps.

Why we should have to use collection framework ?

Collection is resizable.
Provides us built in efficient API which reduces our manual coding efforts.
Provides very useful data structures and provides high performance implementation of useful data structures and algorithams.
Very easy to learn and API utilization.

enter image description here

What to use when ?

1) If you need to access elements frequently by using index, than List is a way to go. Its implementation e.g. ArrayList provides faster access if you know index.

2) If you want to create collection of unique elements and don't want any duplicate than choose any Set implementation e.g. HashSetLinkedHashSet or TreeSet. All Set implementation follow there general contract e.g. uniqueness but also add addition feature e.g. TreeSet is a SortedSet and elements stored on TreeSet can be sorted by using Comparator or Comparable in Java. LinkedHashSet also maintains insertion order.

3) If you store data in form of key and value than Map is the way to go. You can choose from HashtableHashMapTreeMap based upon your subsequent need. 
 

Hash Map
Hash Set
HashMap  is a implementation of Map interface
HashSet is an implementation of Set Interface
HashMap Stores data in form of  key value pair
HashSet Store only objects
Put method is used to add element in map
Add method is used to add element is Set
In hash map hashcode value is calculated using key object
Here member object is used for calculating hashcode value which can be same for two objects so equal () method is used to check for equality if it returns false that means two objects are different.
HashMap is faster than hashset because unique key is used to access object
HashSet is slower than Hashmap


Map vs List vs Set Java

Popular Posts

Featured Post

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...