Review Comments

This commit is contained in:
Niket Agrawal 2023-11-23 20:54:11 +05:30
parent aa357f8b64
commit 39f5de4e30
1 changed files with 8 additions and 14 deletions

View File

@ -1,20 +1,20 @@
package com.baeldung.streams.firstmatchingelement; package com.baeldung.streams.firstmatchingelement;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.apache.commons.collections4.IterableUtils; import org.apache.commons.collections4.IterableUtils;
import org.assertj.core.util.Lists;
import org.junit.Test; import org.junit.Test;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
public class FirstMatchingElementUnitTest { public class FirstMatchingElementUnitTest {
private List<User> userList = Lists.newArrayList(new User(1, "David"), new User(2, "John"), new User(3, "Roger"), new User(4, "John")); private List<User> userList = List.of(new User(1, "David"), new User(2, "John"), new User(3, "Roger"), new User(4, "John"));
private String searchName = "John"; private String searchName = "John";
@Test @Test
@ -43,17 +43,11 @@ public class FirstMatchingElementUnitTest {
@Test @Test
public void whenUsingTakeWhile_thenFindFirstMatchingUserIndex() { public void whenUsingTakeWhile_thenFindFirstMatchingUserIndex() {
AtomicInteger counter = new AtomicInteger(-1); long predicateIndex = userList.stream()
int predicateIndex = userList.stream() .takeWhile(user -> !user.getUserName()
.takeWhile(user -> { .equals(searchName))
counter.getAndIncrement(); .count();
return !(searchName.equals(user.getUserName())); assertEquals(1, predicateIndex);
})
.mapToInt(user -> counter.get())
.max()
.orElse(-1);
assertEquals(1, predicateIndex + 1);
} }
@Test @Test