- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Check if a Java HashSet Collection contains another Collection
To check whether a HashSet contains another, use the contains() method.
Set the first HashSet
String strArr[] = { "P", "Q", "R" }; Set set1 = new HashSet(Arrays.asList(strArr));
Set the second HashSet
String strArr = new String[] { "P", "Q"}; Set set2 = new HashSet(Arrays.asList(strArr));
Check now
set1.containsAll(set2))
The following is an example to check if a HashSet collection in Java contains another Collection
Example
import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Demo { public static void main(String[] a) { String strArr[] = { "P", "Q", "R" }; Set set1 = new HashSet(Arrays.asList(strArr)); strArr = new String[] { "P", "Q"}; Set set2 = new HashSet(Arrays.asList(strArr)); System.out.println(set1.containsAll(set2)); strArr = new String[] { "T", "U"}; Set set3 = new HashSet(Arrays.asList(strArr)); System.out.println(set1.containsAll(set3)); } }
The following is the output
true false
- Related Articles
- Create HashSet from another collection in C#
- Check if OrderedDictionary collection contains a specific key in C#
- Check if a HashSet and a specified collection share common element in C#
- Check if a HashSet is a subset of the specified collection in C#
- Check if a HashSet is a superset of the specified collection in C#
- Check if a HashSet is a proper subset of the specified collection in C#
- Check if a HashSet is a proper superset of the specified collection in C#
- Check if HashSet and the specified collection contain the same elements in C#
- How to retain elements from a Collection in another Collection
- Add all the elements from a collection to the HashSet in Java
- Create a new ArrayList from another collection in Java
- How to remove all elements from a Collection in another Collection
- Check if a HashSet contains the specified element in C#
- Create Octet Tuple from another collection in Java
- Create Ennead Tuple from another collection in Java

Advertisements