Saturday, 19 April 2014

HashSet : toString : Return String Representation Of the HashSet

public String toString()
Returns a string representation of this collection. It prints the elements in the order returned by the iterator.

import java.util.*;

class HashSetToString{
 public static void main(String args[]){
  HashSet<Integer> mySet = new HashSet<> ();
  
  for(int i=0; i < 5; i++)
   mySet.add(i);
   
  System.out.println("Elements in mySet are");
  System.out.println(mySet);
 } 
}

Output
Elements in mySet are
[0, 1, 2, 3, 4]

By default System.out.println method calls the toString method of particular Object.






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment