- Collections in Java
- What You Should Learn in Java Collections?
- What is a Framework?
- Need for a Separate Collection Framework in Java
- Java
- Advantages of the Collection Framework
- Hierarchy of the Collection Framework
- Methods of the Collection Interface
- Interfaces that extend the Collections Interface
- 1. Iterable Interface
- 2. Collection Interface
- 3. List Interface
- i). ArrayList
Collections in Java
Any group of individual objects which are represented as a single unit is known as a collection of objects. In Java, a separate framework named the “Collection Framework” has been defined in JDK 1.2 which holds all the collection classes and interface in it.
In Java, Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root” interfaces of Java collection classes.
What You Should Learn in Java Collections?
- List Interface
- Abstract List Class
- Abstract Sequential List Class
- Array List
- Vector Class
- Stack Class
- LinkedList Class
- Blocking Queue Interface
- AbstractQueue Class
- PriorityQueue Class
- PriorityBlockingQueue Class
- ConcurrentLinkedQueue Class
- ArrayBlockingQueue Class
- DelayQueue Class
- LinkedBlockingQueue Class
- LinkedTransferQueue
- BlockingDeque Interface
- ConcurrentLinkedDeque Class
- ArrayDeque Class
- Abstract Set Class
- CopyOnWriteArraySet Class
- EnumSet Class
- ConcurrentHashMap Class
- HashSet Class
- LinkedHashSet Class
- SortedSet Interface
- NavigableSet Interface
- TreeSet
- ConcurrentSkipListSet Class
- SortedMap Interface
- NavigableMap Interface
- ConcurrentMap Interface
- TreeMap Class
- AbstractMap Class
- ConcurrentHashMap Class
- EnumMap Class
- HashMap Class
- IdentityHashMap Class
- LinkedHashMap Class
- HashTable Class
- Properties Class
- How to convert HashMap to ArrayList
- Randomly select items from a List
- How to add all items from a collection to an ArrayList
- Conversion of Java Maps to List
- Array to ArrayList Conversion
- ArrayList to Array Conversion
- Differences between Array and ArrayList
What is a Framework?
A framework is a set of classes and interfaces which provide a ready-made architecture. In order to implement a new feature or a class, there is no need to define a framework. However, an optimal object-oriented design always includes a framework with a collection of classes such that all the classes perform the same kind of task.
Need for a Separate Collection Framework in Java
Before the 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. Therefore, though the main aim of all the collections is the same, the implementation of all these collections was defined independently and had no correlation among them. And also, it is very difficult for the users to remember all the different methods, syntax, and constructors present in every collection class.
Let’s understand this with an example of adding an element in a hashtable and a vector.Java
As we can observe, none of these collections(Array, Vector, or Hashtable) implements a standard member access interface, it was very difficult for programmers to write algorithms that can work for all kinds of Collections. Another drawback is that most of the ‘Vector’ methods are final, meaning we cannot extend the ’Vector’ class to implement a similar kind of Collection. Therefore, 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 post which both, legacy Vectors and Hashtables were modified to conform to the Collection Framework.
Advantages of the Collection Framework
Since the lack of a collection framework gave rise to the above set of disadvantages, the following are the advantages of the collection framework.
- Consistent API: The API has a basic set of interfaces like Collection, Set, List, or Map, all the 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 the Collection but rather he can focus on its best use in his program. Therefore, the basic concept of Object-oriented programming (i.e.) abstraction has been successfully implemented.
- Increases program speed and quality: Increases performance by providing high-performance implementations of useful data structures and algorithms because in this case, the programmer need not think of the best implementation of a specific data structure. He can simply use the best implementation to drastically boost the performance of his algorithm/program.
Hierarchy of the Collection Framework
The utility package, (java.util) contains all the classes and interfaces that are required by the collection framework. The collection framework contains an interface named an iterable interface which provides the iterator to iterate through all the collections. This interface is extended by the main collection interface which acts as a root for the collection framework. All the collections extend this collection interface thereby extending the properties of the iterator and the methods of this interface. The following figure illustrates the hierarchy of the collection framework.
Hierarchy of the Collection Framework in Java
Before understanding the different components in the above framework, let’s first understand a class and an interface.
- Class: A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.
- Interface: Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, nobody). Interfaces specify what a class must do and not how. It is the blueprint of the class.
Methods of the Collection Interface
This interface contains various methods which can be directly used by all the collections which implement this interface. They are:
Interfaces that extend the Collections Interface
The collection framework contains multiple interfaces where every interface is used to store a specific type of data. The following are the interfaces present in the framework.
1. Iterable Interface
This is the root interface for the entire collection framework. The collection interface extends the iterable interface. Therefore, inherently, all the interfaces and classes implement this interface. The main functionality of this interface is to provide an iterator for the collections. Therefore, this interface contains only one abstract method which is the iterator. It returns the
2. Collection Interface
This interface extends the iterable interface and is implemented by all the classes in the collection framework. This interface contains all the basic methods which every collection has like adding the data into the collection, removing the data, clearing the data, etc. All these methods are implemented in this interface because these methods are implemented by all the classes irrespective of their style of implementation. And also, having these methods in this interface ensures that the names of the methods are universal for all the collections. Therefore, in short, we can say that this interface builds a foundation on which the collection classes are implemented.
3. List Interface
This is a child interface of the collection interface. This interface is dedicated to the data of the list type in which we can store all the ordered collections of the objects. This also allows duplicate data to be present in it. This list interface is implemented by various classes like ArrayList, Vector, Stack, etc. Since all the subclasses implement the list, we can instantiate a list object with any of these classes.
For example:
List al = new ArrayList<> (); List ll = new LinkedList<> (); List v = new Vector<> (); Where T is the type of the object
The classes which implement the List interface are as follows:
i). ArrayList
ArrayList provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. The size of an ArrayList is increased automatically if the collection grows or shrinks if the objects are removed from the collection. Java ArrayList allows us to randomly access the list. ArrayList can not be used for primitive types, like int, char, etc. We will need a wrapper class for such cases.
Let’s understand the ArrayList with the following example: