Program to use HashSet in java



The following constructors are defined:

HashSet( ) ----constructs a default hash set
HashSet(Collection<? extends E>c)---initializes the hash set by using the elements of c.
HashSet(intcapacity) --initializes the capacity of the hash set to capacity
HashSet(intcapacity, float fillRatio)---initializes both the capacity and the fill ratio  The fill ratio must be between 0.0 and 1.0thenumber of elements is greater than the capacity of the hash set multiplied by its fill ratio,the hash set is expanded

the elements of hash set are not stored in sorted order
// Comment
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
packagecom.allinalljava;
importjava.util.*;
/**
 *
 * @author allinalljava
 */
public class JavaHashSet {
    // Demonstrate HashSet.


public static void main(String args[]) {
// Create a hash set.
HashSethashSet = new HashSet();
// Add elements to the hash set.
hashSet.add("B");
hashSet.add("A");
hashSet.add("D");
hashSet.add("E");
hashSet.add("C");
hashSet.add("F");
System.out.println("OutPut of HashSet Elements "+hashSet);
}
}


Output 
OutPut of HashSet Elements [D, E, F, A, B, C]
raja P Web Developer

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...