Java Collections

Collections in Java and Collection Framework

Collections in java is a framework. It is are one of the most commonly reusable data structures. Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet etc.

Java collections are one of the most commonly used data-structures by all Java professionals. But are you using the right collection class that would best suits your need. Most programmers usually use Vectors, ArrayList, HashMap or the Hashtable. There are many other collection classes available with the JDK that you can use instead of re-inventing logic to suite your needs.

We will be trying to understand the different types of classes and when each Collection class could be used. We wouldn’t be looking into the implementation details of any collection, for that please refer the latest Java Collection API docs.

The Core Collection Framework Interfaces
The core collection frameworks are depicted in the following image.

The main type of collections are:

  • Sets
  • Lists
  • Queues
  • Maps

Maps are not an integral part of the Collection framework, but they are still considered as Collection because of their capability to store and manipulate data as collection of objects.

Sorted Sets and Sorted Maps are basically a sorted version of Sets and Maps.
Hierarchy of Collection Framework
Let us see the hierarchy of collection framework.The java.util package contains all the classes and interfaces for Collection framework.

Goals for Java Collections:
The collections framework was designed to meet several goals.

  • The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hash tables) are highly efficient.
  • The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.
  • Extending and/or adapting a collection had to be easy.

Commonly used methods of Collection interface:
There are many methods declared in the Collection interface. They are as follows:

  1. public boolean add(object element): is used to insert an element in this collection.
  2. public boolean addAll(collection c): is used to insert the specified collection elements in the invoking collection.
  3. public boolean remove(object element): is used to delete an element from this collection.
  4. public boolean removeAll(Collection c): is used to delete all the elements of specified collection from the invoking collection.
  5. public boolean retainAll(Collection c): is used to delete all the elements of invoking collection except the specified collection.
  6. public int size(): return the total number of elements in the collection.
  7. public void clear(): removes the total no of element from the collection.
  8. public boolean contains(object element): is used to search an element.
  9. public boolean containsAll(collection c): is used to search the specified collection in this collection.
  10. public Iterator iterator(): returns an iterator.

Iterator interface
Iterator interface provides the facility of iterating the elements in forward direction only.
Methods of Iterator interface
There are only three methods in the Iterator interface. They are:

  1. public boolean hasNext() it returns true if iterator has more elements.
  2. public object next() it returns the element and moves the cursor pointer to the next element.
  3. public void remove() it removes the last elements returned by the iterator. It is rarely used.


References
http://www.janeve.me/articles/which-java-collection-to-use

 

<<Previous <<   || Index ||   >>Next >>

 

Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago