JAVA-5683: Difference Between List.of and Arrays.asList (#12564)

Co-authored-by: Harpal Singh <harpal.singh@kaleyra.com>
This commit is contained in:
Harry9656 2022-08-23 23:22:43 +02:00 committed by GitHub
parent d03fb7e217
commit 77671ade4b
5 changed files with 127 additions and 2 deletions

View File

@ -64,6 +64,23 @@
<colt.version>1.2.0</colt.version> <colt.version>1.2.0</colt.version>
<apache-commons.version>3.0</apache-commons.version> <apache-commons.version>3.0</apache-commons.version>
<assertj.version>3.22.0</assertj.version> <assertj.version>3.22.0</assertj.version>
<maven.compiler.source.version>17</maven.compiler.source.version>
<maven.compiler.target.version>17</maven.compiler.target.version>
</properties> </properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -0,0 +1,56 @@
package com.baeldung.list.creation;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class ArraysAsListUnitTest {
@Test
void givenAnArrayOfIntegersWhenCreatingAListUsingArraysAsListThenItWillHaveTheSameElementsInTheSameOrder() {
Integer[] array = new Integer[]{1, 2, 3, 4};
List<Integer> list = Arrays.asList(array);
assertThat(list).containsExactly(1, 2, 3, 4);
}
@Test
void givenAnListOfIntegersCreatedUsingArraysAsListWhenChangingTheOriginalArrayTheReturnListWillAlsoChange() {
Integer[] array = new Integer[]{1, 2, 3};
List<Integer> list = Arrays.asList(array);
array[0] = 1000;
assertThat(list.get(0)).isEqualTo(1000);
}
@Test
void givenAnListOfIntegersCreatedUsingArraysAsListWhenChangingTheReturnedListTheOriginalArrayWillAlsoChange() {
Integer[] array = new Integer[]{1, 2, 3};
List<Integer> list = Arrays.asList(array);
list.set(0, 1000);
assertThat(array[0]).isEqualTo(1000);
}
@Test
void givenAnArrayOfIntegersCreatedUsingArraysAsListWhenModifyingAnExistingElementThenModifyIt() {
List<Integer> list = Arrays.asList(1, 2, 3, 4);
list.set(1, 1000);
assertThat(list.get(1)).isEqualTo(1000);
}
@Test
void givenAnArrayCreatedWithArraysAsListWhenAddingANewElementThenUnsupportedOperationExceptionIsThrown() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
assertThrows(UnsupportedOperationException.class, () -> list.add(6));
}
@Test
void givenAnArrayCreatedWithArraysAsListWhenRemovingAnExistingElementThenUnsupportedOperationExceptionIsThrown() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
assertThrows(UnsupportedOperationException.class, () -> list.remove(1));
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.list.creation;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class ListOfUnitTest {
@Test
void givenAnArrayOfStringWhenCreatingAListUsingItsValuesThenItWillHaveTheSameElementsInTheSameOrder() {
String[] array = new String[]{"one", "two", "three"};
List<String> list = List.of(array);
assertThat(list).containsExactly("one", "two", "three");
}
@Test
void givenAnListOfStringCreatedUsingListOfWhenChangingTheOriginalArrayTheReturnListWontChange() {
String[] array = new String[]{"one", "two", "three"};
List<String> list = List.of(array);
array[0] = "thousand";
assertThat(list.get(0)).isEqualTo("one");
}
@Test
void givenAnArrayCreatedWithListOfWhenAddingANewElementThenUnsupportedOperationExceptionIsThrown() {
List<String> list = List.of("one", "two", "three");
assertThrows(UnsupportedOperationException.class, () -> list.add("four"));
}
@Test
void givenAnArrayCreatedWithListOfWhenRemovingAnExistingElementThenUnsupportedOperationExceptionIsThrown() {
List<String> list = List.of("one", "two", "three");
assertThrows(UnsupportedOperationException.class, () -> list.remove("two"));
}
@Test
void givenAnArrayCreatedWithListOfWhenModifyingAnExistingElementThenUnsupportedOperationExceptionIsThrown() {
List<String> list = List.of("one", "two", "three");
assertThrows(UnsupportedOperationException.class, () -> list.set(1, "four"));
}
@Test
void givenAnArrayContainingNullElementWhenUsingItToCreateListThenNullPointerExceptionIsThrown() {
assertThrows(NullPointerException.class, () -> List.of("one", null, "two"));
}
}

View File

@ -37,7 +37,6 @@
<module>core-java-collections-list</module> <module>core-java-collections-list</module>
<module>core-java-collections-list-2</module> <module>core-java-collections-list-2</module>
<module>core-java-collections-list-3</module> <module>core-java-collections-list-3</module>
<module>core-java-collections-list-4</module>
<module>core-java-collections-maps</module> <module>core-java-collections-maps</module>
<module>core-java-collections-maps-2</module> <module>core-java-collections-maps-2</module>
<module>core-java-collections-maps-3</module> <module>core-java-collections-maps-3</module>

View File

@ -1161,6 +1161,7 @@
<!-- <module>core-java-modules/core-java-16</module> --> <!-- uses preview features, to be decided how to handle --> <!-- <module>core-java-modules/core-java-16</module> --> <!-- uses preview features, to be decided how to handle -->
<!-- <module>core-java-modules/core-java-17</module> --> <!-- uses preview features, to be decided how to handle --> <!-- <module>core-java-modules/core-java-17</module> --> <!-- uses preview features, to be decided how to handle -->
<module>core-java-modules/core-java-collections-set</module> <module>core-java-modules/core-java-collections-set</module>
<module>core-java-modules/core-java-collections-list-4</module>
<module>core-java-modules/core-java-collections-maps-4</module> <module>core-java-modules/core-java-collections-maps-4</module>
<module>core-java-modules/core-java-date-operations-1</module> <module>core-java-modules/core-java-date-operations-1</module>
<module>core-java-modules/core-java-datetime-conversion</module> <module>core-java-modules/core-java-datetime-conversion</module>
@ -1234,6 +1235,7 @@
<!-- <module>core-java-modules/core-java-16</module> --> <!-- uses preview features, to be decided how to handle --> <!-- <module>core-java-modules/core-java-16</module> --> <!-- uses preview features, to be decided how to handle -->
<!-- <module>core-java-modules/core-java-17</module> --> <!-- uses preview features, to be decided how to handle --> <!-- <module>core-java-modules/core-java-17</module> --> <!-- uses preview features, to be decided how to handle -->
<module>core-java-modules/core-java-collections-set</module> <module>core-java-modules/core-java-collections-set</module>
<module>core-java-modules/core-java-collections-list-4</module>
<module>core-java-modules/core-java-collections-maps-4</module> <module>core-java-modules/core-java-collections-maps-4</module>
<module>core-java-modules/core-java-date-operations-1</module> <module>core-java-modules/core-java-date-operations-1</module>
<module>core-java-modules/core-java-datetime-conversion</module> <module>core-java-modules/core-java-datetime-conversion</module>