In This article we will learn about Collections Framework and how Collections are used in the real time projects.The Java util package contains one of the most powerful subsystem called Collections Framework.I had written about Java collection framework interview questions you people go through this link to know how the questions will be.
Collections Framework:
It defines the group of the classes and interfaces which can be used for representing a collection of objects in a single entity. A framework is set of classes that form a basis for building advanced functionality.The user of framework forms subclasses to extend functionality without having basic mechanism.
All the collection classes and interfaces available in java.util package.It provides 7 key interfaces for collections framework.
The key interfaces for the collection framework:
1) Collection:
This interface is the root interface for the entire collection framework.It can be used for representing a group of objects in a single entity.This interface defines most common general methods which can be applicable for any collection object.
Note: Collection is an interface available in java.util package for representing a group of objects in a single entity whereas Collections is an utility class available in java.util package and defines several utility methods for collection implemented class object.
2) List:
The List is an ordered collection.This can be used for representing a group individual objects in as a single entity where insertion order is preserved and duplicate objects allowed.
The fallowing classes implemented List interface directly.
ArrayList,LinkedList,Vector and Stack
The fallowing diagram shows about List interface and their classes:
Here Vector and Stack classes are Legacy classes and they are re-engineered in 1.2 version to fit into collections framework.
3) Set Interface :
A Set represents a group of elements arranged just like an array. The Set will grow dynamically when the elements are stored into it. A set will not allow duplicate elements.If we try to pass the same elements that is already available in the set,then it is not stored into the set.The insertion order is not preserved. This is the child interface of collections.
HashSet and LinkedHashSet are the classes which implement the Set interface directly.
4) SortedSet interface:
This can be used for representing group of individual and unique objects where all objects are inserted in some sorting order. It is the child interface of Set interface.
TreeSet is the implemented class of SortedSet interface.
The fallowing daigram shows the structure of Set interface:
5) Queue interface:
A Queue represents arrangement of elements in FIFO(First In First Out) order. This means that element that is stored as a first element into the queue will be removed first from the queue. It is child interface of collection. It has introduced in 1.5 version.The main use of this interface is prior to processing.
The fallowing diagram shows the structure of Queue interface:
If we want to represent a group of objects as key and value for then we can't use about interfaces. To handle this type of requirements sun people introduced as Map interface.
6) Map interface:
Maps store elements in the form of key and value pairs. Both key and value are objects only.If the key is provided then its corresponding value can be obtained.the keys should be unique values. we can store objects only but we can not store primitive data types in the collection objects.Since our main aim of collections is to handle objects only,not the primitive data types.
Map interface is not child interface of collections.
7) Sorted Map interface:
This can be used for representing group of objects as (key,value)pairs where all the entries are arranged some sorting order of keys
Tree Map is the which implements of SortedMap interface.
The fallowing diagram shows entire collection Framework Structure:
we had described collection framework interfaces and classes.In come lessons we will learn one by one interfaces and classes in depth with examples. Just stay here for updates.......
Collections Framework:
It defines the group of the classes and interfaces which can be used for representing a collection of objects in a single entity. A framework is set of classes that form a basis for building advanced functionality.The user of framework forms subclasses to extend functionality without having basic mechanism.
All the collection classes and interfaces available in java.util package.It provides 7 key interfaces for collections framework.
The key interfaces for the collection framework:
1) Collection:
This interface is the root interface for the entire collection framework.It can be used for representing a group of objects in a single entity.This interface defines most common general methods which can be applicable for any collection object.
Note: Collection is an interface available in java.util package for representing a group of objects in a single entity whereas Collections is an utility class available in java.util package and defines several utility methods for collection implemented class object.
2) List:
The List is an ordered collection.This can be used for representing a group individual objects in as a single entity where insertion order is preserved and duplicate objects allowed.
The fallowing classes implemented List interface directly.
ArrayList,LinkedList,Vector and Stack
The fallowing diagram shows about List interface and their classes:
Here Vector and Stack classes are Legacy classes and they are re-engineered in 1.2 version to fit into collections framework.
3) Set Interface :
A Set represents a group of elements arranged just like an array. The Set will grow dynamically when the elements are stored into it. A set will not allow duplicate elements.If we try to pass the same elements that is already available in the set,then it is not stored into the set.The insertion order is not preserved. This is the child interface of collections.
HashSet and LinkedHashSet are the classes which implement the Set interface directly.
4) SortedSet interface:
This can be used for representing group of individual and unique objects where all objects are inserted in some sorting order. It is the child interface of Set interface.
TreeSet is the implemented class of SortedSet interface.
The fallowing daigram shows the structure of Set interface:
5) Queue interface:
A Queue represents arrangement of elements in FIFO(First In First Out) order. This means that element that is stored as a first element into the queue will be removed first from the queue. It is child interface of collection. It has introduced in 1.5 version.The main use of this interface is prior to processing.
The fallowing diagram shows the structure of Queue interface:
If we want to represent a group of objects as key and value for then we can't use about interfaces. To handle this type of requirements sun people introduced as Map interface.
6) Map interface:
Maps store elements in the form of key and value pairs. Both key and value are objects only.If the key is provided then its corresponding value can be obtained.the keys should be unique values. we can store objects only but we can not store primitive data types in the collection objects.Since our main aim of collections is to handle objects only,not the primitive data types.
Map interface is not child interface of collections.
7) Sorted Map interface:
This can be used for representing group of objects as (key,value)pairs where all the entries are arranged some sorting order of keys
Tree Map is the which implements of SortedMap interface.
The fallowing diagram shows entire collection Framework Structure:
we had described collection framework interfaces and classes.In come lessons we will learn one by one interfaces and classes in depth with examples. Just stay here for updates.......
No comments:
Post a Comment