Thursday, 17 April 2014

HashSet()

public HashSet()
Constructs a new, empty set. Internally HashSet uses a HashMap. The implementation looks like below.

public HashSet() {
     map = new HashMap<>();
}

the backing HashMap instance has default initial capacity (16) and load factor (0.75).

import java.util.*;

class HashSetConstructor1{
  public static void main(String args[]){
         HashSet<Integer> mySet = new HashSet<> ();
  
  /* Add Elements to HashSet */
  mySet.add(10);
  mySet.add(20);
  mySet.add(30);
  mySet.add(40);
  
  System.out.println("Elements in HashSet are " + mySet);
  }
}

Output
Elements in HashSet are [20, 40, 10, 30]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment