Reputation: 155
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
Upvotes: 4
Views: 32849
Reputation: 425003
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)
Upvotes: 7
Reputation: 240
So you mean like this?
public static void main(String[] args) {
final Set<String> nounPhrases = new HashSet<>();
nounPhrases.add("java");
nounPhrases.add("jsp");
nounPhrases.add("book");
final Set<String> nounPhrases2 = new HashSet<>();
nounPhrases2.add("web");
nounPhrases2.add("php");
nounPhrases2.add("java");
nounPhrases2.add("book");
// Checking for every element in first set
for (final String element : nounPhrases) {
// if second set has the current element
if (nounPhrases2.contains(element)) {
System.out.println("They have " + element);
}
}
}
My output:
They have java
They have book
Edit: Based on your comment, if i understand correctly, if you want to get the common elements in both sets, just store the values and return them:
public static void main(String[] args) {
final Set<String> nounPhrases = new HashSet<>();
nounPhrases.add("java");
nounPhrases.add("jsp");
nounPhrases.add("book");
final Set<String> nounPhrases2 = new HashSet<>();
nounPhrases2.add("web");
nounPhrases2.add("php");
nounPhrases2.add("java");
nounPhrases2.add("book");
System.out.println(getCommon(nounPhrases, nounPhrases2));
}
public final static Set<String> getCommon(Set<String> setA, Set<String> setB) {
final Set<String> result = new HashSet<>();
for (final String element : setA) {
if (setB.contains(element)) {
result.add(element);
}
}
return result;
}
You could use generics to make the method work for other elements than strings:
public final static <T> Set<T> getCommon(Set<T> setA, Set<T> setB) {
final Set<T> result = new HashSet<>();
for (final T element : setA) {
if (setB.contains(element)) {
result.add(element);
}
}
return result;
}
If performance is important, you should check sizes first and only iterate over the elements of the smaller set. If you have one set with 1 elements, and one set with 100, starting with the smaller will leve you with one iteration whereas starting with the bigger will leave you with 100 checks where only 1 could have been in both sets.
Upvotes: 0
Reputation: 85
By the use of Java apache.commons.collections package we can implement
package com.StackoverFlow;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Set hs_1 = new HashSet();
hs_1.add("A");
hs_1.add("B");
hs_1.add("C");
hs_1.add("D");
Set hs_2 = new HashSet();
hs_2.add("A");
hs_2.add("B");
hs_2.add("C");
hs_2.add("D");
Collection result = CollectionUtils.subtract(hs_1, hs_2);
System.out.println(result);
if(result.isEmpty()){
System.out.println("perform Task-->>Value maches ");
}else{
System.out.println("perform Task-->>Value not maches ");
}
}
}
Upvotes: 0
Reputation: 71
If u want to find the common element then use collect(Collectors.toList()) instead of count , If u want simply to find how many set has common element using java 8
long count = nounPhrases.stream().filter(tempstring -> {
return nounPhrases2.stream().anyMatch(tempstring2 -> {
return tempstring.equals(tempstring2);
});
}).count();
if (count > 0)
System.out.println("has common elements-"+count);
else
System.out.println("not common");
Upvotes: 0