The toString() method of Java TreeSet 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 // TreeSet toString() method import java.util.*; public class collection { public static void main(String args[]) { // Creating an Empty TreeSet TreeSet<String> abs = new TreeSet<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()); } } |
[For, Geeks, To, Welcome]
Example 2:
// Java program to demonstrate // TreeSet toString() method import java.util.*; public class collection { public static void main(String args[]) { // Creating an Empty TreeSet TreeSet<Integer> abs = new TreeSet<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()); } } |
[10, 20, 30, 40]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- TreeSet pollLast() method in Java with Example
- TreeSet equals() method in Java with Example
- TreeSet toArray(T[]) method in Java with Example
- TreeSet toArray() method in Java with Example
- TreeSet retainAll() method in Java with Example
- TreeSet removeAll() method in Java with Example
- TreeSet hashCode() method in Java with Example
- TreeSet containsAll() method in Java with Example
- Vector toString() method in Java with Example
- LinkedHashSet toString() method in Java with Example
- HashSet toString() method in Java with Example
- Stack toString() method in Java with Example
- AbstractSet toString() method in Java with Example
- AbstractSequentialList toString() method in Java with Example
- DecimalStyle toString() method in Java with Example
- FieldPosition toString() method in Java with Example
- ParsePosition toString() method in Java with Example
- HijrahDate toString() method in Java with Example
- JapaneseDate toString() method in Java with Example
- MinguoDate toString() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

