BAEL-3476: Add Java 9 example to Immutable ArrayList in Java (#8636)
This commit is contained in:
parent
fd17eaac04
commit
cdac6f117a
|
@ -9,6 +9,7 @@ This module contains articles about Java 9 core features
|
||||||
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
|
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
|
||||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||||
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
|
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
|
||||||
|
- [Immutable ArrayList in Java](https://www.baeldung.com/java-immutable-list)
|
||||||
|
|
||||||
Note: also contains part of the code for the article
|
Note: also contains part of the code for the article
|
||||||
[How to Filter a Collection in Java](https://www.baeldung.com/java-collection-filtering).
|
[How to Filter a Collection in Java](https://www.baeldung.com/java-collection-filtering).
|
||||||
|
|
|
@ -37,6 +37,11 @@
|
||||||
<version>${junit.platform.version}</version>
|
<version>${junit.platform.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-collections4</artifactId>
|
||||||
|
<version>${commons-collections4.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -69,6 +74,7 @@
|
||||||
<maven.compiler.source>1.9</maven.compiler.source>
|
<maven.compiler.source>1.9</maven.compiler.source>
|
||||||
<maven.compiler.target>1.9</maven.compiler.target>
|
<maven.compiler.target>1.9</maven.compiler.target>
|
||||||
<guava.version>25.1-jre</guava.version>
|
<guava.version>25.1-jre</guava.version>
|
||||||
|
<commons-collections4.version>4.1</commons-collections4.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.baeldung.java9.list.immutable;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import org.apache.commons.collections4.ListUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ImmutableArrayListUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public final void givenUsingTheJdk_whenUnmodifiableListIsCreated_thenNotModifiable() {
|
||||||
|
final List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
|
||||||
|
final List<String> unmodifiableList = Collections.unmodifiableList(list);
|
||||||
|
unmodifiableList.add("four");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public final void givenUsingTheJava9_whenUnmodifiableListIsCreated_thenNotModifiable() {
|
||||||
|
final List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
|
||||||
|
final List<String> unmodifiableList = List.of(list.toArray(new String[]{}));
|
||||||
|
unmodifiableList.add("four");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public final void givenUsingGuava_whenUnmodifiableListIsCreated_thenNotModifiable() {
|
||||||
|
final List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
|
||||||
|
final List<String> unmodifiableList = ImmutableList.copyOf(list);
|
||||||
|
unmodifiableList.add("four");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public final void givenUsingGuavaBuilder_whenUnmodifiableListIsCreated_thenNoLongerModifiable() {
|
||||||
|
final List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
|
||||||
|
final ImmutableList<String> unmodifiableList = ImmutableList.<String>builder().addAll(list).build();
|
||||||
|
unmodifiableList.add("four");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public final void givenUsingCommonsCollections_whenUnmodifiableListIsCreated_thenNotModifiable() {
|
||||||
|
final List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
|
||||||
|
final List<String> unmodifiableList = ListUtils.unmodifiableList(list);
|
||||||
|
unmodifiableList.add("four");
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,9 +3,8 @@
|
||||||
This module contains articles about the Java ArrayList collection
|
This module contains articles about the Java ArrayList collection
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Immutable ArrayList in Java](http://www.baeldung.com/java-immutable-list)
|
- [Guide to the Java ArrayList](https://www.baeldung.com/java-arraylist)
|
||||||
- [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist)
|
- [Add Multiple Items to an Java ArrayList](https://www.baeldung.com/java-add-items-array-list)
|
||||||
- [Add Multiple Items to an Java ArrayList](http://www.baeldung.com/java-add-items-array-list)
|
|
||||||
- [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist)
|
- [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist)
|
||||||
- [Multi Dimensional ArrayList in Java](https://www.baeldung.com/java-multi-dimensional-arraylist)
|
- [Multi Dimensional ArrayList in Java](https://www.baeldung.com/java-multi-dimensional-arraylist)
|
||||||
- [Removing an Element From an ArrayList](https://www.baeldung.com/java-arraylist-remove-element)
|
- [Removing an Element From an ArrayList](https://www.baeldung.com/java-arraylist-remove-element)
|
||||||
|
|
|
@ -15,42 +15,11 @@ public class CoreJavaCollectionsUnitTest {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(CoreJavaCollectionsUnitTest.class);
|
private static final Logger LOG = LoggerFactory.getLogger(CoreJavaCollectionsUnitTest.class);
|
||||||
|
|
||||||
|
|
||||||
// tests -
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingTheJdk_whenArrayListIsSynchronized_thenCorrect() {
|
public final void givenUsingTheJdk_whenArrayListIsSynchronized_thenCorrect() {
|
||||||
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
|
final List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
|
||||||
final List<String> synchronizedList = Collections.synchronizedList(list);
|
final List<String> synchronizedList = Collections.synchronizedList(list);
|
||||||
LOG.debug("Synchronized List is: " + synchronizedList);
|
LOG.debug("Synchronized List is: " + synchronizedList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = UnsupportedOperationException.class)
|
|
||||||
public final void givenUsingTheJdk_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
|
|
||||||
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
|
|
||||||
final List<String> unmodifiableList = Collections.unmodifiableList(list);
|
|
||||||
unmodifiableList.add("four");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = UnsupportedOperationException.class)
|
|
||||||
public final void givenUsingGuava_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
|
|
||||||
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
|
|
||||||
final List<String> unmodifiableList = ImmutableList.copyOf(list);
|
|
||||||
unmodifiableList.add("four");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = UnsupportedOperationException.class)
|
|
||||||
public final void givenUsingGuavaBuilder_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
|
|
||||||
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
|
|
||||||
final ImmutableList<String> unmodifiableList = ImmutableList.<String>builder().addAll(list).build();
|
|
||||||
unmodifiableList.add("four");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = UnsupportedOperationException.class)
|
|
||||||
public final void givenUsingCommonsCollections_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
|
|
||||||
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
|
|
||||||
final List<String> unmodifiableList = ListUtils.unmodifiableList(list);
|
|
||||||
unmodifiableList.add("four");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue