Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the HashSet.toString() function in Java?

Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In this shot, we will learn what Java’s HashSet.toString() function is.

First, consider a HashSet of strings. The HashSet class has an inbuilt function, i.e., hashset.toString(). A string inside a square bracket format will be returned.

How to use the hashset.toString() method in Java

The steps to use the function are below:

  • Initialize a HashSet of strings.

  • Add some string elements to it.

  • Call the hashset.toString() function and print in the console.

  • You will see the results in the output section.

Function description

We are going to initiate an empty HashSet and add some data into it, then call the toString() function, in which we will not pass anything as a parameter. The toString() method first casts each element in HashSet to a string.

It does not matter that the HashSet is of String type. The function works on HashSets of Integer and Double type as well.

It then creates a comma-separated string out of it, encloses it in square brackets, and returns it. The result will be printed in the output window.

The String representation consists of a set representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets ([ ]). This method is used mainly to display collections other than of String type (Object or Integer, for instance) in a String representation.

Code

Let’s have a look at the code:

import java.util.*;
class Main{
public static void main(String args[])
{
// Creating an Empty HashSet
HashSet hashSet = new HashSet();
// Use add() method
hashSet.add("Hello");
hashSet.add(2.5);
hashSet.add("love");
hashSet.add("Coding");
// Using toString() method
System.out.println(hashSet.toString());
}
}
Use the hashset.toString() function in Java

Output

[love, Hello, 2.5, Coding]

Explanation

  • In line 1, we imported the java.util classes to include the HashSet data structure.

  • In line 7, we initiated an object: an empty HashSet of strings.

  • In line 9 to 11, we inserted some data to the HashSet.

  • In line 13, we called the hashSet.toString() method to render the HashSet into a string representation, as shown in the output.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring