Java – How to compare two Sets

In Java, there is no ready make API to compare two java.util.Set

1. Solution

Here’s my implementation, combine check size + containsAll :

SetUtils.java

package com.mkyong.core.utils;

import java.util.Set;

public class SetUtils {

    public static boolean equals(Set<?> set1, Set<?> set2){

        if(set1 == null || set2 ==null){
            return false;
        }

        if(set1.size()!=set2.size()){
            return false;
        }

        return set1.containsAll(set2);

    }
}

2. Unit Test

Unit tests for the above SetUtils.java

TestSetUtils.java

package com.mkyong.core.utils;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class TestSetUtils {

    @Test
    public void test1() {

        Set<String> test1 = new HashSet<>();
        test1.add("a");
        test1.add("b");

        Set<String> test2 = new HashSet<>();
        test2.add("b");
        test2.add("c");

        assertThat(SetUtils.equals(test1, test2), is(false));

    }

    @Test
    public void test2() {

        Set<String> test1 = new HashSet<>();
        test1.add("a");
        test1.add("b");

        Set<String> test2 = new HashSet<>();
        test2.add("a");
        test2.add("b");
        test2.add("c");

        assertThat(SetUtils.equals(test1, test2), is(false));

    }

    @Test
    public void test3() {

        Set<String> test1 = new HashSet<>();
        test1.add("a");
        test1.add("b");
        test1.add("c");

        Set<String> test2 = new HashSet<>();
        test2.add("a");
        test2.add("b");

        assertThat(SetUtils.equals(test1, test2), is(false));

    }

    //set ignore sequence
    @Test
    public void test4() {

        Set<String> test1 = new HashSet<>();
        test1.add("a");
        test1.add("b");

        Set<String> test2 = new HashSet<>();
        test2.add("b");
        test2.add("a");

        assertThat(SetUtils.equals(test1, test2), is(true));

    }

    @Test
    public void test5() {

        Set<String> test1 = new HashSet<>();
        test1.add("a");

        Set<String> test2 = new HashSet<>();
        test2.add("a");

        assertThat(SetUtils.equals(test1, test2), is(true));

    }

}

Run it, all unit tests will be passed.

References

  1. java.util.Set JavaDoc

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Marcel Ammerlaan
9 years ago

This is not needed. Look at AbstractSet.equals() it does this (and a bit better).

SreeCharan Shroff
9 years ago

@Test
public void test6(){
Set test1=null;
Set test2=null;
assertTrue(SetUtils.equals(test1, test2));
}
this test fails, should it not pass ?

Jake
9 years ago

Look at the implementation of equals:

if(set1 == null || set2 ==null){
return false;
}

if you need the null valued two objects be equal, change the implementation.

SreeCharan Shroff
9 years ago
Reply to  Jake

Jake,
yes, implementation should be changed to address null
Thanks,
Charan