Java CollectionsImagine that you need to maintain a list of objects, sort them, perform search over thousands of object. What would you do, one way is that you can write your own algorithm to perform those operation on your own. But today, it is almost too awkward to think this solution.
Java Collections Framework provides java developers a way to store and manipulate collection of objects. Java introduced the Collection API as part of Java Development Kit 1.2 release and continue expending.
Collections API provides set of Classes and Interfaces that made developer’s task easy to handle collections of objects. You can think of Collection as bit like array with slight difference that it can grow it’s size as required and some other features as well.
You don’t need to write your own logic and you don’t need to worry about the performance part as well. As Java Collections API provides you ready made classes and interfaces to do the work for you. It has List, Map, Set, Queue and much more to do almost every thing.
Java Collection framework is in java.util package of Java Development Kit.
What we can do with Collection
- Add number of objects to the collection.
- Retrieve particular object from the list of objects.
- Remove object from the collection.
- Search for a object in the collection.
- Iterate the collection.
- Sort the collection as per requirement.
- Order the collection.
What are some important interfaces of Java Collection APIThere are number of interfaces available as part of java collection API but most important are below which you need to understand as your first step. We will discuss each one of them later in our tutorials.
|Collection||The root interface for all of the collection which represents the group of objects.|
|List||Used to store list of objects. Also contains duplicate elements.|
|Set||Collection API which is used to store unique elements only.|
|SortedSet||Used to sort the elements of Set interface.|
|Map||Contains elements as key value pair.|
|SortedMap||Used to sort the elements of Map interface.|
|Queue||Used to maintain queue of objects in java.|
|NavigableSet||Used to navigate forward and backward in Set collection framework.|
|NavigableMap||Used to navigate forward and backward in Map collection framework.|
What are some useful Collection ClassesThere are number of Java Collection classes used to manipulate the group of objects but most important are below classes.
|Data Structure||Implementation Classes|
|List||ArrayList, LinkedList, Vector, CopyOnWriteArrayList|
|Set||HashSet, LinkedHashSet, TreeSet, ConcurrentSkipListSet, CopyOnWriteArraySet|
|Map||HashMap, LinkedHashMap, Hashtable, TreeMap, ConcurrentHashMap, ConcurrentSkipListMap,|
|Queue||PriorityQueue, ArrayBlockingQueue, ConcurrentLinkedQueue,
DelayQueue, LinkedBlockingQueue, LinkedTransferQueue, PriorityBlockingQueue,
|DQue||DelayQueue, ConcurrentLinkedDeque, LinkedBlockingDeque|
Java Collection Overview
Let’s see the Java Collection Hierarchy which helps to see which class is inheriting which interface. In order to use Collection API properly, one need to understand the interface and classes hierarchy.
Java has divided this hierarchy into two parts one is “Collection” hierarchy and other one is “Map”s hierarchy.
Java Map Hierarchy