Java Collections – List, Set, Queue & Map

Java Collections – List, Set, Queue & Map

Introduction:

Most real-world applications work with collections like files, variables, records from files, or database result sets. The Java language provides a set of collection frameworks that you can use to create and manage various types of object collections. This blog describes the most common collection classes and how to start using them.

 

 

List:

The list is an ordered collection, also known as a sequence. Because the list is organized, you have complete control over where list items are placed in a list. One thing to note here is that the Java list collection can only contain objects.

 

Declare a List:

 

  • A generic way

 

 

 

List listOfStrings = new ArrayList();

 

 

  • Using the Diamond operator

 

 

 

List<String> listOfStrings = new ArrayList<>();

 

 

Here the object type is not specified during ArrayList instantiation. This is because the type of the class to the right of the expression must match the type on the left. Note that the ArrayList java object is assigned to a variable of the list type. In Java programming, you can assign one type of variable to another, as long as the assigned variable is a superclass or interface implemented by the assignment variable.

 

 

List Methods:

 

  • To place a list item on a list using add() method

 

 

 

List<Integer> listOfIntegers = new ArrayList<>(); listOfIntegers.add(Integer.valueOf(100));

 

 

Note that the add() method adds elements to the end of the list.

 

 

  • To ask how big the list is, call size(). Here in the above example to get the current list size we will call,

 

 

 

listOfIntegers.size();

 

 

  • To retrieve an item from the list call get() and pass it the index of the item you want. For example if you want to get the first item from the listOfIntergers, then you will specify it like this:

 

 

 

listOfIntegers.get(0);

 

 

To go through all the records in a list you will iterate through the collection. You can do that easily because list implements the java.lang.Iterable.
 
When java.lang.Iterable is implemented by a collection it is called an Iterative variable collection. Here you will begin at one end and work on the collection, item by item until you’ve finished processing all the items.To get each item in a list, you can do the following:

 

 

 

for (Integer i : listOfIntegers) {

  System.out.print(“Integer value is : ” + i);

}

 

 

Set:

Java Set is a collection construct that, by definition, contains unique elements — that is, no duplicates. The Java Set collection can only contain objects, and it is not an ordered list, which means it does not care about the order of the elements. Because the set is an interface, you cannot instantiate it directly.

 

Types of Java Set:

 

In Java, there are three implementations available for a set. They are HashSet, LinkedHashSet & TreeSet.

 

Declare a Set:

 

 

 

Set<Integer> setOfNumbers = new HashSet<>();

 

 
This Hashset is the most widely used version of a Set which gives a unique ordered list.

 

 

 

Set<String> setOfNames = new LinkedHashSet<>();

 

 

The only difference in LinkedHashSet with HasSet is that it orders the elements based on insertion order.

 

 

 

Set<Integer>setOfNumbers = new TreeSet<>();

 

 

In a TreeSet the ordering takes place based on the values of the inserted element. It follows a natural ordering by default.

 

 

 

Queue:

Java Queue is a collection that works on FIFO (First In First Out) principle. The elements that are added first will be removed first from the queue. LinkedList and Priority Queue are the most common types of Queue implementations in Java.

 

Basic Queue Operations
The common operations that can be performed in a queue are addition, deletion & iteration. Like other collections here also we can find out the queue size & length.

The enqueue() method will add an element at the back of the queue and the dequeue() method will remove the item which is at the front of a given queue.

 

 

 

 

Map:

The map is a convenient collection construct that you can use to associate one object (key) with another object (value). As you can imagine, the key of the Map must be unique and can be used later to retrieve values. Different implementations of the Map are HashMap, TreeMap, LinkedHashMap, etc. HashMap Java is the common Map type used by programmers.
 


Declare a Map:
A Map can be declared using the Diamond Operator as given below:

 

 

 

Map<Integer, String> sampleMap = new HashMap<>();

 

 

Here, the Integer will be the ‘Key’ and Sting will be the ‘Value’.

Basic Map Operations:
The basic operations that can be performed in a Map are:

  • Put the content in Map
  • Get content from Map
  • Get the key set for Map – use it to iterate.

     

     

     

    Common Operations in Java 8 Collections:

    The collection framework in Java has many common operations that apply to all types of collections. They are summarized here.

     

     

    End note:

    We have seen different types of collections in Java, their usage, and the main operations that each collection can perform. When dealing with large amounts of data, collections are the most commonly used data structure in the Java programming world. So it is important to understand their usage thoroughly to become a good Java developer. A Java developer will also need to understand Wrapper class, Streams, Enumeration, Autoboxing, and Threads.

     

     

    Get one step closer to your dream job!

     

     

    Prepare for your Java programming interview with Core Java Questions You’ll Most Likely Be Asked. This book has a whopping 290 technical interview questions on Java Collections: List, queues, stacks, and Declarations and access control in Java, Exceptional Handling, Wrapper class, Java Operators, Java 8 and Java 9, etc. and 77 behavioral interview questions crafted by industry experts and interviewers from various interview panels.

    We’ve updated our blogs to reflect the latest changes in Java technologies.
    This blog was previously uploaded on April 3rd, 2020, and has been updated on January 8th, 2022.