Open In App
Related Articles

LinkedHashSet toString() method in Java with Example

Improve Article
Improve
Save Article
Save
Like Article
Like

The toString() method of Java LinkedHashSet is used to return a string representation of the elements of the Collection.

The String representation comprises 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 String type(for instance: Object, Integer)in a String Representation.

Syntax:

public String toString()

Parameter The method does not take any parameters.

Return This method returns a String representation of the collection.

Below examples illustrate the toString() method:

Example 1:




// Java program to demonstrate
// LinkedHashSet toString() method
  
import java.util.*;
  
public class collection {
    public static void main(String args[])
    {
        // Creating an Empty LinkedHashSet
        LinkedHashSet<String> abs
            = new LinkedHashSet<String>();
  
        // Use add() method
        // to add elements to the Collection
        abs.add("Welcome");
        abs.add("To");
        abs.add("Geeks");
        abs.add("For");
        abs.add("Geeks");
  
        // Using toString() method
        System.out.println(abs.toString());
    }
}


Output:

[Welcome, To, Geeks, For]

Example 2:




// Java program to demonstrate
// LinkedHashSet toString() method
  
import java.util.*;
  
public class collection {
    public static void main(String args[])
    {
        // Creating an Empty LinkedHashSet
        LinkedHashSet<Integer> abs
            = new LinkedHashSet<Integer>();
  
        // Use add() method
        // to add elements to the Collection
        abs.add(10);
        abs.add(20);
        abs.add(30);
        abs.add(40);
  
        // Using toString() method
        System.out.println(abs.toString());
    }
}


Output:

[10, 20, 30, 40]

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 24 Dec, 2018
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials