22
33import java .util .ArrayList ;
44import java .util .Arrays ;
5+ import java .util .Collections ;
6+ import java .util .HashSet ;
57import java .util .List ;
8+ import java .util .Set ;
69
710import org .apache .commons .collections4 .CollectionUtils ;
11+ import org .apache .commons .lang3 .ArrayUtils ;
812import org .junit .Test ;
913
1014import com .google .common .collect .Lists ;
15+ import com .google .common .collect .Sets ;
1116import com .google .common .primitives .Ints ;
1217
1318@ SuppressWarnings ("unused" )
1419public class JavaCollectionConversionUnitTest {
1520
21+ // List -> array; array -> List
22+
1623 @ Test
1724 public final void givenUsingCoreJava_whenArrayConvertedToList_thenCorrect () {
1825 final Integer [] sourceArray = { 0 , 1 , 2 , 3 , 4 , 5 };
1926 final List <Integer > targetList = Arrays .asList (sourceArray );
2027 }
2128
2229 @ Test
23- public void givenUsingCoreJava_whenListConvertedToArray_thenCorrect () {
30+ public final void givenUsingCoreJava_whenListConvertedToArray_thenCorrect () {
2431 final List <Integer > sourceList = Lists .<Integer > newArrayList (0 , 1 , 2 , 3 , 4 , 5 );
2532 final Integer [] targetArray = sourceList .toArray (new Integer [sourceList .size ()]);
2633 }
@@ -32,16 +39,69 @@ public final void givenUsingGuava_whenArrayConvertedToList_thenCorrect() {
3239 }
3340
3441 @ Test
35- public void givenUsingGuava_whenLIistConvertedToArray_thenCorrect () {
42+ public final void givenUsingGuava_whenListConvertedToArray_thenCorrect () {
3643 final List <Integer > sourceList = Lists .newArrayList (0 , 1 , 2 , 3 , 4 , 5 );
3744 final int [] targetArray = Ints .toArray (sourceList );
3845 }
3946
4047 @ Test
41- public void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect () {
48+ public final void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect () {
4249 final Integer [] sourceArray = { 0 , 1 , 2 , 3 , 4 , 5 };
4350 final List <Integer > targetList = new ArrayList <>(6 );
4451 CollectionUtils .addAll (targetList , sourceArray );
4552 }
4653
54+ // Set -> array; array -> Set
55+
56+ @ Test
57+ public final void givenUsingCoreJavaV1_whenArrayConvertedToSet_thenCorrect () {
58+ final Integer [] sourceArray = { 0 , 1 , 2 , 3 , 4 , 5 };
59+ final Set <Integer > targetSet = new HashSet <Integer >(Arrays .asList (sourceArray ));
60+ }
61+
62+ @ Test
63+ public final void givenUsingCoreJavaV2_whenArrayConvertedToSet_thenCorrect () {
64+ final Integer [] sourceArray = { 0 , 1 , 2 , 3 , 4 , 5 };
65+ final Set <Integer > targetSet = new HashSet <Integer >();
66+ Collections .addAll (targetSet , sourceArray );
67+ }
68+
69+ @ Test
70+ public final void givenUsingCoreJava_whenSetConvertedToArray_thenCorrect () {
71+ final Set <Integer > sourceSet = Sets .newHashSet (0 , 1 , 2 , 3 , 4 , 5 );
72+ final Integer [] targetArray = sourceSet .toArray (new Integer [sourceSet .size ()]);
73+ }
74+
75+ @ Test
76+ public final void givenUsingGuava_whenArrayConvertedToSet_thenCorrect () {
77+ final Integer [] sourceArray = { 0 , 1 , 2 , 3 , 4 , 5 };
78+ final Set <Integer > targetSet = Sets .newHashSet (sourceArray );
79+ }
80+
81+ @ Test
82+ public final void givenUsingGuava_whenSetConvertedToArray_thenCorrect () {
83+ final Set <Integer > sourceSet = Sets .newHashSet (0 , 1 , 2 , 3 , 4 , 5 );
84+ final int [] targetArray = Ints .toArray (sourceSet );
85+ }
86+
87+ @ Test
88+ public final void givenUsingCommonsCollections_whenArrayConvertedToSet_thenCorrect () {
89+ final Integer [] sourceArray = { 0 , 1 , 2 , 3 , 4 , 5 };
90+ final Set <Integer > targetSet = new HashSet <>(6 );
91+ CollectionUtils .addAll (targetSet , sourceArray );
92+ }
93+
94+ @ Test
95+ public final void givenUsingCommonsCollections_whenSetConvertedToArray_thenCorrect () {
96+ final Set <Integer > sourceSet = Sets .newHashSet (0 , 1 , 2 , 3 , 4 , 5 );
97+ final Integer [] targetArray = sourceSet .toArray (new Integer [sourceSet .size ()]);
98+ }
99+
100+ @ Test
101+ public final void givenUsingCommonsCollections_whenSetConvertedToArrayOfPrimitives_thenCorrect () {
102+ final Set <Integer > sourceSet = Sets .newHashSet (0 , 1 , 2 , 3 , 4 , 5 );
103+ final Integer [] targetArray = sourceSet .toArray (new Integer [sourceSet .size ()]);
104+ final int [] primitiveTargetArray = ArrayUtils .toPrimitive (targetArray );
105+ }
106+
47107}
0 commit comments