Skip to content

Commit c1c90f7

Browse files
committed
Cleanup and function references
1 parent 540bd04 commit c1c90f7

3 files changed

Lines changed: 11 additions & 10 deletions

File tree

java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.HashMap;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.function.Function;
910
import java.util.stream.Collectors;
1011

1112
public class ConvertListToMapService {
@@ -21,7 +22,7 @@ public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
2122
}
2223

2324
public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) {
24-
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal));
25+
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, Function.identity()));
2526
return map;
2627
}
2728

java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public void whenUsingStream_thenVerifyShallowCopy() {
5353

5454
verifyShallowCopy(srcCollection, newList);
5555
}
56-
56+
5757
/**
5858
* Section 5. Deep Copy
5959
*/
6060
@Test
6161
public void whenUsingDeepCopy_thenVerifyDeepCopy() {
6262
ArrayList<Foo> newList = srcCollection.stream()
63-
.map(foo -> foo.deepCopy())
63+
.map(Foo::deepCopy)
6464
.collect(toCollection(ArrayList::new));
6565

6666
verifyDeepCopy(srcCollection, newList);
@@ -83,13 +83,13 @@ public void whenUsingSortedStream_thenVerifySortOrder() {
8383
* @param a
8484
* @param b
8585
*/
86-
private void verifyShallowCopy(Collection a, Collection b) {
86+
private void verifyShallowCopy(Collection<Foo> a, Collection<Foo> b) {
8787
assertEquals("Collections have different lengths", a.size(), b.size());
8888
Iterator<Foo> iterA = a.iterator();
8989
Iterator<Foo> iterB = b.iterator();
9090
while (iterA.hasNext()) {
9191
// use '==' to test instance identity
92-
assertTrue("Foo instances differ!", iterA.next() == iterB.next());
92+
assertSame("Foo instances differ!", iterA.next(), iterB.next());
9393
}
9494
}
9595

@@ -98,15 +98,15 @@ private void verifyShallowCopy(Collection a, Collection b) {
9898
* @param a
9999
* @param b
100100
*/
101-
private void verifyDeepCopy(Collection a, Collection b) {
101+
private void verifyDeepCopy(Collection<Foo> a, Collection<Foo> b) {
102102
assertEquals("Collections have different lengths", a.size(), b.size());
103103
Iterator<Foo> iterA = a.iterator();
104104
Iterator<Foo> iterB = b.iterator();
105105
while (iterA.hasNext()) {
106106
Foo nextA = iterA.next();
107107
Foo nextB = iterB.next();
108108
// should not be same instance
109-
assertFalse("Foo instances are the same!", nextA == nextB);
109+
assertNotSame("Foo instances are the same!", nextA, nextB);
110110
// but should have same content
111111
assertFalse("Foo instances have different content!", fooDiff(nextA, nextB));
112112
}

java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public class ConvertIteratorToListServiceUnitTest {
2323
Iterator<Integer> iterator;
2424

2525
@Before
26-
public void setUp() throws Exception {
26+
public void setUp() {
2727
iterator = Arrays.asList(1, 2, 3)
2828
.iterator();
2929
}
3030

3131
@Test
3232
public void givenAnIterator_whenConvertIteratorToListUsingWhileLoop_thenReturnAList() {
3333

34-
List<Integer> actualList = new ArrayList<Integer>();
34+
List<Integer> actualList = new ArrayList<>();
3535

3636
// Convert Iterator to List using while loop dsf
3737
while (iterator.hasNext()) {
@@ -44,7 +44,7 @@ public void givenAnIterator_whenConvertIteratorToListUsingWhileLoop_thenReturnAL
4444

4545
@Test
4646
public void givenAnIterator_whenConvertIteratorToListAfterJava8_thenReturnAList() {
47-
List<Integer> actualList = new ArrayList<Integer>();
47+
List<Integer> actualList = new ArrayList<>();
4848

4949
// Convert Iterator to List using Java 8
5050
iterator.forEachRemaining(actualList::add);

0 commit comments

Comments
 (0)