Introduction to Java Collections Framework

Collection framework in Java provides a set of readily usable efficient implementations for commonly used data structures and algorithms. 

A basic understanding of the data constructs like arrays, String, linked list, stack, queue, trees etc. and concepts like sorting, searching, hashing etc. from computer science will help you understand collections better. 

We can use and reuse these implementations of data structures and algorithms in a type safe manner, using it along with Generics. While Java collections is a framework and not a language feature, generics is a language feature introduced in Java 1.5.

Some collections allow duplicate elements and some collections do not allow. Some collections are ordered and some collections are unordered.

We can extend these collections and add our on implementation through inheritance; collections framework provide many interfaces and abstract classes for this purpose. 


Java Collections API Basics

Collections API mainly contains below three types of components:

  1. Abstractions (abstract classes and interfaces)

    • We can extend collections framework and add our on implementation through inheritance, from these abstractions.

    • Examples include Iterable, Collection, List, Set, SortedSet, NavigableSet, Queue, Deque, Map, SortedMap, NavigableMap, Iterator, ListIterator etc.

  2. Concrete classes

    • These are the actual container implementations (often based on abstractions) that we can actually use in our programs.

    • Examples include ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, PriorityQueue etc.

  3. Algorithms

    • These include commonly used functionality such as sorting, searching etc.

    • Algorithms are mostly part of the java.util.Collections util class.

    • Example methods in java.util.Collections interface that acts on various collections include binarySearch, copy, fill, max, min, replaceAll, reverse, repeat, shuffle, sort and swap.


Collections, Collection and collection

  • java.util.Collections class is an utility class with many methods that can be applied to various collections.

  • java.util.Collection interface is the parent interface for many collections such as List, Set, Queue etc.

  • The collection, with small c, denotes the collections framework (the concept).


Important additional points about collections API

  1. Iterable interface is a parent of Collection interface.

  2. Most collection interfaces such as List, Set and Queue are children of Collection interface, but Map interface is not.

  3. The Map interface is the parent interface in the map hierarchy. SortedMap interface is a child of Map interface and NavigableMap interface is a child of SortedMap interface.

  4. There are various implementations, sub interfaces, abstract implementations of List, Set, Queue and Map in the Java collections framework.

Short description of important collections

  • List interface

    • List is an ordered collection.

  • Set interface

    • Set is a collection that does not contain duplicates. 

  • Map interface

    • Map is a group of key value pairs.

  • Tree (data structure concept)

    • Tree based data structures are sorted and ordered.

    • There is no Tree interface or Tree class in Java, but there are Map and Set implementations such as TreeMap and TreeSet that uses the concept of Trees.

  • Queue interface

    • Queue processes its elements in the order "First In First Out" (e.g. queue in a movie theatre).

    • Queue is often compared against Stack data structure, and a Stack processes its elements in the order "Last in First Out". 

      • Stack is a class in Java unlike Queue which is an interface. Stack is implemented as a child of List in Java.

  • Deque interface

    • Deque stands for Double Ended Queue

    • Allow you to insert and remove elements from both the ends.

    • BlockingDeque interface is a child of Deque interface.

    • ArrayDeque, ConcurrentLinkedDeque, LinkedBlockingDeque and a LinkedList implement Deque interface.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)