Java exists in set

Java hashset contains – How to Search for an Element in HashSet in Java? | HashSet contains() Method Implementation in Java

How to Search for an Element in HashSet in Java

Java hashset contains: The HashSet class implements the Set interface and is supported by a hash table, which is a HashMap instance. The null element is allowable in this class. The class also provides continuous time for basic operations like adding, removing, containing, and size provided that the Hash function correctly distributes the elements between the buckets.

In this tutorial we will explain all about how to Search for an Element in a HashSet using Contains() method. Refer to the Programs that Checks Whether a Specific Element is present in Hashet or not. Given a HashSet, the task is to search for an element in HashSet.

Given hashset elements : < 'hello' , 'this' , 'is', 'BTechGeeks' , 'is', 'platform' >search_element ='BTechGeeks'
The given hashset contains BTechGeeks element in it.

How to Search for an Element in HashSet in Java?

Hashset contains java: A HashSet’s main feature is fast searching. This is accomplished through the use of Hashing. Assume we want to see if an element in HashSet exists. It is possible to accomplish this by using HashSet’s contains() member function.

Читайте также:  Платформа java на телефоне

HashSet contains() Method in Java

Hashset contains: The Java.util.HashSet.contains() method is used to determine whether or not a specific element exists in the HashSet. So, in essence, it is used to determine whether a Set contains a specific element or not.

given_hashSet.contains(Object element)
HashSet is the type of the parameter element. This is the element that must be checked to see if it is present in the set.
If the element is present in the set, the method returns true otherwise, it returns False.

Example Program Implementing the Java.util.HashSet.contains() method

// Java code to illustrate HashSet.contains() method import java.io.*; import java.util.HashSet; public class HashSetDemo < public static void main(String args[]) < // Creating an empty HashSet HashSetset = new HashSet(); // Using add() method to add elements into the Set set.add("Welcome"); set.add("To"); set.add("Geeks"); set.add("4"); set.add("Geeks"); // Displaying the HashSet System.out.println("HashSet: " + set); // Check for "Geeks" in the set System.out.println("Does the Set contains 'Geeks'? " + set.contains("Geeks")); // Check for "4" in the set System.out.println("Does the Set contains '4'? " + set.contains("4")); // Check if the Set contains "No" System.out.println("Does the Set contains 'No'? " + set.contains("No")); > >
HashSet: [4, Geeks, Welcome, To] Does the Set contains 'Geeks'? true Does the Set contains '4'? true Does the Set contains 'No'? false

It will first determine the Hash Code of the given object. Then, based on that hash code, it will go to a bucket and use the equals() member function to check all elements within the bucket.

Example to Check if an Item Exists in HashSet or Not

import java.io.*; import java.lang.*; import java.util.*; class BtechGeeks < public static void main(String[] args) throws java.lang.Exception < // initailzing a hashset which is of type string HashSetstringset = new HashSet<>(); // adding elements to the hashset stringset.add("hello"); stringset.add("this"); stringset.add("is"); stringset.add("BTechGeeks"); stringset.add("is"); stringset.add("platform"); // given element String element = "BTechGeeks"; // looking for the given element in the hash set if (stringset.contains(element)) < System.out.println("The given hashset contains " + element + " element in it."); >else < System.out.println( "The given hashset doesn't contain " + element + " element in it."); >> >
The given hashset contains BTechGeeks element in it.

Explanation: Here BTechGeeks element is present in the given HashSet
Related Programs:

Читайте также:  Css link not underlined

Источник

Java HashSet

A HashSet is a collection of items where every item is unique, and it is found in the java.util package:

Example

Create a HashSet object called cars that will store strings:

import java.util.HashSet; // Import the HashSet class HashSet cars = new HashSet(); 

Add Items

The HashSet class has many useful methods. For example, to add items to it, use the add() method:

Example

// Import the HashSet class import java.util.HashSet; public class Main < public static void main(String[] args) < HashSetcars = new HashSet(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("BMW"); cars.add("Mazda"); System.out.println(cars); > > 

Note: In the example above, even though BMW is added twice it only appears once in the set because every item in a set has to be unique.

Check If an Item Exists

To check whether an item exists in a HashSet, use the contains() method:

Example

Remove an Item

To remove an item, use the remove() method:

Example

To remove all items, use the clear() method:

Example

HashSet Size

To find out how many items there are, use the size method:

Example

Loop Through a HashSet

Loop through the items of an HashSet with a for-each loop:

Example

Other Types

Items in an HashSet are actually objects. In the examples above, we created items (objects) of type «String». Remember that a String in Java is an object (not a primitive type). To use other types, such as int, you must specify an equivalent wrapper class: Integer . For other primitive types, use: Boolean for boolean, Character for char, Double for double, etc:

Example

Use a HashSet that stores Integer objects:

import java.util.HashSet; public class Main < public static void main(String[] args) < // Create a HashSet object called numbers HashSetnumbers = new HashSet(); // Add values to the set numbers.add(4); numbers.add(7); numbers.add(8); // Show which numbers between 1 and 10 are in the set for(int i = 1; i else < System.out.println(i + " was not found in the set."); >> > > 

Источник

Оцените статью