Advertisement
1) HashSet() |
---|
This constructor creates an empty HashSet of a particular object type. Example - |
HashSet<Integer> arrL= new HashSet<Integer>();
2) HashSet(Collection <? extends E>c) |
---|
This constructor example creates a HashSet initialized with the elements of Collection c. With a condition, that, Collection c holds similar type of objects as the type declared by the HashSet.
Example - |
HashSet<Integer> arrL1 = new HashSet<Integer>():
HashSet<Integer> arrL2 = new HashSet<Integer>(arrL1);
Methods | Description |
---|---|
boolean add(E e) | This method adds the specified element e to the end of the HashSet. |
boolean addAll(Collection<? extends E> c) | Adds the content of a Collection c to the end of the HashSet. |
int size() | Returns the total number of elements in the HashSet. |
void clear() | Removes all of the elements from the HashSet. |
boolean isEmpty() | Returns true is HashSet is empty, else false. |
contains(Object o) | Checks if HashSet contains a specific Object. |
boolean remove(Object o) | Removes the first occurrence of an Object from the HashSet, if it is present. |
Object[] toArray() | Returns an Object array containing all the elements of HashSet. |
Iterator |
Returns an Iterator to iterate over elements of the HashSet. |
Advertisement
import java.util.*;
class HashSet1
{
public static void main(String... ar)
{
HashSet<String> hash= new HashSet<String>();
hash.add("B");
hash.add("3");
hash.add("G");
hash.add("C");
hash.add("1");
System.out.println("HashSet after adding objects = " + hash);
System.out.println("Size of HashSet = "+ hash.size());
hash.remove("G");
System.out.println("After removing element G, HashSet = "+ hash);
//Creating new Hashset from existing Hashset
HashSet<String> hash2= new HashSet<String>(hash);
System.out.println("New HashSet created from an existing HashSet = " + hash2);
}
}
HashSet after adding objects = [1, B, 3, C, G]
Size of HashSet = 5
Afer removing G, HashSet contains = [1, B, 3, C]
New HashSet created from an existing HashSet = [1, B, 3, C]
import java.util.*;
class HashSet1
{
public static void main(String... ar)
{
//Creating the first HashSet
HashSet<Integer> Hash1 = new HashSet<Integer>();
Hash1.add(4);
Hash1.add(1);
Hash1.add(5);
Hash1.add(2);
Hash1.add(3);
System.out.println("Contens of first HashSet= " + Hash1);
System.out.println("Size of first HashSet = "+ Hash1.size());
//Creating the second HashSet
HashSet<Integer> Hash2= new HashSet<Integer>();
Hash2.add(200);
Hash2.add(300);
System.out.println("Contents of second HashSet : " + Hash2);
//Adding the contents of first HashSet to the end of second HashSet
Hash2.addAll(Hash1);
System.out.println("Contents of second HashSet after adding first HashSet to its end : " + Hash2);
System.out.println("Size of second HashSet = "+ Hash2.size());
}
}
Contens of first HashSet= [1, 2, 3, 4, 5]
Size of first HashSet = 5
Contents of second HashSet : [200, 300]
Contents of second HashSet after adding first HashSet to its end : [1, 2, 3, 4,5, 200, 300]
Size of second HashSet = 7
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement