Home » Java programming language

Differences between Set and List interface in Java

Java Set vs List: Here, we are going to learn what are the differences between Set and List interface in Java programming language?
Submitted by Preeti Jain, on August 04, 2019

Set vs List

Here, we will see the difference between Set and List interface in Java.

At very first we will see how Set interface differs from List interface in Java?

Set interface in Java

  • This interface is available in java.util package.
  • This interface is a child interface of Collection interface.
  • If we want to represent a group of individual objects where "duplicate objects or elements are not allowed"(i.e. we can't insert one object multiple times).
  • If we want to represent a group of individual objects where "insertion order is not preserved"(i.e. the order of insertion is not needed to be the same as the order of retrieval).
  • We should go for the Set interface where "Duplicate object are not allowed" (i.e. duplicacy is important) and "insertion order is not preserved" (i.e. the order of insertion and retrieval is not important).
  • The implementation class of Set interface is HashSet and LinkedHashSet.

Example:

Let suppose we have a Set with few elements. Here we are adding the elements in the order is [10, 20, 30, 50, null] and if we are retrieving the elements so the order of retrieving elements can be different (i.e. it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like [null, 50, 20, 10, 30].

// Java program to demonstrate the behavior of Set interface 

import java.util.*;

class SetInterface {
    public static void main(String[] args) {
        // Creating an instance
        Set set = new HashSet();

        // By using add() method to add an elements
        set.add(10);
        set.add(20);
        set.add(30);
        set.add(50);
        set.add(null);
        // set.add(20)* if we add again 20 then we will not get 
        // an error but duplicate element  will be ignored

        // Display Set elements
        System.out.println("Retrieval order of the elements in Set is :" + set);
    }
}

Output

E:\Programs>javac SetInterface.java

E:\Programs>java SetInterface
Retrieval order of the elements in Set is :[null, 50, 20, 10, 30]

List interface in Java

  • This interface is available in java.util package.
  • This interface is a child interface of Collection interface.
  • If we want to represent a group of individual objects where "duplicate objects or elements are allowed" (i.e. we can insert one object multiple times).
  • If we want to represent a group of individual objects where "insertion order is preserved" (i.e. the order of insertion is must be same as the order of retrieval).
  • We should go for List interface where "Duplicate object are allowed" (i.e. duplicacy is not important) and "insertion order is preserved" (i.e. the order of insertion and retrieval is important).
  • The implementation class of List interface is ArrayList and LinkedList, Vector, Stack, etc.

Example:

Let suppose we have a List with few elements. Here we are adding the elements in the order is [10, 20, 30, 50, null, 30] and if we are retrieving the elements so the order of retrieving elements must be the same (i.e. it is needed to be the same insertion and retrieval order of the elements.) so the output will be the same and the order will be like [10, 20, 30, null, 30].

// Java program to demonstrate the behavior of List interface

import java.util.*;

class ListInterface {
    public static void main(String[] args) {
        // Creating an instance
        List list = new ArrayList();

        // By using add() method to add an elements
        list.add(10);
        list.add(20);
        list.add(30);
        list.add(50);
        list.add(null);

        // if we add again 30 then we will not get an error 
        // because duplicate element  is allowed
        list.add(30);

        // Display List elements
        System.out.println("Retrieval order of the elements in List is :" + list);
    }
}

Output

E:\Programs>javac ListInterface.java

E:\Programs>java ListInterface
Retrieval order of the elements in List is :[10, 20, 30, 50, null, 30]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.