BAEL-2562 New section in Generics article

This commit is contained in:
mikr 2019-03-10 19:28:09 +01:00 committed by ashleyfrieze
parent 94917d8755
commit bde5b5f559
2 changed files with 16 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package com.baeldung.generics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
@ -28,4 +29,10 @@ public class Generics {
buildings.forEach(Building::paint);
}
public static List<Integer> createList(int a) {
List<Integer> list = new ArrayList<>();
list.add(a);
return list;
}
}

View File

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
@ -66,4 +67,12 @@ public class GenericsUnitTest {
}
}
@Test
public void givenAnInt_whenAddedToAGenericIntegerList_thenAListItemCanBeAssignedToAnInt() {
int number = 7;
List<Integer> list = Generics.createList(number);
int otherNumber = list.get(0);
assertThat(otherNumber, is(number));
}
}