Cleanup and function references

This commit is contained in:
mrsoto 2019-12-14 22:03:27 -03:00
parent d80152c450
commit 421d15b132
3 changed files with 11 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import org.apache.commons.collections4.MapUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ConvertListToMapService {
@ -21,7 +22,7 @@ public class ConvertListToMapService {
}
public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) {
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal));
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, Function.identity()));
return map;
}

View File

@ -53,14 +53,14 @@ public class CollectionToArrayListUnitTest {
verifyShallowCopy(srcCollection, newList);
}
/**
* Section 5. Deep Copy
*/
@Test
public void whenUsingDeepCopy_thenVerifyDeepCopy() {
ArrayList<Foo> newList = srcCollection.stream()
.map(foo -> foo.deepCopy())
.map(Foo::deepCopy)
.collect(toCollection(ArrayList::new));
verifyDeepCopy(srcCollection, newList);
@ -83,13 +83,13 @@ public class CollectionToArrayListUnitTest {
* @param a
* @param b
*/
private void verifyShallowCopy(Collection a, Collection b) {
private void verifyShallowCopy(Collection<Foo> a, Collection<Foo> b) {
assertEquals("Collections have different lengths", a.size(), b.size());
Iterator<Foo> iterA = a.iterator();
Iterator<Foo> iterB = b.iterator();
while (iterA.hasNext()) {
// use '==' to test instance identity
assertTrue("Foo instances differ!", iterA.next() == iterB.next());
assertSame("Foo instances differ!", iterA.next(), iterB.next());
}
}
@ -98,7 +98,7 @@ public class CollectionToArrayListUnitTest {
* @param a
* @param b
*/
private void verifyDeepCopy(Collection a, Collection b) {
private void verifyDeepCopy(Collection<Foo> a, Collection<Foo> b) {
assertEquals("Collections have different lengths", a.size(), b.size());
Iterator<Foo> iterA = a.iterator();
Iterator<Foo> iterB = b.iterator();
@ -106,7 +106,7 @@ public class CollectionToArrayListUnitTest {
Foo nextA = iterA.next();
Foo nextB = iterB.next();
// should not be same instance
assertFalse("Foo instances are the same!", nextA == nextB);
assertNotSame("Foo instances are the same!", nextA, nextB);
// but should have same content
assertFalse("Foo instances have different content!", fooDiff(nextA, nextB));
}

View File

@ -23,7 +23,7 @@ public class ConvertIteratorToListServiceUnitTest {
Iterator<Integer> iterator;
@Before
public void setUp() throws Exception {
public void setUp() {
iterator = Arrays.asList(1, 2, 3)
.iterator();
}
@ -31,7 +31,7 @@ public class ConvertIteratorToListServiceUnitTest {
@Test
public void givenAnIterator_whenConvertIteratorToListUsingWhileLoop_thenReturnAList() {
List<Integer> actualList = new ArrayList<Integer>();
List<Integer> actualList = new ArrayList<>();
// Convert Iterator to List using while loop dsf
while (iterator.hasNext()) {
@ -44,7 +44,7 @@ public class ConvertIteratorToListServiceUnitTest {
@Test
public void givenAnIterator_whenConvertIteratorToListAfterJava8_thenReturnAList() {
List<Integer> actualList = new ArrayList<Integer>();
List<Integer> actualList = new ArrayList<>();
// Convert Iterator to List using Java 8
iterator.forEachRemaining(actualList::add);