simple java conversion logic

This commit is contained in:
eugenp 2014-05-31 00:30:37 +03:00
parent 2084d90f28
commit da60bb960e
2 changed files with 48 additions and 1 deletions

View File

@ -1,4 +1,4 @@
package org.baeldung.java;
package org.baeldung.java.collections;
import java.util.ArrayList;
import java.util.Arrays;

View File

@ -0,0 +1,47 @@
package org.baeldung.java.collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
@SuppressWarnings("unused")
public class JavaCollectionConversionUnitTest {
@Test
public final void givenUsingCoreJava_whenArrayConvertedToList_thenCorrect() {
final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
final List<Integer> targetList = Arrays.asList(sourceArray);
}
@Test
public void givenUsingCoreJava_whenListConvertedToArray_thenCorrect() {
final List<Integer> sourceList = Lists.<Integer> newArrayList(0, 1, 2, 3, 4, 5);
final Integer[] targetArray = sourceList.toArray(new Integer[sourceList.size()]);
}
@Test
public final void givenUsingGuava_whenArrayConvertedToList_thenCorrect() {
final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
final List<Integer> targetList = Lists.newArrayList(sourceArray);
}
@Test
public void givenUsingGuava_whenLIistConvertedToArray_thenCorrect() {
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
final int[] targetArray = Ints.toArray(sourceList);
}
@Test
public void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect() {
final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
final List<Integer> targetList = new ArrayList<>(6);
CollectionUtils.addAll(targetList, sourceArray);
}
}