Last active
December 18, 2015 23:09
-
-
Save hachi-eiji/5859893 to your computer and use it in GitHub Desktop.
Objects.equals and deepEquals method sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package objects; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertTrue; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Objects; | |
import org.junit.Test; | |
public class EqualsSample { | |
@Test | |
public void test_equals_list() { | |
List<String> list1 = new ArrayList<>(); | |
list1.add("hoge"); | |
list1.add("fuga"); | |
List<String> list2 = new ArrayList<>(); | |
list2.add("hoge"); | |
list2.add("fuga"); | |
assertTrue(Objects.equals(list1, list2)); | |
assertTrue(Objects.deepEquals(list1, list2)); | |
} | |
@Test | |
public void test_equals_sampleBean() { | |
List<String> list1 = new ArrayList<>(); | |
list1.add("hoge"); | |
list1.add("fuga"); | |
NoImplementsEqualsMethodBean sample1 = new NoImplementsEqualsMethodBean(1, list1); | |
NoImplementsEqualsMethodBean sample2 = new NoImplementsEqualsMethodBean(1, list1); | |
assertFalse(Objects.equals(sample1, sample2)); | |
assertFalse(Objects.deepEquals(sample1, sample2)); | |
} | |
@Test | |
public void test_equals_sampleBean2() { | |
List<String> list1 = new ArrayList<>(); | |
list1.add("hoge"); | |
list1.add("fuga"); | |
ImplementsEqualsMethodBean sample1 = new ImplementsEqualsMethodBean(1, list1); | |
List<String> list2 = new ArrayList<>(); | |
list2.add("hoge"); | |
list2.add("fuga"); | |
ImplementsEqualsMethodBean sample2 = new ImplementsEqualsMethodBean(1, list2); | |
assertTrue(Objects.equals(sample1, sample2)); | |
assertTrue(Objects.deepEquals(sample1, sample2)); | |
} | |
@Test | |
public void test_equals_null() { | |
ImplementsEqualsMethodBean sample1 = null; | |
ImplementsEqualsMethodBean sample2 = null; | |
assertTrue(Objects.equals(sample1, sample2)); | |
assertTrue(Objects.deepEquals(sample1, sample2)); | |
} | |
@Test | |
public void test_arrays_equals(){ | |
ImplementsEqualsMethodBean array1 [] = new ImplementsEqualsMethodBean[4]; | |
ImplementsEqualsMethodBean bean1 = new ImplementsEqualsMethodBean(1, new ArrayList<String>()); | |
array1[0] = bean1; | |
ImplementsEqualsMethodBean array2 [] = new ImplementsEqualsMethodBean[4]; | |
ImplementsEqualsMethodBean bean2 = new ImplementsEqualsMethodBean(1, new ArrayList<String>()); | |
array2[0] = bean2; | |
assertFalse(Objects.equals(array1, array2)); | |
assertTrue(Objects.deepEquals(array1, array2)); | |
} | |
public class NoImplementsEqualsMethodBean { | |
private Integer id; | |
private List<String> list; | |
public NoImplementsEqualsMethodBean(Integer id, List<String> list) { | |
this.id = id; | |
this.list = list; | |
} | |
} | |
public class ImplementsEqualsMethodBean { | |
private Integer id; | |
private List<String> list; | |
public ImplementsEqualsMethodBean(Integer id, List<String> list) { | |
this.id = id; | |
this.list = list; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.lang.Object#hashCode() | |
*/ | |
@Override | |
public int hashCode() { | |
return Objects.hash(id, list); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.lang.Object#equals(java.lang.Object) | |
*/ | |
@Override | |
public boolean equals(Object obj) { | |
if (obj == null) { | |
return false; | |
} | |
if (!(obj instanceof ImplementsEqualsMethodBean)) { | |
return false; | |
} | |
return this.id == ((ImplementsEqualsMethodBean) obj).id; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment