[BAEL-4281] Examples of array comparisons
This commit is contained in:
parent
6c921b3558
commit
d1a9ff7480
8
java-collections/README.md
Normal file
8
java-collections/README.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
## Java Collections Examples
|
||||||
|
|
||||||
|
This module support code for articles about handling java collections and arrays.
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Comparing two integer arrays in Java](https://stackoverflow.com/questions/14897366/comparing-two-integer-arrays-in-java)
|
||||||
|
- [Compare two arrays with not the same order](https://stackoverflow.com/questions/54711228/compare-two-arrays-with-not-the-same-order)
|
||||||
|
- More articles: [[next -->]](../java-collections-conversions-2)
|
49
java-collections/pom.xml
Normal file
49
java-collections/pom.xml
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>java-collections</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>java-collections</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-collections4</artifactId>
|
||||||
|
<version>${commons-collections4.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-all</artifactId>
|
||||||
|
<version>${hamcrest-all.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>java-collections</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<commons-collections4.version>4.1</commons-collections4.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.comparingarrays;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
13
java-collections/src/main/resources/logback.xml
Normal file
13
java-collections/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import com.baeldung.comparingarrays.Plane;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class DeepEqualsCompareUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArray1andArray2_whenSameContent_thenDeepEquals() {
|
||||||
|
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") } };
|
||||||
|
|
||||||
|
boolean result = Arrays.deepEquals(planes1, planes2);
|
||||||
|
assertTrue("Result is not true", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() {
|
||||||
|
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") } };
|
||||||
|
|
||||||
|
boolean result = Arrays.deepEquals(planes1, planes2);
|
||||||
|
assertFalse("Result is true", result);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class EqualsCompareUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArray1andArray2_whenSameContent_thenEquals() {
|
||||||
|
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" };
|
||||||
|
|
||||||
|
boolean result = Arrays.equals(planes1, planes2);
|
||||||
|
assertTrue("Result is not true", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArray1andArray2_whenSameContentOtherSort_thenNotEquals() {
|
||||||
|
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" };
|
||||||
|
|
||||||
|
boolean result = Arrays.equals(planes1, planes2);
|
||||||
|
assertFalse("Result is true", result);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
public class LengthsCompareUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArray1andArray2_whenSameSizes_thenSizeEqualsOk() {
|
||||||
|
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, arrayWithSize(8));
|
||||||
|
assertThat(quantities, arrayWithSize(8));
|
||||||
|
assertThat(planes1.length, is(8));
|
||||||
|
assertThat(quantities.length, is(8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import com.baeldung.comparingarrays.Plane;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class OrderCompareUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() {
|
||||||
|
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);
|
||||||
|
|
||||||
|
boolean result = Arrays.deepEquals(planes1, planes2);
|
||||||
|
assertTrue("Result is false", result);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.Assert.assertNotSame;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
|
|
||||||
|
public class ReferenceCompareUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArray1andArray2_whenEquals_thenEqual() {
|
||||||
|
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||||
|
final String[] planes2 = planes1;
|
||||||
|
|
||||||
|
assertSame("Objects are not equal!", planes1, planes2);
|
||||||
|
|
||||||
|
planes2[0] = "747";
|
||||||
|
|
||||||
|
assertSame("Objects are not same!", planes1, planes2);
|
||||||
|
assertEquals("Objects are not equal!", "747", planes2[0]);
|
||||||
|
assertEquals("Objects are not equal!", "747", planes1[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArray1andArray2_whenDifferentValues_thenNotEqual() {
|
||||||
|
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" };
|
||||||
|
|
||||||
|
assertNotSame("Objects are the same!", planes1, planes2);
|
||||||
|
assertNotEquals("Objects are equal!", planes1, planes2);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user