in Java API, it seems that there is no way to compare the contents of two sets, so we can only write one by ourselves to realize it. In fact, it is relatively simple to compare the number of records to see whether they are the same, and then see whether the contents are consistent. The test method is as follows:
public static boolean equals(Set<?> set1, Set<?> set2){
if(set1 == null || set2 ==null){//null就直接不比了
return false;
}
if(set1.size()!=set2.size()){//大小不同也不用比了
return false;
}
return set1.containsAll(set2);//最后比containsAll
}
unit test:
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));
}
}
p>
reproduced from: http://ju.outofmemory.cn/entry/288737 p>
Read More:
- How to compare the equality of structures in golang
- Error: (44,73) lambda expression and XML are not supported in Java: – source 1.7 Error:java : Compilation failed
- @Value sets the default value
- Two ways of fuzzy query in mybatis
- Struts 2 encapsulates form data into list and map sets
- Error: java.lang.IllegalArgumentExcept :Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
- Two problems in OpenGL Programming
- Two methods of cmake in VTK program
- MySQL creates tables and sets auto increment of primary keys
- In pandas, dataframe and np.array The mutual transformation between the two
- Eclipse .java File Syntax error, parameterized types are only available if source level is 1.5 or
- Using the pit in Git that rebase encountered two could not apply XXXX
- In the next two years, how do data analysts hang up highly educated engineers? be convinced!
- Error in idea compilation: java.lang.OutOfMemoryError Java heap space and java.lang.StackOverflowError
- IntelliJ idea sets the default working directory
- What are the web front end technologies? What are the differences between cookie and session
- PostgreSQL sets the default value of the field to the current year
- Word column after the text evenly distributed in the left and right two columns, rather than fill in the left column and then fill in the right column, how to do?
- The sum of the two numbers of leetcode
- The solution of comments are not permitted in JSON. (521) in vscode