A HashSet
is a collection that stores unique elements. It is implemented using a hash table data structure. The elements in a HashSet
are not stored in any particular order, and their position in the HashSet
can change as elements are added or removed.
When you add an element to a HashSet
, the hash code of the element is used to calculate its position in the hash table. If another element already exists at that position, the HashSet
uses the equals() method to determine if the two elements are equal. If the two elements are equal, the new element is not added. If the two elements are not equal, the HashSet
puts the new element in a different position in the hash table.
HashSet
is often used when you want to maintain a set of unique elements and don't care about their order.
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> monitors = new HashSet<String>();
monitors.add("Dell");
monitors.add("IBM");
monitors.add("Benq");
System.out.println(monitors);
//Size
System.out.println("Set Size: " + monitors.size());
//Check for element
System.out.println("Set Size: " + monitors.contains("Dell"));
//Get all elements
for (String x : monitors) {
System.out.println(x);
}
//Remove element
monitors.remove("Benq");
System.out.println(monitors);
//Clear all elements
monitors.clear();
System.out.println(monitors);
}
}
This is Java code that demonstrates the usage of a HashSet. The HashSet is a collection in Java that is used to store unique elements. Below is a breakdown of the code, line by line:
import java.util.HashSet;
This line imports the HashSet class from the Java Utility library. This is required because the code will make use of the HashSet.
public class Main {
This line declares a class called Main
. The class has a public access modifier, which means it can be accessed from outside of the class.
public static void main(String[] args) {
This line declares a main method, which is the entry point of the Java program. The public
modifier means the method can be accessed from outside of the class. The static
modifier means the method can be called without an instance of the class. The void
keyword indicates that the method doesn't return a value. The String[] args
parameter allows the method to accept command-line arguments.
HashSet<String> monitors = new HashSet<String>();
This line declares a HashSet called monitors
. The HashSet is used to store unique strings. The String
in HashSet<String>
specifies the type of elements to be stored in the HashSet. Here, it is String
.
monitors.add("Dell");
monitors.add("IBM");
monitors.add("Benq");
These three lines add three elements to the monitors
HashSet. The add
method is called on the HashSet to add each element.
System.out.println(monitors);
This line prints the entire monitors
HashSet to the console. The println
method is used to print the object to the console.
System.out.println("Set Size: " + monitors.size());
This line prints the size of the monitors
HashSet to the console. The size
method is called on the HashSet to get the number of elements in it. The +
operator is used to concatenate the size with the string "Set Size: ".
System.out.println("Set Size: " + monitors.contains("Dell"));
This line checks whether the monitors
HashSet contains the element "Dell". The contains
method is called on the HashSet to check whether the element is present. The result is then printed to the console.
for (String x : monitors) {
System.out.println(x);
}
This loop prints all the elements of the monitors
HashSet to the console. The loop iterates over each element in the HashSet, and the println
method is used to print each element to the console.
monitors.remove("Benq");
System.out.println(monitors);
This line removes the element "Benq" from the monitors
HashSet. The remove
method is called on the HashSet to remove the element. The updated HashSet is then printed to the console.
monitors.clear();
System.out.println(monitors);
This line removes all elements from the monitors
HashSet. The clear
method is called on the HashSet to remove all elements. The updated HashSet is then printed to the console.
No comments:
Post a Comment