A Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit.
The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root” interfaces of Java collection classes.
Need for Collection Framework :
Before Collection Framework (or before JDK 1.2) was introduced, the standard methods for grouping Java objects (or collections) were Arrays or Vectors or Hashtables. All of these collections had no common interface.
Accessing elements of these Data Structures was a hassle as each had a different method (and syntax) for accessing its members:
1 1 geeks
As we can see, none of these collections (Array, Vector or Hashtable) implement a standard member access interface. It was very difficult for programmers to write algorithms that can work for all kinds of Collections. Another drawback being that most of the ‘Vector’ methods are final, meaning we cannot extend the ’Vector’ class to implement a similar kind of Collection.
Java developers decided to come up with a common interface to deal with the above mentioned problems and introduced the Collection Framework in JDK 1.2.
Both legacy Vectors and Hashtables were modified to conform to the Collection Framework.
Advantages of Collection Framework:
- Consistent API : The API has a basic set of interfaces like Collection, Set, List, or Map. All classes (ArrayList, LinkedList, Vector, etc) that implement these interfaces have some common set of methods.
- Reduces programming effort: A programmer doesn’t have to worry about the design of Collection, and he can focus on its best use in his program.
- Increases program speed and quality: Increases performance by providing high-performance implementations of useful data structures and algorithms.
Hierarchy of Collection Framework
Collection Map / / \ \ | / / \ \ | Set List Queue Dequeue SortedMap / / SortedSet Core Interfaces in Collections Note that this diagram only shows core interfaces.
Collection : Root interface with basic methods like add(), remove(), contains(), isEmpty(), addAll(), ... etc. Set : Doesn't allow duplicates. Example implementations of Set interface are HashSet (Hashing based) and TreeSet (balanced BST based). Note that TreeSet implements SortedSet. List : Can contain duplicates and elements are ordered. Example implementations are LinkedList (linked list based) and ArrayList (dynamic array based) Queue : Typically order elements in FIFO order except exceptions like PriorityQueue. Deque : Elements can be inserted and removed at both ends. Allows both LIFO and FIFO. Map : Contains Key value pairs. Doesn't allow duplicates. Example implementation are HashMap and TreeMap. TreeMap implements SortedMap. The difference between Set and Map interface is that in Set we have only keys, whereas in Map, we have key, value pairs.
We will soon be discussing examples of interface implementations.
This article is contributed by Dharmesh Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Difference between Traditional Collections and Concurrent Collections in java
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.Collections.frequency() in Java with Examples
- Collections.nCopies() in Java
- Multidimensional Collections in Java
- Need of Concurrent Collections in java
- Java.util.Collections.frequency() in Java
- Collections.sort() in Java with Examples
- Collections.binarySearch() in Java with Examples
- Collections.reverse() in Java with Examples
- Collections.shuffle() in Java with Examples
- Java Lambda Expression with Collections
- Output of Java programs | Set 13 (Collections)
- Collections.reverseOrder() in Java with Examples