The toString() method of JavaAbstractClass is used to return a string representation of the elements of the Collection.
The String representation comprises a list 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:
Object.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:
import java.util.*;
public class collection {
public static void main(String args[])
{
AbstractCollection<String> abs
= new PriorityQueue<String>();
abs.add("Welcome");
abs.add("To");
abs.add("Geeks");
abs.add("For");
abs.add("Geeks");
System.out.println(abs.toString());
}
}
Output:
[For, Geeks, To, Welcome, Geeks]
Example 2:
import java.util.*;
public class collection {
public static void main(String args[])
{
AbstractCollection<Integer> abs
= new PriorityQueue<Integer>();
abs.add(10);
abs.add(20);
abs.add(30);
abs.add(40);
System.out.println(abs.toString());
}
}
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/AbstractCollection.html#toString–
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Dec, 2018
Like Article
Save Article