Java Collections framework
Java Collections is a framework in java, which help to hold a group of objects together. A group of Objects that belongs to the same type or a different set of types. A framework is nothing but a certain classes and interfaces(interfaces may contain or may not contain) helpful to provide a solution to the common or repeated problems.
Java Collections framework not only group the Objects and provides the flexibility to manipulate all the objects in the group or any object among the group. This manipulation and processing can be done by the Programmer choice via wide API's of the Collection Framework.
Before introducing or adding the Java Collections framework, in Java there were few classes which helps to group the object and can perform up to certain actions but were not that much flexible to use because of no common interface among these or no common API's.
- Array(Which is not a class)
Java Collection framework consists of three types of collection interfaces, all these interfaces extends the Collection interface we may say Collection interface is root for following interfaces.
List is an interface, whose concrete implementations are ArrayList, LinkedList and Vector. We have covered about these classes in later chapters.
Behavior of the List
- The elements/Objects inserted into the list holds the sequence manner.
- And, it allows to add elements to certain position based on index.
- When the elements added without index or position, will append that element to end of the list.
- When elements added at certain position, if any elements present from that position will be shifted to right side
Set is an interface in the Java Collection framework, like the List interface this Set interface also extends the Collection interface. Set interface is implemented the following concrete classes.
We have covered the Concrete classes of Set interface in the later chapters. The following is the behavior of the Set interface or in other words the classes which are implementing the Set interface should possess the following behavior.
Behavior of the Set
- Set will not allow the duplicate elements or Objects inserted into it.
- Duplication check of the Object will be performed by the equals and hashCode methods of adding Object.
- It will not allow the null object to be added into the Set
- Set will be useful for eliminating the duplicate elements in the group
- Set will hold the elements or objects in unordered list
Queue is the interface in the Collection Framework, like Set and List interfaces Queue also extends the Collection interface. There are no direct classes which are implemented the Queue interface but there are classes implemented the child interfaces of the Queue interface.
Behavior of Queue
- Queue follows the FIFO order (First inserted element will be the first came out element)
- Queue will have the limitation for adding the elements into the queue, if the queue is full then it will not allow any more elements into the queue.
Map is the only interface in the Java Collection Framework which will not extend the Collection interface. Map holds the Objects or elements in the key value pair. While fetching or processing the objects done by via key of that respective object stored or grouped in the Map.
Map mapObject = new HashMap();
mapObject.put(1, "Value 1");
mapObject.put(2, "Value 2");
If we see above lines of code where 1 and 2 will be the Keys, "Value 1" and "Value 2" will be the values of Key 1 and 2 respectively.
Behavior of the Map
- Map will not allow the duplicate keys
- Duplication check will be performed by the equals and hashCode methods of Key class.
- At max we can insert only one value per unique key.
- Order of the elements will be depending upon the implementation class of the Map interface.