BAEL-376 - java generics code
This commit is contained in:
parent
1f0065299f
commit
2186f64126
@ -1,4 +1,7 @@
|
|||||||
|
package com.baeldung.generics;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Generics {
|
public class Generics {
|
||||||
@ -6,18 +9,14 @@ public class Generics {
|
|||||||
// definition of a generic method
|
// definition of a generic method
|
||||||
public static <T> List<T> fromArrayToList(T[] a) {
|
public static <T> List<T> fromArrayToList(T[] a) {
|
||||||
List<T> list = new ArrayList<>();
|
List<T> list = new ArrayList<>();
|
||||||
for (T t : a) {
|
Arrays.stream(a).forEach(list::add);
|
||||||
list.add(t);
|
|
||||||
}
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
// example of a generic method that has Number as an upper bound for T
|
// example of a generic method that has Number as an upper bound for T
|
||||||
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
||||||
List<T> list = new ArrayList<>();
|
List<T> list = new ArrayList<>();
|
||||||
for (T t : a) {
|
Arrays.stream(a).forEach(list::add);
|
||||||
list.add(t);
|
|
||||||
}
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,46 +1,41 @@
|
|||||||
import java.util.Iterator;
|
package com.baeldung.generics;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.hasItems;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
|
||||||
public class GenericsTest {
|
public class GenericsTest {
|
||||||
|
|
||||||
// testing the generic method with Integer
|
// testing the generic method with Integer
|
||||||
@Test
|
@Test
|
||||||
public void fromArrayToListIntTest() {
|
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
|
||||||
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
Integer[] intArray = {1, 2, 3, 4, 5};
|
||||||
List<Integer> list = Generics.fromArrayToList(intArray);
|
List<Integer> list = Generics.fromArrayToList(intArray);
|
||||||
Iterator<Integer> iterator;
|
|
||||||
iterator = list.iterator();
|
assertThat(list, hasItems(intArray));
|
||||||
while (iterator.hasNext()) {
|
|
||||||
System.out.println(iterator.next());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing the generic method with String
|
// testing the generic method with String
|
||||||
@Test
|
@Test
|
||||||
public void fromArrayToListStringTest() {
|
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
|
||||||
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
|
String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"};
|
||||||
List<String> list = Generics.fromArrayToList(stringArray);
|
List<String> list = Generics.fromArrayToList(stringArray);
|
||||||
Iterator<String> iterator;
|
|
||||||
iterator = list.iterator();
|
assertThat(list, hasItems(stringArray));
|
||||||
while (iterator.hasNext()) {
|
|
||||||
System.out.println(iterator.next());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing the generic method with Number as upper bound with Integer
|
// testing the generic method with Number as upper bound with Integer
|
||||||
// if we test fromArrayToListWithUpperBound with any type that doesn't
|
// if we test fromArrayToListWithUpperBound with any type that doesn't
|
||||||
// extend Number it will fail to compile
|
// extend Number it will fail to compile
|
||||||
@Test
|
@Test
|
||||||
public void fromArrayToListUpperboundIntTest() {
|
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
|
||||||
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
Integer[] intArray = {1, 2, 3, 4, 5};
|
||||||
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
|
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
|
||||||
Iterator<Integer> iterator;
|
|
||||||
iterator = list.iterator();
|
assertThat(list, hasItems(intArray));
|
||||||
while (iterator.hasNext()) {
|
|
||||||
System.out.println(iterator.next());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user