Skip to content

Commit 2186f64

Browse files
BAEL-376 - java generics code
1 parent 1f00652 commit 2186f64

2 files changed

Lines changed: 49 additions & 55 deletions

File tree

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1+
package com.baeldung.generics;
2+
13
import java.util.ArrayList;
4+
import java.util.Arrays;
25
import java.util.List;
36

47
public class Generics {
58

6-
// definition of a generic method
7-
public static <T> List<T> fromArrayToList(T[] a) {
8-
List<T> list = new ArrayList<>();
9-
for (T t : a) {
10-
list.add(t);
11-
}
12-
return list;
13-
}
9+
// definition of a generic method
10+
public static <T> List<T> fromArrayToList(T[] a) {
11+
List<T> list = new ArrayList<>();
12+
Arrays.stream(a).forEach(list::add);
13+
return list;
14+
}
1415

15-
// example of a generic method that has Number as an upper bound for T
16-
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
17-
List<T> list = new ArrayList<>();
18-
for (T t : a) {
19-
list.add(t);
20-
}
21-
return list;
22-
}
16+
// example of a generic method that has Number as an upper bound for T
17+
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
18+
List<T> list = new ArrayList<>();
19+
Arrays.stream(a).forEach(list::add);
20+
return list;
21+
}
2322

2423
}
Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
1-
import java.util.Iterator;
2-
import java.util.List;
1+
package com.baeldung.generics;
32

43
import org.junit.Test;
54

5+
import java.util.List;
6+
7+
import static org.hamcrest.CoreMatchers.hasItems;
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
610
public class GenericsTest {
711

8-
// testing the generic method with Integer
9-
@Test
10-
public void fromArrayToListIntTest() {
11-
Integer[] intArray = { 1, 2, 3, 4, 5 };
12-
List<Integer> list = Generics.fromArrayToList(intArray);
13-
Iterator<Integer> iterator;
14-
iterator = list.iterator();
15-
while (iterator.hasNext()) {
16-
System.out.println(iterator.next());
17-
}
18-
}
19-
20-
// testing the generic method with String
21-
@Test
22-
public void fromArrayToListStringTest() {
23-
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
24-
List<String> list = Generics.fromArrayToList(stringArray);
25-
Iterator<String> iterator;
26-
iterator = list.iterator();
27-
while (iterator.hasNext()) {
28-
System.out.println(iterator.next());
29-
}
30-
}
31-
32-
// testing the generic method with Number as upper bound with Integer
33-
// if we test fromArrayToListWithUpperBound with any type that doesn't
34-
// extend Number it will fail to compile
35-
@Test
36-
public void fromArrayToListUpperboundIntTest() {
37-
Integer[] intArray = { 1, 2, 3, 4, 5 };
38-
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
39-
Iterator<Integer> iterator;
40-
iterator = list.iterator();
41-
while (iterator.hasNext()) {
42-
System.out.println(iterator.next());
43-
}
44-
}
12+
// testing the generic method with Integer
13+
@Test
14+
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
15+
Integer[] intArray = {1, 2, 3, 4, 5};
16+
List<Integer> list = Generics.fromArrayToList(intArray);
17+
18+
assertThat(list, hasItems(intArray));
19+
}
20+
21+
// testing the generic method with String
22+
@Test
23+
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
24+
String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"};
25+
List<String> list = Generics.fromArrayToList(stringArray);
26+
27+
assertThat(list, hasItems(stringArray));
28+
}
29+
30+
// testing the generic method with Number as upper bound with Integer
31+
// if we test fromArrayToListWithUpperBound with any type that doesn't
32+
// extend Number it will fail to compile
33+
@Test
34+
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
35+
Integer[] intArray = {1, 2, 3, 4, 5};
36+
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
37+
38+
assertThat(list, hasItems(intArray));
39+
}
4540

4641
}

0 commit comments

Comments
 (0)