Trail: Collections
This section describes the Java Collections Framework. Here, you will learn what collections are and how they can make your job easier and programs better. You’ll learn about the core elements interfaces, implementations, aggregate operations, and algorithms that comprise the Java Collections Framework.
Introduction tells you what collections are, and how they’ll make your job easier and your programs better. You’ll learn about the core elements that comprise the Collections Framework: interfaces, implementations and algorithms.
Interfaces describes the core collection interfaces, which are the heart and soul of the Java Collections Framework. You’ll learn general guidelines for effective use of these interfaces, including when to use which interface. You’ll also learn idioms for each interface that will help you get the most out of the interfaces.
Aggregate Operations iterate over collections on your behalf, which enable you to write more concise and efficient code that process elements stored in collections.
Implementations describes the JDK’s general-purpose collection implementations and tells you when to use which implementation. You’ll also learn about the wrapper implementations, which add functionality to general-purpose implementations.
Algorithms describes the polymorphic algorithms provided by the JDK to operate on collections. With any luck you’ll never have to write your own sort routine again!
Custom Implementations tells you why you might want to write your own collection implementation (instead of using one of the general-purpose implementations provided by the JDK), and how you’d go about it. It’s easy with the JDK’s abstract collection implementations!
Interoperability tells you how the Collections Framework interoperates with older APIs that predate the addition of Collections to Java. Also, it tells you how to design new APIs so that they’ll interoperate seamlessly with other new APIs.
Collection Interface in Java with Examples
The Collection interface is a member of the Java Collections Framework. It is a part of java.util package. It is one of the root interfaces of the Collection Hierarchy. The Collection interface is not directly implemented by any class. However, it is implemented indirectly via its subtypes or subinterfaces like List, Queue, and Set.
For Example, the HashSet class implements the Set interface which is a subinterface of the Collection interface. If a collection implementation doesn’t implement a particular operation, it should define the corresponding method to throw UnsupportedOperationException.
The Hierarchy of Collection
It implements the Iterable interface. The sub-interfaces of Collection are BeanContext, BeanContextServices, BlockingDeque , BlockingQueue , Deque , EventSet, List , NavigableSet , Queue , Set , SortedSet , TransferQueue .
SubInterfaces of Collection Interface
All the Classes of the Collection Framework implement the subInterfaces of the Collection Interface. All the methods of Collection interfaces are also contained in it’s subinterfaces. These subInterfaces are sometimes called as Collection Types or SubTypes of Collection. These include the following:
List: 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 collection 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
Set: A set is an unordered collection of objects in which duplicate values cannot be stored. This collection is used when we wish to avoid the duplication of the objects and wish to store only the unique objects. This set interface is implemented by various classes like HashSet, TreeSet, LinkedHashSet, etc. Since all the subclasses implement the set, we can instantiate a set object with any of these classes. For example,
Set hs = new HashSet<> ();
Set lhs = new LinkedHashSet<> ();
Set ts = new TreeSet<> ();
Where T is the type of the object.
SortedSet: This interface is very similar to the set interface. The only difference is that this interface has extra methods that maintain the ordering of the elements. The sorted set interface extends the set interface and is used to handle the data which needs to be sorted. The class which implements this interface is TreeSet. Since this class implements the SortedSet, we can instantiate a SortedSet object with this class. For example,
SortedSet ts = new TreeSet<> ();
Where T is the type of the object.
Queue: As the name suggests, a queue interface maintains the FIFO(First In First Out) order similar to a real-world queue line. This interface is dedicated to storing all the elements where the order of the elements matter. For example, whenever we try to book a ticket, the tickets are sold at the first come first serve basis. Therefore, the person whose request arrives first into the queue gets the ticket. There are various classes like PriorityQueue, Deque, ArrayDeque, etc. Since all these subclasses implement the queue, we can instantiate a queue object with any of these classes. For example,
Queue pq = new PriorityQueue<> ();
Queue ad = new ArrayDeque<> ();
Where T is the type of the object.
Deque: This is a very slight variation of the queue data structure. Deque, also known as a double-ended queue, is a data structure where we can add and remove the elements from both the ends of the queue. This interface extends the queue interface. The class which implements this interface is ArrayDeque. Since this class implements the deque, we can instantiate a deque object with this class. For example,
Deque ad = new ArrayDeque<> ();
Where T is the type of the object.
Declaration:
public interface Collection extends Iterable
Here, E is the type of elements stored in the collection.
How to Learn Java Collections – A Complete Guide
In the real world, a collection by definition is a group of articles that have similar properties and attributes. Since Java is an Object-Oriented Language it mimics the real world. In Java, a Collection is a group of multiple objects put together into a single unit. Java Collections is a very vast topic and as a beginner can be difficult to navigate your way while learning it. Here we have everything you need to know while starting off with Java Collections.
What is a Collection Framework?
We have our collection of objects, now we need an organized way to use these collections, therefore we need a framework. The Java Collection Framework, first introduced in JDK 1.2 ( Java Development Kit 1.2 ), is an architecture made up of interfaces and classes. In simple words, it is like a skeletal structure for components that is ready to use for various programming needs. It also offers different data operations like searching, sorting, insertion, deletion, and manipulation. All of the classes and interfaces of the collection framework are bundled into the java.util package.
The Collection Framework Hierarchy
Class Vs Interface
Now that we have the basic concepts of what Java collections are made of we will understand each of its components in detail. What are their properties and a few examples of the most used collections?
1. Iterable
The Iterable interface is the root of the entire collection hierarchy, which means that every class and interface implements it. The primary function of an iterator is to allow the user to traverse through all of the collection class objects as if they were simple sequences of data items.
2. Collection
The Collection interface extends the Iterable interface. It has the basic methods required for using all the other collections in the framework to add, delete, and manipulate data. Since it is an interface it only has a method signature ( i.e. methodName ( ArgumentList ); ) and no definition because every interface or class that implements this interface will have different types of elements to handle. But since they implement this interface there is uniformity and structure to the rest of the collection. The methods of the collection interface are given below, all the interfaces and classes that extend or implement the Collection interface use these methods along with their own added methods specific to them.
3. List
The List interface extends from the Collection interface. The elements in a list are ordered like a sequence. The user can use the index number to access a particular element in the list, that is to say, the user has complete control over which element is inserted wherein the list.
3. a) ArrayList
The ArrayList class implements the List interface. The objects of this class are dynamic arrays. The ArrayList is essentially a resizable implementation of List. It implements all of the List methods and allows all elements even null elements. The ArrayList objects have a capacity, which is initially equal to the size but increases dynamically as new elements are added. An ArrayList is unsynchronised, which means multiple threads can access them at the same time. A thread is a unit of sequential flow control that can be processed in the Operating System.
ArrayList arrayListName = new ArrayList();
Example: Now we will take an example and perform some basic operations on an ArrayList. Here we instantiate an ArrayList named intArr. We use the add() method to add integers to intArr. The Integer class used in declaring intArr is a wrapper class for that basic datatype int. Wrapper classes extend from the Object class and they are used so that basic datatypes are compatible with other classes. Next, we print the ArrayList on the console. We use the remove() method to remove elements from the specified indices. We check if an element, 25 here, exists in intArr and print the appropriate message. Then we retrieve the element at index 1 using the get() method. As you can observe when an element is removed using remove() method the rest of the elements shift in sequence.