HashSet toString() in Java

toString(): This method is available in java.util.HashSet class of Java.

Syntax:

String java.util.AbstractCollection.toString()

This method returns a string representation of this collection.

Parameters: NA

Returns: a string representation of this collection.

Exceptions: NA

Approach

Java

import java.util.HashSet;

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

        HashSet<String> hashSet = new HashSet<String>();

        hashSet.add("Hello");
        hashSet.add("Java");
        hashSet.add("C++");
        hashSet.add("Hello");

        System.out.println(hashSet.toString());
    }
}

Output:

[Java, C++, Hello]


No comments:

Post a Comment