Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaDifference between List, Set and Map in Java

Difference between List, Set and Map in Java

List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows positional access and insertion of elements. It found in the java.util package. Let’s consider an example for a better understanding where you will see how you can add elements by using a list interface in java. 

Example:

Java




// Java Program to illustrate the
// addition of elements in a List
 
import java.util.*;
public class GFG {
 
    public static void main(String args[])
    {
 
        // Creating a List
        List<String> al = new ArrayList<>();
 
        // Adding elements in the List
        al.add("mango");
        al.add("orange");
        al.add("Grapes");
 
        // Iterating the List
        // element using for-each loop
        for (String fruit : al)
            System.out.println(fruit);
    }
}


Output :

mango
orange
Grapes

The Set follows the unordered way and it found in java.util package and extends the collection interface in java. Duplicate item will be ignored in Set and it will not print in the final output. Let’s consider an example for a better understanding where you will see how you can add elements by using a set interface in java. Let’s have a look.

Example:

Java




// A Java program to demonstrate a Set.
// Here, you will see how you can add
// Elements using Set.
 
import java.util.*;
 
public class SetExample {
   
    public static void main(String[] args)
    {
        // Set demonstration using HashSet
        Set<String> Set = new HashSet<String>();
         
        // Adding Elements 
        Set.add("one");
        Set.add("two");
        Set.add("three");
        Set.add("four");
        Set.add("five");
         
        // Set follows unordered way.
        System.out.println(Set);
    }
}


Output :

[four, one, two, three, five]

The Java Map interface, java.util.Map represents a mapping between a key and a value. More specifically, a Java Map can store pairs of keys and values. Each key is linked to a specific value. Once stored in a Map, you can later look up the value using just the key. Let’s consider an example for a better understanding where you will see how you can add elements by using the Map interface in java. Let’s have a look.

Example:

Java




// A sample program to demonstrate Map.
 
// Here, you will see how you
// can add elements using Map
import java.util.*;
 
class MapExample {
 
    public static void main(String args[])
    {
 
        // Creating object for Map.
        Map<Integer, String> map
            = new HashMap<Integer, String>();
 
        // Adding Elements using Map.
        map.put(100, "Amit");
        map.put(101, "Vijay");
        map.put(102, "Rahul");
 
        // Elements can traverse in any order
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + " "
                               + m.getValue());
        }
    }
}


Output :

100 Amit
101 Vijay
102 Rahul

Difference between List, Set, and Map in Java

List

Set

Map

The list interface allows duplicate elements

Set does not allow duplicate elements.

The map does not allow duplicate elements

The list maintains insertion order.

Set do not maintain any insertion order. 

The map also does not maintain any insertion order. 

We can add any number of null values.

But in set almost only one null value.

The map allows a single null key at most and any number of null values.

List implementation classes are Array List, LinkedList.

Set implementation classes are HashSet, LinkedHashSet, and TreeSet

Map implementation classes are HashMap, HashTable, TreeMap, ConcurrentHashMap, and LinkedHashMap.

The list provides get() method to get the element at a specified index.

Set does not provide get method to get the elements at a specified index

The map does not  provide get method to get the elements at a specified index

If you need to access the elements frequently by using the index then we can use the list

If you want to create a collection of unique elements then we can use set

If you want to store the data in the form of key/value pair then we can use the map.

To traverse the list elements by using Listlterator.

Iterator can be used traverse the set elements

Through keyset, value, and entry set.

RELATED ARTICLES

Most Popular

Recent Comments