collections work

This commit is contained in:
eugenp 2013-12-24 16:46:15 +02:00
parent d9b9c30aa2
commit 22ebf4a218
2 changed files with 41 additions and 3 deletions

View File

@ -64,7 +64,7 @@
</dependencies>
<build>
<finalName>jackson</finalName>
<finalName>core-java</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>

View File

@ -1,7 +1,45 @@
package org.baeldung.java;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.collections4.ListUtils;
import org.junit.Test;
import com.google.common.collect.ImmutableList;
public class CoreJavaUnitTest {
// tests -
@Test
public final void givenUsingTheJdk_whenArrayListIsSynchronized_thenCorrect() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> synchronizedList = Collections.synchronizedList(list);
System.out.println("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 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");
}
}