LinkedHashSet toArray() in Java

toArray(): This method is available in java.util.LinkedHashSet class of Java.

Syntax:

Object[] java.util.HashSet.toArray()

This method returns an array containing all of the elements in this set.

Parameters: NA

Returns: an array containing all the elements in this set.

Exceptions: NA

Approach

Java

import java.util.Arrays;
import java.util.LinkedHashSet;

public class LinkedHashSettoArray {
    public static void main(String[] args) {

        LinkedHashSet<String> linkedHashSet =
new LinkedHashSet<>();

        linkedHashSet.add("Hello");
        linkedHashSet.add("Java");

        System.out.println(Arrays.toString(
linkedHashSet.toArray()));

    }
}

Output:

[Hello, Java]


No comments:

Post a Comment