Java Collection Framework and Gernerics Part-I - JavaTechWorld

Sunday, 12 February 2017

Java Collection Framework and Gernerics Part-I

Java Collection- Collection is a java object which is used to group homogeneous and heterogeneous, duplicate and unique objects without size limitation carrying multiple object at a time from one application to another application. java.util package contains several classes to group homogeneous and heterogeneous object. These classes are usually called collection framework classes.

Why collection classes are given when we have Object[] to group heterogeneous object ?- All array object has two problems

  • It allows us to store only same type of elements.
  • it is fixed in size. 
The first problem can be solved using java.lang Object[] array we can collect all types of objects. But the second problem can't be solved automatically.

why the name collection framework?- Framework is a semi finished reusable application which provides some common low level services for solving reoccurring problems and that can be customized according to our requirement.

Example- Computer is a framework, it can be used by all people according to their requirement.

Types of collection classes-  There are 2 types of collection classes.
  • To collect objects in array format.
  • To collect object in (key,value) pair format.
Legacy collection framework classes- In java 1.0 version SUN introduced 2 classes.
  • Vector- It stores object in array format.
  • Hastable- It stores object in (key,value) pair format.
Drawback of Vector and Hashtable classes- These two classed were created as thread safe classes, means all the methods in these two classes are declared as synchronized. Hence for every method call on these two objects either for adding or removing elements. JVM locks and unlock these two objects. So in Single thread model application t leads to performance issue. In Single thread model application execution is sequential so there is no data corruption, hence no need locking. To solve this performance issue problem in Java 1.2 SUN introduced non-thread safe classes as a alternative to above two classes.

Collection Hierarchy- Collection hierarchy classes are divided into three categories - Set, List and Queue.

Set is collection which doesn't allow duplicate values.
List is a duplicate collection. List is super interface for all duplicate collection classes
SortedSet is a sub interface of set that store elements in sorted order either in ascending or descending order.
NavigableSet is a sub interface of SortedSet. it is added in java 1.6 to add more navigation methods to sorted set.
Queue is root interface of all types of queues. queues provide additional insertion, extraction and inspection operations.

Collection Interface classes hierarchy- In the below hierarchy, Vector and Stack classes are available since Java 1.0, LinkedHashSet class is available since Java 1.4, Queue is available since Java 1.5, and NavigableSet is available sing Java1.6, and all other remaining classes and interface are available sing Java 1.2 version.

Collection Framework

Map Hierarchy- Map hierarchy classes are used to collect elements in (Key,Value) pair format. In a map, keys should be unique and value can be duplicated. Each (key,value) is called an entry. In map by default entries are not sorted.

Sorted Map is the sub interface of Map. It sorts entries based on keys natural sorting order.

© 2014 JavaTechWorld. Designed by Bloggertheme9
Powered by Blogger.