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.

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

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.

Similar Reads

Program to Convert a LinkedHashSet into an Array in Java

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

Program to Convert a LinkedHashSet into a List in Java

...

Contact Us