Java LinkedHashSet toArray() Method

There are two LinkedHashSet.toArray() and LinkedHashSet.toArray(T[] a) overloaded methods in Java are used to convert a LinkedHashSet into an array. 

Table of Contents

  1. Introduction
  2. toArray() Method Syntax
  3. toArray(T[] a) Method Syntax
  4. Examples
    • Converting LinkedHashSet to an Array of Objects
    • Converting LinkedHashSet to an Array of Specified Type
  5. Conclusion

Introduction

The LinkedHashSet.toArray() and LinkedHashSet.toArray(T[] a) methods allow you to convert a LinkedHashSet to an array. The former returns an array of Object, while the latter returns an array of a specified type.

toArray()() Method Syntax

The syntax for the toArray() method is as follows:

public Object[] toArray()
  • The method does not take any parameters.
  • The method returns an array containing all the elements of the LinkedHashSet.

toArray(T[] a)() Method Syntax

The syntax for the toArray(T[] a) method is as follows:

public <T> T[] toArray(T[] a)
  • The method takes a single parameter a of type T[], which represents the array into which the elements of the LinkedHashSet are to be stored.
  • The method returns an array containing all the elements of the LinkedHashSet.

Examples

Converting LinkedHashSet to an Array of Objects

The toArray() method can be used to convert a LinkedHashSet to an array of Object.

Example

import java.util.LinkedHashSet;

public class ToArrayExample {
    public static void main(String[] args) {
        // Creating a LinkedHashSet of Strings
        LinkedHashSet<String> animals = new LinkedHashSet<>();

        // Adding elements to the LinkedHashSet
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Elephant");

        // Converting LinkedHashSet to an array of Objects
        Object[] animalArray = animals.toArray();

        // Printing the array
        System.out.print("Array from LinkedHashSet: ");
        for (Object animal : animalArray) {
            System.out.print(animal + " ");
        }
    }
}

Output:

Array from LinkedHashSet: Lion Tiger Elephant

Converting LinkedHashSet to an Array of Specified Type

The toArray(T[] a) method can be used to convert a LinkedHashSet to an array of a specified type.

Example

import java.util.LinkedHashSet;

public class ToArraySpecifiedTypeExample {
    public static void main(String[] args) {
        // Creating a LinkedHashSet of Strings
        LinkedHashSet<String> animals = new LinkedHashSet<>();

        // Adding elements to the LinkedHashSet
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Elephant");

        // Converting LinkedHashSet to an array of Strings
        String[] animalArray = animals.toArray(new String[0]);

        // Printing the array
        System.out.print("Array from LinkedHashSet: ");
        for (String animal : animalArray) {
            System.out.print(animal + " ");
        }
    }
}

Output:

Array from LinkedHashSet: Lion Tiger Elephant

Conclusion

The LinkedHashSet.toArray() and LinkedHashSet.toArray(T[] a) methods in Java provide a way to convert a LinkedHashSet into an array. By understanding how to use these methods, you can efficiently manage and manipulate collections in array form. These methods are useful for converting collections to arrays, allowing for more flexible data processing in your Java applications.

Comments