Java Collections
In this tutorial, we are going to discuss java collections in java. Before going further, let’s discuss arrays in java.
What is Array?
An array is an indexed collection of a fixed number of homogeneous data elements.
Limitations of Object Arrays
- Arrays are fixed in size, i.e., once we create an array, there is no chance of increasing or decreasing its size based on our requirement.
2. Arrays can hold only homogeneous data elements.
E.g
Student[] s = new Student[600];
s[0] = new Student; // Fine
s[1] = new Integer(10); //C.E
s[2] = "Ashok"; //C.E
We can resolve this problem by using object Arrays.
Object[] o = new Object[600];
o[0] = new Student;
o[1] = new Integer(10);
o[2] = "Ashok";
i.e., By using Object Arrays, we can hold heterogeneous data elements.
3. For the Arrays, there is no underlying Data Structure, i.e., for every requirement, we have to code explicitly, and there is no default ready-made support(like sorting, searching). We can’t represent array elements in some sorting order by default. We can’t prevent duplicate object insertion etc.
To resolve the above problems of arrays, Sun people have introduced ‘collections concept.’
Advantages of Collections over Arrays
- Collections are growable in nature, i.e., based on the requirement, we can increase or decrease the size.
- Collections can hold heterogeneous data elements also.
- Every collection class has been implemented based on some data structure
Collection
A group of individual objects as a single entity is called a collection.
Collection framework
It defines several classes and interfaces that can represent a group of objects as a single entity.
Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque, etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet, etc.).
What is framework in java
- Provides ready made architecture.
- Represents set of classes and interface.
Key Interfaces of collection Frame Work
1. Collection
- This interface is the root interface for the entire collection framework.
- This interface can be used for representing a group of objects as a single entity.
- This interface defines the most common general methods which can be applicable for any collection object.
- There is no concrete class that implements the collection interface directly.
Difference Between Collection and Collections
The collection is an interface available in java.util package for representing a group of objects as a single entity.
Collections is a utility class available in java.util package and defines several utility methods for collection implemented class object.
2. List interface
This can be used for representing a group of individual objects as a single entity where insertion order is preserved and duplicate objects allowed. This is the child interface of collection. The following classes implemented List interface directly.
ArrayList, LinkedList, Vector and Stack.
Vector and stack classes are re-engineered in the 1.2 version to fit into the collection framework. By means of Indexes, we can preserve insertion order, and we can differentiate duplicate objects.
3. Set interface
This can be used for representing a group of individual objects where duplicate objects are not allowed, and insertion operation is not preserved.
This is the child interface of collection HashSet and LinkedHashSet are the classes which implements Set interface directly.
4. SortedSet interface
This can be used for representing a group of individual and unique objects where all the objects are inserted in some sorting order. It is the child interface of the Set interface.
TreeSet is the implemented class of SortedSet.
5. NavigableSet
- It is the child interface of SortedSet, to provide several methods for navigation purpose.
- It is introduced in the 1.6 version
6. Queue
It is the child interface of collection. It has been introduced in the 1.5 version this interface can be used for representing a group of individual objects before processing.
All the above interfaces (collection, List, Set, SortedSet, Queue) can be used for representing a group of individual objects. If you want to represent a group of objects as key-value pairs, we can’t use the above interfaces. To handle this requirement, sun people have introduced a map interface.
7. Map
This can be used for representing a group of objects as key-value pairs. Both key and value are objects only.
StudentName -> StudentRollNo
phoneNumber -> contactdetails
word -> meaning
IP Address -> Domain-name
Map interface is not child interface of collection.
8. SortedMap
It is the child interface of Map. If we want to represent a group of objects as key-value pairs “according to some sorting order of keys,” then we should go for SortedMap. Here Sorting should be done only based on keys. But not based on values. TreeMap is the class that implements the SortedMap interface.
9. NavigableMap
It is the child interface of SortedMap and defines several methods for navigation purposes.
Collection Vs Collections
“Collection” is an “interface” that can be used to represent a group of objects as a single entity. Where as “Collections” is a utility class” present in java.util package to define several utility methods for Collection objects.
Collection --> interface
Collections --> class
In collection framework the following are legacy characters.
1. Enumeration(I)
2. Dictionary(AC)
3. Vector(C)
4. Stack(C)
5. Hashtable(C)
6. Properties(C)
Here I --> Interface, AC --> Abstract Class, C --> Class
That’s all about Java Collections in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!