[JAVA-616] core-java-arrays-operations-advanced
* Creation * Moved code from https://www.baeldung.com/java-array-copy * Moved code from https://www.baeldung.com/java-arrays-deepequals * Moved code from https://www.baeldung.com/java-array-sum-average * Moved code from https://www.baeldung.com/java-array-intersection
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
## Core Java Arrays - Advanced Operations
|
||||
|
||||
This module contains articles about advanced operations on arrays in Java
|
||||
|
||||
Advanced operations would be much logic or interactions with other arrays, such as:
|
||||
|
||||
- Sum, average of elements
|
||||
- Intersections
|
||||
- ...
|
||||
|
||||
### Relevant Articles:
|
||||
- [How to Copy an Array in Java](https://www.baeldung.com/java-array-copy)
|
||||
- [Arrays.deepEquals](https://www.baeldung.com/java-arrays-deepequals)
|
||||
- [Find Sum and Average in a Java Array](https://www.baeldung.com/java-array-sum-average)
|
||||
- [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection)
|
||||
- [[More basic operations-->]](/core-java-modules/core-java-operations-basic)
|
||||
@@ -0,0 +1,36 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>core-java-arrays-operations-advanced</artifactId>
|
||||
<name>core-java-arrays-operations-advanced</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SumAndAverageInArray {
|
||||
|
||||
public static int findSumWithoutUsingStream(int[] array) {
|
||||
int sum = 0;
|
||||
for (int value : array) {
|
||||
sum += value;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static int findSumUsingStream(int[] array) {
|
||||
return Arrays.stream(array).sum();
|
||||
}
|
||||
|
||||
public static int findSumUsingStream(Integer[] array) {
|
||||
return Arrays.stream(array).mapToInt(Integer::intValue).sum();
|
||||
}
|
||||
|
||||
public static double findAverageWithoutUsingStream(int[] array) {
|
||||
int sum = findSumWithoutUsingStream(array);
|
||||
return (double) sum / array.length;
|
||||
}
|
||||
|
||||
public static double findAverageUsingStream(int[] array) {
|
||||
return Arrays.stream(array).average().orElse(Double.NaN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.array.operations;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ArraysIntersectionOperations {
|
||||
|
||||
public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(Arrays.asList(b)::contains)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
public static Integer[] intersectionSet(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(Arrays.asList(b)::contains)
|
||||
.distinct()
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(new LinkedList<>(Arrays.asList(b))::remove)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.arraycopy.model;
|
||||
|
||||
public class Address implements Cloneable {
|
||||
private String country;
|
||||
private String state;
|
||||
private String city;
|
||||
private String street;
|
||||
private String zipcode;
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getZipcode() {
|
||||
return zipcode;
|
||||
}
|
||||
|
||||
public void setZipcode(String zipcode) {
|
||||
this.zipcode = zipcode;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone() throws CloneNotSupportedException {
|
||||
super.clone();
|
||||
Address address = new Address();
|
||||
address.setCity(this.city);
|
||||
address.setCountry(this.country);
|
||||
address.setState(this.state);
|
||||
address.setStreet(this.street);
|
||||
address.setZipcode(this.zipcode);
|
||||
return address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.arraycopy.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Employee implements Serializable {
|
||||
private static final long serialVersionUID = -2454619097207585825L;
|
||||
private int id;
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Employee() {
|
||||
}
|
||||
|
||||
public Employee(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Employee(int id, String name, int age) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SumAndAverageInArrayUnitTest {
|
||||
@Test
|
||||
public void givenAnIntArray_whenNotUsingStream_thenFindSum() {
|
||||
int[] array = { 1, 3, 4, 8, 19, 20 };
|
||||
int expectedSumOfArray = 55;
|
||||
int actualSumOfArray = SumAndAverageInArray.findSumWithoutUsingStream(array);
|
||||
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnIntArray_whenUsingStream_thenFindSum() {
|
||||
int[] array = { 1, 3, 4, 8, 19, 20 };
|
||||
int expectedSumOfArray = 55;
|
||||
int actualSumOfArray = SumAndAverageInArray.findSumUsingStream(array);
|
||||
|
||||
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnBoxedIntegerArray_whenUsingStream_thenFindSum() {
|
||||
Integer[] array = new Integer[]{1, 3, 4, 8, 19, 20};
|
||||
int expectedSumOfArray = 55;
|
||||
int actualSumOfArray = SumAndAverageInArray.findSumUsingStream(array);
|
||||
|
||||
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnIntArray_whenNotUsingStream_thenFindAverage() {
|
||||
int[] array = { 1, 3, 4, 8, 19, 20 };
|
||||
double expectedAvgOfArray = 9.17;
|
||||
double actualAvgOfArray = SumAndAverageInArray.findAverageWithoutUsingStream(array);
|
||||
|
||||
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnIntArray_whenUsingStream_thenFindAverage() {
|
||||
int[] array = { 1, 3, 4, 8, 19, 20 };
|
||||
double expectedAvgOfArray = 9.17;
|
||||
double actualAvgOfArray = SumAndAverageInArray.findAverageUsingStream(array);
|
||||
|
||||
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnEmptyIntArray_whenUsingStream_thenFindAverage() {
|
||||
int[] array = {};
|
||||
double expectedAvgOfArray = Double.NaN;
|
||||
double actualAvgOfArray = SumAndAverageInArray.findAverageUsingStream(array);
|
||||
|
||||
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.00);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.array.operations;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.baeldung.array.operations.ArraysIntersectionOperations.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class IntersectionUnitTest {
|
||||
private static final Integer[] a = { 1, 3, 2 };
|
||||
private static final Integer[] b = { 4, 3, 2, 4, 2, 3, 4, 4, 3 };
|
||||
private static final Integer[] c = { 1, 3, 2, 3, 3, 2 };
|
||||
|
||||
@Test
|
||||
void whenIntersectionSimpleIsUsed_thenCommonEntriesAreInTheResult() {
|
||||
assertThat(intersectionSimple(a, b)).isEqualTo(new Integer[] { 3, 2 });
|
||||
assertThat(intersectionSimple(b, a)).isEqualTo(new Integer[] { 3, 2, 2, 3, 3 });
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenIntersectionSimpleIsUsedWithAnArrayAndItself_thenTheResultIsTheIdentity() {
|
||||
assertThat(intersectionSimple(b, b)).isEqualTo(b);
|
||||
assertThat(intersectionSimple(a, a)).isEqualTo(a);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenIntersectionSetIsUsed_thenCommonEntriesAreInTheResult() {
|
||||
assertThat(intersectionSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenIntersectionSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder() {
|
||||
assertThat(intersectionSet(a, b)).isEqualTo(new Integer[] { 3, 2 });
|
||||
assertThat(intersectionSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasNoDuplicateEntries_ThenTheResultIsTheIdentity() {
|
||||
assertThat(intersectionSet(a, a)).isEqualTo(a);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasDuplicateEntries_ThenTheResultIsNotTheIdentity() {
|
||||
assertThat(intersectionSet(b, b)).isNotEqualTo(b);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMultiSetIsUsed_thenCommonEntriesAreInTheResult() {
|
||||
assertThat(intersectionMultiSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenIntersectionMultiSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder() {
|
||||
assertThat(intersectionMultiSet(a, b)).isEqualTo(new Integer[] { 3, 2 });
|
||||
assertThat(intersectionMultiSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
|
||||
assertThat(intersectionMultiSet(b, c)).isEqualTo(new Integer[] { 3, 2, 2, 3, 3 });
|
||||
assertThat(intersectionMultiSet(c, b)).isEqualTo(new Integer[] { 3, 2, 3, 3, 2 });
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenIntersectionMultiSetIsUsedWithAnArrayAndWithItself_ThenTheResultIsTheIdentity() {
|
||||
assertThat(intersectionMultiSet(b, b)).isEqualTo(b);
|
||||
assertThat(intersectionMultiSet(a, a)).isEqualTo(a);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package com.baeldung.arraycopy;
|
||||
|
||||
import com.baeldung.arraycopy.model.Address;
|
||||
import com.baeldung.arraycopy.model.Employee;
|
||||
import org.apache.commons.lang3.SerializationUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ArrayCopyUtilUnitTest {
|
||||
private static Employee[] employees;
|
||||
private static final int MAX = 2;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup(){
|
||||
createEmployeesArray();
|
||||
}
|
||||
|
||||
private static void createEmployeesArray() {
|
||||
employees = new Employee[MAX];
|
||||
Employee employee;
|
||||
for(int i = 0; i < MAX; i++) {
|
||||
employee = new Employee();
|
||||
employee.setName("Emp"+i);
|
||||
employee.setId(i);
|
||||
employees[i] = employee;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedViaSystemsArrayCopy_thenSuccessful(){
|
||||
int[] array = {23, 43, 55};
|
||||
int[] copiedArray = new int[3];
|
||||
|
||||
System.arraycopy(array, 0, copiedArray, 0, 3);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, array);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaSystemsArrayCopy_thenSuccessful(){
|
||||
int[] array = {23, 43, 55, 12, 65, 88, 92};
|
||||
int[] copiedArray = new int[3];
|
||||
|
||||
System.arraycopy(array, 2, copiedArray, 0, 3);
|
||||
|
||||
assertTrue(3 == copiedArray.length);
|
||||
assertTrue(copiedArray[0] == array[2]);
|
||||
assertTrue(copiedArray[1] == array[3]);
|
||||
assertTrue(copiedArray[2] == array[4]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaArraysCopyOfRange_thenSuccessful(){
|
||||
int[] array = {23, 43, 55, 12, 65, 88, 92};
|
||||
|
||||
int[] copiedArray = Arrays.copyOfRange(array, 1, 4);
|
||||
|
||||
assertTrue(3 == copiedArray.length);
|
||||
assertTrue(copiedArray[0] == array[1]);
|
||||
assertTrue(copiedArray[1] == array[2]);
|
||||
assertTrue(copiedArray[2] == array[3]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedViaArraysCopyOf_thenValueChangeIsSuccessful(){
|
||||
int[] array = {23, 43, 55, 12};
|
||||
int newLength = array.length;
|
||||
|
||||
int[] copiedArray = Arrays.copyOf(array, newLength);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, array);
|
||||
array[0] = 9;
|
||||
assertTrue(copiedArray[0] != array[0]);
|
||||
copiedArray[1] = 12;
|
||||
assertTrue(copiedArray[1] != array[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfNonPrimitiveType_whenCopiedViaArraysCopyOf_thenDoShallowCopy(){
|
||||
Employee[] copiedArray = Arrays.copyOf(employees, employees.length);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, employees);
|
||||
employees[0].setName(employees[0].getName()+"_Changed");
|
||||
//change in employees' element caused change in the copied array
|
||||
assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedViaArrayClone_thenValueChangeIsSuccessful(){
|
||||
int[] array = {23, 43, 55, 12};
|
||||
|
||||
int[] copiedArray = array.clone();
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, array);
|
||||
array[0] = 9;
|
||||
assertTrue(copiedArray[0] != array[0]);
|
||||
copiedArray[1] = 12;
|
||||
assertTrue(copiedArray[1] != array[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){
|
||||
Employee[] copiedArray = employees.clone();
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, employees);;
|
||||
employees[0].setName(employees[0].getName()+"_Changed");
|
||||
//change in employees' element changed the copied array
|
||||
assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfCloneableNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){
|
||||
Address[] addresses = createAddressArray();
|
||||
|
||||
Address[] copiedArray = addresses.clone();
|
||||
|
||||
addresses[0].setCity(addresses[0].getCity()+"_Changed");
|
||||
Assert.assertArrayEquals(copiedArray, addresses);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfSerializableNonPrimitiveType_whenCopiedViaSerializationUtils_thenDoDeepCopy(){
|
||||
Employee[] copiedArray = SerializationUtils.clone(employees);
|
||||
|
||||
employees[0].setName(employees[0].getName()+"_Changed");
|
||||
//change in employees' element didn't change in the copied array
|
||||
Assert.assertFalse(
|
||||
copiedArray[0].getName().equals(employees[0].getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfNonPrimitiveType_whenCopiedViaStream_thenDoShallowCopy(){
|
||||
Employee[] copiedArray = Arrays.stream(employees).toArray(Employee[]::new);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, employees);
|
||||
employees[0].setName(employees[0].getName()+"_Changed");
|
||||
//change in employees' element didn't change in the copied array
|
||||
assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfPrimitiveType_whenCopiedViaStream_thenSuccessful(){
|
||||
String[] strArray = {"orange", "red", "green'"};
|
||||
|
||||
String[] copiedArray = Arrays.stream(strArray).toArray(String[]::new);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, strArray);
|
||||
}
|
||||
|
||||
private Address[] createAddressArray(){
|
||||
Address[] addresses = new Address[1];
|
||||
addresses[0] = createAddress();
|
||||
return addresses;
|
||||
}
|
||||
|
||||
private Address createAddress() {
|
||||
Address address = new Address();
|
||||
address.setCountry("USA");
|
||||
address.setState("CA");
|
||||
address.setCity("San Francisco");
|
||||
address.setStreet("Street 1");
|
||||
address.setZipcode("59999");
|
||||
return address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.arrays.deepequals;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ArraysDeepEqualsUnitTest {
|
||||
|
||||
class Person {
|
||||
private int id;
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
Person(int id, String name, int age) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof Person))
|
||||
return false;
|
||||
Person person = (Person) obj;
|
||||
return id == person.id && name.equals(person.name) && age == person.age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, age);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoUnidimensionalObjectTypeArrays_whenUsingEqualsAndDeepEquals_thenBothShouldReturnTrue() {
|
||||
Object[] anArray = new Object[] { "string1", "string2", "string3" };
|
||||
Object[] anotherArray = new Object[] { "string1", "string2", "string3" };
|
||||
|
||||
assertTrue(Arrays.equals(anArray, anotherArray));
|
||||
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoUnidimensionalObjectTypeArraysWithNullElements_whenUsingEqualsAndDeepEquals_thenBothShouldReturnTrue() {
|
||||
Object[] anArray = new Object[] { "string1", null, "string3" };
|
||||
Object[] anotherArray = new Object[] { "string1", null, "string3" };
|
||||
|
||||
assertTrue(Arrays.equals(anArray, anotherArray));
|
||||
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoUnidimensionalObjectTypeArraysWithNestedElements_whenUsingEqualsAndDeepEquals_thenShouldReturnDifferently() {
|
||||
Object[] anArray = new Object[] { "string1", null, new String[] { "nestedString1", "nestedString2" } };
|
||||
Object[] anotherArray = new Object[] { "string1", null, new String[] { "nestedString1", "nestedString2" } };
|
||||
|
||||
assertFalse(Arrays.equals(anArray, anotherArray));
|
||||
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoMultidimensionalPrimitiveTypeArrays_whenUsingEqualsAndDeepEquals_thenBothShouldReturnDifferently() {
|
||||
int[][] anArray = { { 1, 2, 3 }, { 4, 5, 6, 9 }, { 7 } };
|
||||
int[][] anotherArray = { { 1, 2, 3 }, { 4, 5, 6, 9 }, { 7 } };
|
||||
|
||||
assertFalse(Arrays.equals(anArray, anotherArray));
|
||||
assertTrue(Arrays.deepEquals(anArray, anotherArray));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoMultidimensionalObjectTypeArrays_whenUsingEqualsAndDeepEquals_thenBothShouldReturnDifferently() {
|
||||
Person personArray1[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) }, { new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
||||
Person personArray2[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) }, { new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
||||
|
||||
assertFalse(Arrays.equals(personArray1, personArray2));
|
||||
assertTrue(Arrays.deepEquals(personArray1, personArray2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoMultidimensionalObjectTypeArrays_whenUsingDeepEqualsFromObjectsAndArraysClasses_thenBothShouldReturnTrue() {
|
||||
Person personArray1[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) }, { new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
||||
Person personArray2[][] = { { new Person(1, "John", 22), new Person(2, "Mike", 23) }, { new Person(3, "Steve", 27), new Person(4, "Gary", 28) } };
|
||||
|
||||
assertTrue(Objects.deepEquals(personArray1, personArray2));
|
||||
assertTrue(Arrays.deepEquals(personArray1, personArray2));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user