Skip to content

Commit c3eb2b0

Browse files
JAVA-5683: Difference Between List.of and Arrays.asList (eugenp#12564)
Co-authored-by: Harpal Singh <[email protected]>
1 parent cf5c2be commit c3eb2b0

5 files changed

Lines changed: 127 additions & 2 deletions

File tree

core-java-modules/core-java-collections-list-4/pom.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@
6464
<colt.version>1.2.0</colt.version>
6565
<apache-commons.version>3.0</apache-commons.version>
6666
<assertj.version>3.22.0</assertj.version>
67+
<maven.compiler.source.version>17</maven.compiler.source.version>
68+
<maven.compiler.target.version>17</maven.compiler.target.version>
69+
6770
</properties>
71+
72+
<build>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-compiler-plugin</artifactId>
77+
<version>${maven-compiler-plugin.version}</version>
78+
<configuration>
79+
<source>${maven.compiler.source.version}</source>
80+
<target>${maven.compiler.target.version}</target>
81+
</configuration>
82+
</plugin>
83+
</plugins>
84+
</build>
6885

69-
</project>
86+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.list.creation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
11+
class ArraysAsListUnitTest {
12+
13+
@Test
14+
void givenAnArrayOfIntegersWhenCreatingAListUsingArraysAsListThenItWillHaveTheSameElementsInTheSameOrder() {
15+
Integer[] array = new Integer[]{1, 2, 3, 4};
16+
List<Integer> list = Arrays.asList(array);
17+
assertThat(list).containsExactly(1, 2, 3, 4);
18+
}
19+
20+
@Test
21+
void givenAnListOfIntegersCreatedUsingArraysAsListWhenChangingTheOriginalArrayTheReturnListWillAlsoChange() {
22+
Integer[] array = new Integer[]{1, 2, 3};
23+
List<Integer> list = Arrays.asList(array);
24+
array[0] = 1000;
25+
assertThat(list.get(0)).isEqualTo(1000);
26+
}
27+
28+
@Test
29+
void givenAnListOfIntegersCreatedUsingArraysAsListWhenChangingTheReturnedListTheOriginalArrayWillAlsoChange() {
30+
Integer[] array = new Integer[]{1, 2, 3};
31+
List<Integer> list = Arrays.asList(array);
32+
list.set(0, 1000);
33+
assertThat(array[0]).isEqualTo(1000);
34+
}
35+
36+
@Test
37+
void givenAnArrayOfIntegersCreatedUsingArraysAsListWhenModifyingAnExistingElementThenModifyIt() {
38+
List<Integer> list = Arrays.asList(1, 2, 3, 4);
39+
list.set(1, 1000);
40+
assertThat(list.get(1)).isEqualTo(1000);
41+
}
42+
43+
@Test
44+
void givenAnArrayCreatedWithArraysAsListWhenAddingANewElementThenUnsupportedOperationExceptionIsThrown() {
45+
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
46+
assertThrows(UnsupportedOperationException.class, () -> list.add(6));
47+
}
48+
49+
@Test
50+
void givenAnArrayCreatedWithArraysAsListWhenRemovingAnExistingElementThenUnsupportedOperationExceptionIsThrown() {
51+
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
52+
assertThrows(UnsupportedOperationException.class, () -> list.remove(1));
53+
}
54+
55+
56+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.list.creation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
10+
class ListOfUnitTest {
11+
12+
@Test
13+
void givenAnArrayOfStringWhenCreatingAListUsingItsValuesThenItWillHaveTheSameElementsInTheSameOrder() {
14+
String[] array = new String[]{"one", "two", "three"};
15+
List<String> list = List.of(array);
16+
assertThat(list).containsExactly("one", "two", "three");
17+
}
18+
19+
@Test
20+
void givenAnListOfStringCreatedUsingListOfWhenChangingTheOriginalArrayTheReturnListWontChange() {
21+
String[] array = new String[]{"one", "two", "three"};
22+
List<String> list = List.of(array);
23+
array[0] = "thousand";
24+
assertThat(list.get(0)).isEqualTo("one");
25+
}
26+
27+
@Test
28+
void givenAnArrayCreatedWithListOfWhenAddingANewElementThenUnsupportedOperationExceptionIsThrown() {
29+
List<String> list = List.of("one", "two", "three");
30+
assertThrows(UnsupportedOperationException.class, () -> list.add("four"));
31+
}
32+
33+
@Test
34+
void givenAnArrayCreatedWithListOfWhenRemovingAnExistingElementThenUnsupportedOperationExceptionIsThrown() {
35+
List<String> list = List.of("one", "two", "three");
36+
assertThrows(UnsupportedOperationException.class, () -> list.remove("two"));
37+
}
38+
39+
@Test
40+
void givenAnArrayCreatedWithListOfWhenModifyingAnExistingElementThenUnsupportedOperationExceptionIsThrown() {
41+
List<String> list = List.of("one", "two", "three");
42+
assertThrows(UnsupportedOperationException.class, () -> list.set(1, "four"));
43+
}
44+
45+
@Test
46+
void givenAnArrayContainingNullElementWhenUsingItToCreateListThenNullPointerExceptionIsThrown() {
47+
assertThrows(NullPointerException.class, () -> List.of("one", null, "two"));
48+
}
49+
50+
51+
}

core-java-modules/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<module>core-java-collections-list</module>
3838
<module>core-java-collections-list-2</module>
3939
<module>core-java-collections-list-3</module>
40-
<module>core-java-collections-list-4</module>
4140
<module>core-java-collections-maps</module>
4241
<module>core-java-collections-maps-2</module>
4342
<module>core-java-collections-maps-3</module>

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,7 @@
11611161
<!-- <module>core-java-modules/core-java-16</module> --> <!-- uses preview features, to be decided how to handle -->
11621162
<!-- <module>core-java-modules/core-java-17</module> --> <!-- uses preview features, to be decided how to handle -->
11631163
<module>core-java-modules/core-java-collections-set</module>
1164+
<module>core-java-modules/core-java-collections-list-4</module>
11641165
<module>core-java-modules/core-java-collections-maps-4</module>
11651166
<module>core-java-modules/core-java-date-operations-1</module>
11661167
<module>core-java-modules/core-java-datetime-conversion</module>
@@ -1234,6 +1235,7 @@
12341235
<!-- <module>core-java-modules/core-java-16</module> --> <!-- uses preview features, to be decided how to handle -->
12351236
<!-- <module>core-java-modules/core-java-17</module> --> <!-- uses preview features, to be decided how to handle -->
12361237
<module>core-java-modules/core-java-collections-set</module>
1238+
<module>core-java-modules/core-java-collections-list-4</module>
12371239
<module>core-java-modules/core-java-collections-maps-4</module>
12381240
<module>core-java-modules/core-java-date-operations-1</module>
12391241
<module>core-java-modules/core-java-datetime-conversion</module>

0 commit comments

Comments
 (0)