Nov 11

HashSet- isEmpty() example

HashSet  has isEmpty() method which return true if there are no elements in the set otherwise returns false.

Signature :  boolean isEmpty()

 

Example to demo if HashSet has elements or not :

package javaexamples;

 

import java.util.HashSet;

import java.util.Set;

 

public class HashSetIsEmpty {

 

public static void main(String[] args) {

 

Set<Integer> integerHashSet = new HashSet<Integer>();

integerHashSet.add(1);

integerHashSet.add(2);

System.out.println(integerHashSet.isEmpty());

 

}

 

}

 

Output

false

 

 

 

 

Nov 11

Removing All elements from collection

8)      Removing All elements from collection:

Example to demo removing all elements from HashSet

Clear() method in HashSet removes all the elements from hashset.

 

package javaexamples;

 

import java.util.HashSet;

import java.util.Set;

 

public class HashSetClear {

public static void main(String[] args) {

Set<Integer> integerHashSet = new HashSet<Integer>();

integerHashSet.add(1);

integerHashSet.add(2);

integerHashSet.add(3);

System.out.println(“Before Clearing HashSet Size ”

+ integerHashSet.size());

integerHashSet.clear();

System.out.println(“After clearing HashSet Size ”

+ integerHashSet.size());

 

}

 

}

 

Output

Before Clearing HashSet Size 3

After clearing HashSet Size 0

 

 

 

Nov 11

How to remove element from hashset?

7)  Removing specified element from HashSet

 HashSet contains remove method which is used to remove specified element from HashSet. If the specified element is found in the set it removes otherwise it ust ignores.

Signature : boolean remove(Object o)

Example to demo removing element from HashSet :

 

package javaexamples;

 

import java.util.HashSet;

import java.util.Set;

 

public class HashSetRemove {

 

public static void main(String[] args) {

Set<String> stringsSet = new HashSet<String>();

stringsSet.add(“firsthashset”);

stringsSet.add(“secondhashset”);

System.out.println(“Printing HashSet Elements Before removing”);

for (String element : stringsSet) {

System.out.println(element);

}

stringsSet.remove(“firsthashse”);

System.out.println(“Printing HashSet Elements After removing”);

for (String element : stringsSet) {

System.out.println(element);

}

 

}

 

}

 

OutPut:

Printing HashSet Elements Before removing

secondhashset

firsthashset

Printing HashSet Elements After removing

secondhashset

 

 

 

 

 

 

Nov 11

HashSet-addAll() Adding collection to HashSet

7)    Example to demo addding collection to HashSet

 

package javaexamples;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class HashSetAddAll {

public static void main(String[] args) {

List<String> stringList = new ArrayList<String>();
stringList.add(“HashSet1”);
stringList.add(“HashSet2”);
Set<String> stringSet = new HashSet<String>();
stringSet.addAll(stringList);
System.out.println(“Printing set Elements”);
for (String string : stringSet) {
System.out.println(string);
}
}

}

Output

Printing set Elements
HashSet2
HashSet1

Nov 11

How to convert HashSet to array of values in java?

 6) Converting HashSet to array Example ?

import java.util.HashSet;
import java.util.Set;

public class HashSetToArrayExample {

	public static void main(String[] args) {

		Set<Integer> integerHashSet = new HashSet<Integer>();

		integerHashSet.add(1);

		integerHashSet.add(2);

		integerHashSet.add(3);

		Object[] integerArray = integerHashSet.toArray();

		for (Object intValue : integerArray) {

			System.out.println(intValue);

		}

	}

}

 

Output:

Printing elements in array12

3

Nov 11

How to clone HashSet in java?

5)  HashSet cloning  in java?

packagejavaexamples;import java.util.HashSet;

import java.util.Set;

public class CloningHashSet {

public static void main(String[] args) {

HashSet<Boolean> set = new HashSet<Boolean>();

set.add(true);

set.add(false);

Object Clonedset = set.clone();

System.out.println(“Original HashSet Values ” + set.toString());

System.out.println(“Cloned Set Values” + Clonedset.toString());

 

}

 

}

 

 

Output:

Original HashSet Values [false, true]Cloned Set Values[false, true]

 

Nov 11

How to verify an element exists in HashSet ?

4) Checking if  an element exists in HashSet ?

packagejavaexamples;importjava.util.HashSet;import java.util.Set;

public class VerifyHashSet {

public static void main(String[] args) {

Set<String> strings = new HashSet<String>();

strings.add(“HashSetElement1”);

strings.add(“HashSetElement2”);

strings.add(“HashSetElement3”);

System.out.println(“Elements of HashSet are printed below”);

if (strings.contains(“HashSetElement1”)) {

System.out.println(“Element is in HashSet”);

else {

System.out.println(“Element is not present”);

}

}

}

 

 

Output

Element is in HashSet
Nov 11

How to iterate HashSet in java using iterator?

3)      Example to iterate HashSet in java

package javaexamples;import java.util.HashSet;import java.util.Iterator;

import java.util.Set;

 

public class IteratingHashSet {

public static void main(String[] args) {

Set<String> strings = new HashSet<String>();

strings.add(“HashSetElement1”);

strings.add(“HashSetElement2”);

strings.add(“HashSetElement3”);

System.out.println(“Elements of HashSet are printed below”);

Iterator<String> itr = strings.iterator();

while (itr.hasNext()) {

System.out.println(itr.next());

}

}

}

 

 

Output:

Elements of HashSet are printed belowHashSetElement4HashSetElement3HashSetElement1
Nov 11

How to find size of HashSet in java?

 2)      Example to find size of  HashSet  in java

package javaexamples;import java.util.HashSet;

import java.util.Set;

 

public class HashSetExample {

public static void main(String[] args) {

Set<String> strings = new HashSet<String>();

strings.add(“example1”);

strings.add(“example2”);

strings.add(“example3”);

System.out.println(“Initial size of HashSet is ” + strings.size());

strings.remove(“example1”);

System.out.println(“After removing object size of Hash Set is ” + strings.size());

 

}

 

}

Output

Initial size of HashSet is 3After removing object size of Hash Set is 2
Nov 11

How to create HashSet in Java?

1)   Example to create HashSet in java

package javaexamples; 

import java.util.HashSet;

import java.util.Set;

public class CreatingHashSet {

public static void main(String[] args) {

Set<Integer> set = new HashSet<Integer>();

set.add(1);

set.add(2);

set.add(3);

for (Integer integer : set) {

System.out.println(integer);

}

}

}

 

Output :

123