diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arraycompare/Plane.java b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arraycompare/Plane.java new file mode 100644 index 0000000000..1731578b76 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arraycompare/Plane.java @@ -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); + } +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java new file mode 100644 index 0000000000..c8ebafb26b --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java @@ -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(); + } +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java new file mode 100644 index 0000000000..a022bf7082 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java @@ -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(); + } +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java new file mode 100644 index 0000000000..23187b827c --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java @@ -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); + } +} + diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java new file mode 100644 index 0000000000..4dd7964020 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java @@ -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 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(); + } +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java new file mode 100644 index 0000000000..d8072a98e3 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java @@ -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); + } +}