Knoldus Blog Audio
In this blog we will understand basics of JAVA Collections framework.
What are JAVA Collections and Why do we need them?
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.
Collections are like containers that group multiple items in a single unit. For example, a jar of chocolates, a list of names, etc. Collections are used in every programming language and when Java arrived, it also came with few. Let’s see how does it help us:
- Reduces programming effort
- Increases program speed and quality
- Allows interoperability among unrelated APIs
- Reduces effort to learn and to use new APIs
- Reduces effort to design new APIs
- Fosters software reuse
Collections Framework Hierarchy
We can say collection has in 4 basic flavors as below,
The List interface extends Collection to define an ordered collection with duplicates allowed. The List interface adds position-oriented operations, as well as a new list iterator that enables the user to traverse the list bi-directionally. ArrayList, LinkedList, and vector are classes implementing List interface.
The Set interface extends the Collection interface. It will make sure that an instance of Set contains no duplicate elements. The concrete class implements hashcode and equals methods to make sure uniqueness of objects. Three concrete classes of Set are HashSet, LinkedHashSet, and TreeSet.
A map is a container that stores the elements along with the keys. The keys are like indexes. In List, the indexes are integers. In Map, the keys can be any object. A map cannot contain duplicate keys. Each key maps to one value. A key and its corresponding value from an entry, which are actually stored in a map. HashMap, HashTable,TreeMap, and LinkedHashMap are classes implementing Map interface.
A queue is a first-in, first-out data structure. Elements are appended to the end of the queue and are removed from the beginning of the queue. In a priority queue, elements are assigned priorities. When accessing elements, the element with the highest priority is removed first.
Difference between Arrays & Collections in Java
The difference between arrays and collections are as follows:
1. Arrays are fixed in size but collections are growable in nature. We can increase or decrease size.
2. Arrays are not recommended to use with respect to memory whereas collections are recommended to use with respect to memory.
3. Arrays are recommended to use with respect to performance but collections are not recommended to use with respect to performance.
4. Arrays can store only homogeneous data elements (similar type of data) but collections can hold both homogeneous and heterogeneous elements.
5. Arrays do not support any method but collections support various kinds of methods.
6. Arrays can have both hold primitives and object types but collections can hold only objects but not primitive.
In next blog we will understand the collection framework by implementing them.
Checkout our other blogs on java Knoldus blogs.
Checkout our templates here.