0% found this document useful (0 votes)
5 views3 pages

Java HashSet Operations Guide

The document provides a series of Java code examples demonstrating various operations on HashSet, including adding elements, checking existence, removing elements, clearing the set, and iterating through it. It also covers advanced operations like union, intersection, difference, cloning, and comparing two HashSets. Each example is self-contained and illustrates a specific functionality of the HashSet class.

Uploaded by

Akash Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Java HashSet Operations Guide

The document provides a series of Java code examples demonstrating various operations on HashSet, including adding elements, checking existence, removing elements, clearing the set, and iterating through it. It also covers advanced operations like union, intersection, difference, cloning, and comparing two HashSets. Each example is self-contained and illustrates a specific functionality of the HashSet class.

Uploaded by

Akash Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1■■ Basic HashSet Example

import [Link];

public class HashSetProgram1 {


public static void main(String[] args) {
HashSet<String> fruits = new HashSet<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Mango");
[Link]("Apple"); // Duplicate not added
[Link](fruits);
}
}

2■■ Iterate HashSet using for-each loop


import [Link];

public class HashSetProgram2 {


public static void main(String[] args) {
HashSet<Integer> numbers = new HashSet<>();
[Link](10);
[Link](20);
[Link](30);
for (int num : numbers) {
[Link](num);
}
}
}

3■■ Check if an element exists in HashSet


import [Link];

public class HashSetProgram3 {


public static void main(String[] args) {
HashSet<String> cities = new HashSet<>();
[Link]("Delhi");
[Link]("Mumbai");
[Link]([Link]("Delhi"));
[Link]([Link]("Chennai"));
}
}

4■■ Remove element from HashSet


import [Link];

public class HashSetProgram4 {


public static void main(String[] args) {
HashSet<String> colors = new HashSet<>();
[Link]("Red");
[Link]("Green");
[Link]("Blue");
[Link]("Green");
[Link](colors);
}
}

5■■ Clear HashSet


import [Link];

public class HashSetProgram5 {


public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
[Link](1);
[Link](2);
[Link](3);
[Link]();
[Link](set);
}
}

6■■ Size of HashSet


import [Link];

public class HashSetProgram6 {


public static void main(String[] args) {
HashSet<String> names = new HashSet<>();
[Link]("Akash");
[Link]("Ravi");
[Link]("Size: " + [Link]());
}
}

7■■ Convert HashSet to Array


import [Link];

public class HashSetProgram7 {


public static void main(String[] args) {
HashSet<String> items = new HashSet<>();
[Link]("Pen");
[Link]("Book");
Object[] arr = [Link]();
for (Object obj : arr) {
[Link](obj);
}
}
}

8■■ Copy one HashSet to another


import [Link];

public class HashSetProgram8 {


public static void main(String[] args) {
HashSet<String> set1 = new HashSet<>();
[Link]("A");
[Link]("B");

HashSet<String> set2 = new HashSet<>(set1);


[Link](set2);
}
}

9■■ Union of two HashSets


import [Link];

public class HashSetProgram9 {


public static void main(String[] args) {
HashSet<Integer> set1 = new HashSet<>();
HashSet<Integer> set2 = new HashSet<>();
[Link](1); [Link](2);
[Link](2); [Link](3);
[Link](set2);
[Link](set1);
}
}

■ Intersection of two HashSets


import [Link];

public class HashSetProgram10 {


public static void main(String[] args) {
HashSet<Integer> set1 = new HashSet<>();
HashSet<Integer> set2 = new HashSet<>();
[Link](1); [Link](2); [Link](3);
[Link](2); [Link](3); [Link](4);
[Link](set2);
[Link](set1);
}
}

11■■ Difference of two HashSets


import [Link];

public class HashSetProgram11 {


public static void main(String[] args) {
HashSet<Integer> set1 = new HashSet<>();
HashSet<Integer> set2 = new HashSet<>();
[Link](1); [Link](2); [Link](3);
[Link](3); [Link](4);
[Link](set2);
[Link](set1);
}
}
12■■ Clone a HashSet
import [Link];

public class HashSetProgram12 {


public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
[Link]("X");
[Link]("Y");
HashSet<String> clone = (HashSet<String>) [Link]();
[Link](clone);
}
}

13■■ HashSet with null element


import [Link];

public class HashSetProgram13 {


public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
[Link](null);
[Link]("Hello");
[Link](set);
}
}

14■■ Compare two HashSets


import [Link];

public class HashSetProgram14 {


public static void main(String[] args) {
HashSet<Integer> a = new HashSet<>();
HashSet<Integer> b = new HashSet<>();
[Link](1); [Link](2);
[Link](1); [Link](2);
[Link]([Link](b));
}
}

15■■ Iterate HashSet using Iterator


import [Link];
import [Link];

public class HashSetProgram15 {


public static void main(String[] args) {
HashSet<String> languages = new HashSet<>();
[Link]("Java");
[Link]("Python");
[Link]("C++");

Iterator<String> itr = [Link]();


while ([Link]()) {
[Link]([Link]());
}
}
}

You might also like