Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two hash sets in java?

How can I compare two hash sets in java ? My first hash sets looks like below.

static Set<String> nounPhrases = new HashSet<>();

Above hash set contains elements like this.

List of Noun Parse : [java, jsp, book]

2nd hash set

static Set<String> nounPhrases2 = new HashSet<>();

List of Noun Parse : [web, php, java,book]

Note - I need to check if there are equal nouns in both sets. and if they have similar nouns then I need to do another task

like image 757
user8048032 Avatar asked Sep 13 '25 18:09

user8048032


1 Answers

This is a wheel already invented.

Set#equals() compares sets in the way you would expect:

set1.equals(set2)

If you want two Set variables that are both null to be "equal", then use:

Objects.equals(set1, set2)
like image 110
Bohemian Avatar answered Sep 15 '25 07:09

Bohemian