- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Checking if a HashSet contains certain value in Java
Let’s say the following is our Integer array −
Integer arr[] = { 50, 100, 150, 200, 250, 300 };
Set the above integer array in HashSet −
Set<Integer>set = new HashSet<Integer>(Arrays.asList(arr));
Now, let us check whether the HashSet contains certain value or not −
set.contains(150)
TRUE is returned if the value is in the List, else FALSE is the return value.
Example
import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Demo { public static void main(String[] a) { Integer arr[] = { 50, 100, 150, 200, 250, 300 }; Set<Integer>set = new HashSet<Integer>(Arrays.asList(arr)); System.out.println(set.contains(200)); System.out.println(set.contains(150)); System.out.println(set.contains(100)); System.out.println(set.contains(10)); } }
Output
true true true false
- Related Articles
- Check if a Java HashSet Collection contains another Collection
- Check if a HashSet contains the specified element in C#
- Java Program to check if the String contains only certain characters
- Checking if a string contains all unique characters using JavaScript
- How to check if a string contains a certain word in C#?
- Java Program to Check if An Array Contains a Given Value
- How to check if a string only contains certain characters in Python?
- Java Program to check if a particular element exists in HashSet
- HashSet in Java
- Traverse through a HashSet in Java
- Java Program to Check if An Array Contains the Given Value
- Initialize HashSet in Java
- Initializing HashSet in Java
- The HashSet in Java
- Check if the Hashtable contains a specific value in C#

Advertisements