Last Updated : 24 Dec, 2018
The
toString()method of
Java AbstractSetis 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.
ReturnThis method returns a
String representationof the collection. Below examples illustrate the toString() method:
Example 1: Java
// Java program to demonstrate
// Abstract Collection toString() method
import java.util.*;
public class collection {
public static void main(String args[])
{
// Creating an Empty AbstractSet
AbstractSet<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());
}
}
Output:
[For, Geeks, To, Welcome]Example 2: Java
// Java program to demonstrate
// Abstract Collection toString() method
import java.util.*;
public class collection {
public static void main(String args[])
{
// Creating an Empty AbstractSet
AbstractSet<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());
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4