Java : How to Compare two Sets and check its Equality efficiently
This tutorial explains the best and efficient way to compare two sets and equality using containsAll() and equals() method of Set interface. let’s look at the example below.
Compare two sets and check its equality
In this example you will learn how overriding equals() and hashcode() method of Object class make object comparisons efficient and easy in Java programs.
Let’s consider the following Skill pojo for our example with equals() and hashcode() overriden.
Skill.java
package com.sneppets.model; public class Skill { private Long id; private String name; public Skill() { } public Skill(Long id, String name) { super(id); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public boolean equals(Object obj) { if(obj == this) return true; if(!(obj instanceof Skill)) { return false; } Skill skill = (Skill)obj; return skill.id.equals(id); } @Override public int hashCode() { return this.id.hashCode(); } }
Now, consider the following example CompareTwoSetsExample code which actually compares sets and also check its equality.
CompareTwoSetsExample.java
package com.sneppets.app; import java.util.HashSet; import java.util.Set; import com.plan.domain.Skill; public class CompareTwoSetsExample { public static void main (String[] args) { Set<Skill> set1 = new HashSet<>(); set1.add(new Skill(1L, "Java")); set1.add(new Skill(2L, "Python")); set1.add(new Skill(3L, "Golang")); set1.add(new Skill(4L, "C++")); Set<Skill> set2 = new HashSet<>(); set2.add(new Skill(1L, "Java")); set2.add(new Skill(2L, "Python")); set2.add(new Skill(3L, "Scala")); set2.add(new Skill(4L, "Kubernetes")); set2.add(new Skill(5L, "Docker")); Set<Skill> set3 = new HashSet<>(); set3.add(new Skill(1L, "Java")); set3.add(new Skill(2L, "Python")); set3.add(new Skill(3L, "Golang")); Set<Skill> set4 = new HashSet<>(); set4.add(new Skill(1L, "Java")); set4.add(new Skill(2L, "Python")); set4.add(new Skill(3L, "Golang")); set4.add(new Skill(4L, "C++")); //compare using containsAll System.out.println("---Compare two sets---"); System.out.println("Is set2 contains all elements of set1 ? " + compareSets(set2, set1)); System.out.println("Is set3 contains all elements of set1 ? " + compareSets(set3, set1)); //check equality using equals System.out.println("---Check if two sets are equal---"); System.out.println("Is set2 equal to set1 ? " + checkEquality(set3, set1)); System.out.println("Is set4 equal to set1 ? " + checkEquality(set4, set1)); } //check whether set2 contains all elements of set1 private static boolean compareSets(Set<Skill> set1, Set<Skill> set2) { if(set1.isEmpty() || set2.isEmpty()) { return false; } return set2.containsAll(set1); } //check equality private static boolean checkEquality(Set<Skill> set1, Set<Skill> set2) { if(set1.isEmpty() || set2.isEmpty()) { return false; } if(set1.size() != set2.size()) { return false; } return set2.equals(set1); } }
Output
---Compare two sets--- Is set2 contains all elements of set1 ? false Is set3 contains all elements of set1 ? true ---Check if two sets are equal--- Is set2 equal to set1 ? false Is set4 equal to set1 ? true
Further Reading
- How to add hours to unix timestamp or epochmilli in java?
- Convert List to Map in Java 8 using Streams and Collectors
- Java ArrayList removeAll() method not removing elements
- Difference between Spring ApplicationContext and BeanFactory