Merge pull request #9621 from jesperancinha/BAEL-4281
[BAEL-4281] Examples of array comparisons
This commit is contained in:
commit
acf23c0a11
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.arraycompare;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Plane {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String model;
|
||||
|
||||
public Plane(String name, String model) {
|
||||
|
||||
this.name = name;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
Plane plane = (Plane) o;
|
||||
return Objects.equals(name, plane.name) && Objects.equals(model, plane.model);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, model);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DeepEqualsCompareUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSameContents_whenDeepEquals_thenTrue() {
|
||||
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
|
||||
new Plane[] { new Plane("Plane 2", "B738") } };
|
||||
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
|
||||
new Plane[] { new Plane("Plane 2", "B738") } };
|
||||
|
||||
assertThat(Arrays.deepEquals(planes1, planes2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameContentsWithDifferentOrder_whenDeepEquals_thenFalse() {
|
||||
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
|
||||
new Plane[] { new Plane("Plane 2", "B738") } };
|
||||
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 2", "B738") },
|
||||
new Plane[] { new Plane("Plane 1", "A320") } };
|
||||
|
||||
assertThat(Arrays.deepEquals(planes1, planes2)).isFalse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class EqualsCompareUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSameContents_whenEquals_thenTrue() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
|
||||
assertThat(Arrays.equals(planes1, planes2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameContentsDifferentOrder_whenEquals_thenFalse() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final String[] planes2 = new String[] { "B738", "A320", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
|
||||
assertThat(Arrays.equals(planes1, planes2)).isFalse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LengthsCompareUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSameContent_whenSizeCompare_thenTrue() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final Integer[] quantities = new Integer[] { 10, 12, 34, 45, 12, 43, 5, 2 };
|
||||
|
||||
assertThat(planes1).hasSize(8);
|
||||
assertThat(quantities).hasSize(8);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class OrderCompareUnitTest {
|
||||
@Test
|
||||
public void givenSameContentDifferentOrder_whenSortedAndDeepEquals_thenTrue() {
|
||||
final Plane[][] planes1 = new Plane[][] {
|
||||
new Plane[] { new Plane("Plane 1", "A320"), new Plane("Plane 2", "B738") } };
|
||||
final Plane[][] planes2 = new Plane[][] {
|
||||
new Plane[] { new Plane("Plane 2", "B738"), new Plane("Plane 1", "A320") } };
|
||||
|
||||
Comparator<Plane> planeComparator = (o1, o2) -> {
|
||||
if (o1.getName()
|
||||
.equals(o2.getName())) {
|
||||
return o2.getModel()
|
||||
.compareTo(o1.getModel());
|
||||
}
|
||||
return o2.getName()
|
||||
.compareTo(o1.getName());
|
||||
};
|
||||
Arrays.sort(planes1[0], planeComparator);
|
||||
Arrays.sort(planes2[0], planeComparator);
|
||||
|
||||
assertThat(Arrays.deepEquals(planes1, planes2)).isTrue();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ReferenceCompareUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSameReferences_whenSame_thenTrue() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final String[] planes2 = planes1;
|
||||
|
||||
assertThat(planes1).isSameAs(planes2);
|
||||
|
||||
planes2[0] = "747";
|
||||
|
||||
assertThat(planes1).isSameAs(planes2);
|
||||
assertThat(planes2[0]).isEqualTo("747");
|
||||
assertThat(planes1[0]).isEqualTo("747");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameContentDifferentReferences_whenSame_thenFalse() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
|
||||
assertThat(planes1).isNotSameAs(planes2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue