Difference between List and Set in Java

Difference between List and Set in Java

In Java, List and Set are two common types of collections that are part of the Java Collections Framework. They have distinct characteristics and use cases.

List and Set

List

1. Introduction

  • List is an interface in the java.util package.
  • It represents an ordered collection (also known as a sequence).

2. Characteristics

  • Order: Elements are ordered and can be accessed by their index.
  •  Duplicates: Allows duplicate elements.
  •  Null Elements: Can contain null elements.
  •  Common implementations: ArrayList, LinkedList, Vector, Stack.

3. Methods

  •  add(E e): Appends the specified element to the end of the list.
  •  get(int index): Returns the element at the specified position.
  •  remove(int index): Removes the element at the specified position.
  •  set(int index, E element): Replaces the element at the specified position with the specified element.
  •  size(): Returns the number of elements in the list.

Set

1. Introduction

  • Set is an interface in the java.util package.
  • It represents a collection that does not allow duplicate elements.

2. Characteristics

  • Order: Does not maintain any order of elements (although specific implementations like LinkedHashSet and TreeSet maintain order).
  • Duplicates: Does not allow duplicate elements.
  • Null Elements: Can contain at most one null element.
  • Common implementations: HashSet, LinkedHashSet, TreeSet.

3. Methods

  •  add(E e): Adds the specified element to the set if it is not already present.
  •  remove(Object o): Removes the specified element from the set if it is present.
  •  contains(Object o): Returns true if the set contains the specified element.    – size(): Returns the number of elements in the set.

Example
Here’s an example demonstrating the use of both List and Set:

java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ListVsSet {
    public static void main(String[] args) {
        // Create a List and add some elements
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        list.add("Date");
        list.add("Banana"); // Duplicate element

        // Display the List
        System.out.println("List elements:");
        for (String element : list) {
            System.out.println(element);
        }

        // Create a Set and add some elements
        Set<String> set = new HashSet<>();
        set.add("Apple");
        set.add("Banana");
        set.add("Cherry");
        set.add("Date");
        set.add("Banana"); // Duplicate element

        // Display the Set
        System.out.println("\nSet elements:");
        for (String element : set) {
            System.out.println(element);
        }

        // Demonstrating unique characteristics of List
        System.out.println("\nAccessing List element at index 1: " + list.get(1));

        // Demonstrating unique characteristics of Set
        System.out.println("Does the Set contain 'Cherry'? " + set.contains("Cherry"));
    }
}

Explanation List and Set

List

  •  An ArrayList is created, and elements are added to it, including a duplicate element (“Banana”).
  • The elements of the List are displayed, showing the order and the duplicate element.

Output:

    List elements

    Apple , Banana , Cherry Date Banana

   

Set:

  •  A HashSet is created, and elements are added to it, including a duplicate element (“Banana”).
  •  The elements of the Set are displayed, showing no duplicates and no particular order.

Output

    Set elements: Apple , Date, Cherry , Banana

Unique Characteristics

  • Accessing an element in the List by index: list.get(1) returns “Banana”.
  • Checking if the Set contains a specific element: set.contains(“Cherry”) returns true.

Homepage

Readmore