Open In App

How to Convert a LinkedHashSet into an Array and List in Java?

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, LinkedHashSet is a predefined class of Java that extends the HashSet and it implements the Set interface. It can be used to implement the doubly linked list of the elements in the set, and it provides the predictable iteration order.

In this article, we will learn how to convert a LinkedHashSet into an Array and List in Java.

Step-by-Step Implementation to Convert a LinkedHashSet into an Array

  • Create the class named GfGLinkedHashSetToArray and write the main method into the class.
  • Create the one LinkedHashSet of the string instance which can store the string elements on it.
  • Add the elements on the LinkedHashSet using add() method.
  • Create the one-string array after that using toArray() method to convert the LinkedHashSet to an array.
  • Print the result conversion array.

Program to Convert a LinkedHashSet into an Array in Java

Below is the implementation of the Program to Convert a LinkedHashSet into an Array:

Java




// Java program to convert a LinkedHashSet into an Array
import java.util.LinkedHashSet;
import java.util.Arrays;
  
public class GfGLinkedHashSetToArray 
{
    public static void main(String[] args) 
    {
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("Java");
        linkedHashSet.add("Spring Boot");
        linkedHashSet.add("AWS");
  
        // convert LinkedHashSet to Array
        String[] array = linkedHashSet.toArray(new String[0]);
  
        // print the array
        System.out.println("Array: " + Arrays.toString(array));
    }
}


Output

Array: [Java, Spring Boot, AWS]

Explanation of the above Program:

The above program is the example program of the converting the LinkedHashSet into an array that can be implements the toArray pre-defined method.

  • add(): This is pre-defined method and it can be used to add the elements into the LinkedHashSet
  • toArray( ): This method is a pre-defined method that can be used to convert the another types into the array.

Step-by-Step Implementation to Convert a LinkedHashSet into a List

  • Create the class named GfGLinkedHashSetToList and write the main method into the class.
  • Create the one LinkedHashSet of the string instance which can stores the string elements on it.
  • Add the elements on the LinkedHashSet using add() method.
  • Create the one string list after that adding the LinkedHashSet into the list.
  • Print the result conversion list.

Program to Convert a LinkedHashSet into a List in Java

Below is the implementation of Program to Convert a LinkedHashSet into a List :

Java




// Java to convert a LinkedHashSet into a List
import java.util.LinkedHashSet;
import java.util.ArrayList;
import java.util.List;
  
public class GfGLinkedHashSetToList {
    public static void main(String[] args) {
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("C");
        linkedHashSet.add("C++");
        linkedHashSet.add("C#");
  
        // Convert LinkedHashSet to List
        List<String> list = new ArrayList<>(linkedHashSet);
  
        // Print the list
        System.out.println("List: " + list);
    }
}


Output

List: [C, C++, C#]



Explanation of the above Program:

  • The above program is the example program of the converting the LinkedHashSet into list that can be implements easily.
  • First create the instance of the LinkedHashSet after that adding the elements on the LinkedHashSet after that create the String instance of the list then add the LinkedHashSet into the list.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads