0% found this document useful (0 votes)
153 views2 pages

Java HashSet Basics for Beginners

This document discusses how to use a HashSet in Java. It shows how to add elements to the set, check the size, search for elements, remove elements, print all elements, iterate through the set using an iterator, and check if the set is empty. The HashSet does not maintain insertion order of elements and does not allow duplicate values.
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)
153 views2 pages

Java HashSet Basics for Beginners

This document discusses how to use a HashSet in Java. It shows how to add elements to the set, check the size, search for elements, remove elements, print all elements, iterate through the set using an iterator, and check if the set is empty. The HashSet does not maintain insertion order of elements and does not allow duplicate values.
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
You are on page 1/ 2

HashSet in Java

import [Link];
import [Link];

public class Hashing {


public static void main(String args[]) {
HashSet<Integer> set = new HashSet<>();

//Add
[Link](1);
[Link](2);
[Link](3);
[Link](1);

//Size
[Link]("size of set is : " + [Link]());

//Search
if([Link](1)) {
[Link]("present");
}

if(![Link](6)) {
[Link]("absent");
}

//Delete
[Link](1);
if(![Link](1)) {
[Link]("absent");
}

//Print all elements


[Link](set);

//Iteration - HashSet does not have an order


[Link](0);
Iterator it = [Link]();
while ([Link]()) {
[Link]([Link]() + ", ");
}
[Link]();

//isEmpty
if(![Link]()) {
[Link]("set is not empty");
}
}
}

You might also like