Java ArrayList to LinkedHashSet Conversion

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to convert an ArrayList to a LinkedHashSet in Java. LinkedHashSet is a class that utilizes the linked list data structure to implement Set in Java, making it useful when we want to convert an array-like list to a unique collection of data. LinkedHashSet is used to collect unique data.

Create an ArrayList

Create an ArrayList in Java and add a few elements to it using the add() method.

import java.util.ArrayList;

public class ArrayListToLinkedHashSet {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Apple");
        arrayList.add("Mango");
        arrayList.add("Orange");
        arrayList.add("Mango");
    }
}

Convert ArrayList to LinkedHashSet using stream API

You can use the Java 8 stream API to convert ArrayList to LinkedHashSet. The Collectors.toCollection() method collects the ArrayList elements into a LinkedHashSet to get unique elements.

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.stream.Collectors;

public class ArrayListToLinkedHashSet {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Apple");
        arrayList.add("Mango");
        arrayList.add("Orange");
        arrayList.add("Mango");

        //ArrayList to LinkedHashSet using stream API
        LinkedHashSet<String> linkedHashSet = arrayList.stream().collect(Collectors.toCollection(LinkedHashSet::new));
    }
}

Convert ArrayList to LinkedHashSet using the add() method

You can also convert an ArrayList to LinkedHashSet by using the add() method to add elements one by one and get a collection of unique elements.

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class ArrayListToLinkedHashSet {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Apple");
        arrayList.add("Mango");
        arrayList.add("Orange");
        arrayList.add("Mango");

        //ArrayList to LinkedHashSet using add() method
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
        for (String element : arrayList) {
            linkedHashSet.add(element);
        }
    }
}

Convert ArrayList to LinkedHashSet using the constructor

You can also get a LinkedHashSet from an ArrayList by passing it as an argument to the constructor of LinkedHashSet.

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class ArrayListToLinkedHashSet {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Apple");
        arrayList.add("Mango");
        arrayList.add("Orange");
        arrayList.add("Mango");

        //ArrayList to LinkedHashSet using constructor
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(arrayList);
    }
}

Print the ArrayList and LinkedHashSet

After converting the ArrayList to LinkedHashSet, let's print both of them to verify the unique elements.

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class ArrayListToLinkedHashSet {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Apple");
        arrayList.add("Mango");
        arrayList.add("Orange");
        arrayList.add("Mango");

        //ArrayList to LinkedHashSet using constructor
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(arrayList);

        //print ArrayList
        System.out.println("ArrayList: " + arrayList);

        //print LinkedHashSet
        System.out.println("LinkedHashSet: " + linkedHashSet);
    }
}

Compile and run the code

Open a terminal window and navigate to the directory where the code file is located. Compile the code using the javac command and run it using the java command.

javac ArrayListToLinkedHashSet.java
java ArrayListToLinkedHashSet

Summary

In this lab, we learned how to convert an ArrayList to a LinkedHashSet in Java using three different methods - stream API, add() method, and constructor. We also printed both the ArrayList and the LinkedHashSet to verify the unique elements.

Other Java Tutorials you may like