BAEL-7062 - Fetching Index from List using Streams (#14914)

* Primary Commit

* More Cases covered

* Indentation Corrections on POM

* Merge Conflict Fixed

* Updating as per Jira comments

* Correction

* Review Comment Implementation

* Indentation fixes

* Review Comment Implementation

* Method removal for Iterators

* Test Case Update

* Review Comment changes

* formatting change

* Updating map method

* Review comment update

* Review Comments

* Review Comments
This commit is contained in:
Niket Agrawal 2023-12-04 22:05:54 +05:30 committed by GitHub
parent 4b176f5549
commit e8c97fd6e5
3 changed files with 98 additions and 0 deletions

View File

@ -48,6 +48,11 @@
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>${guava.version}</version> <version>${guava.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -0,0 +1,21 @@
package com.baeldung.streams.firstmatchingelement;
public class User {
private String userName;
private Integer userId;
public User(Integer userId, String userName) {
this.userId = userId;
this.userName = userName;
}
public String getUserName() {
return userName;
}
public Integer getUserId() {
return userId;
}
}

View File

@ -0,0 +1,72 @@
package com.baeldung.streams.firstmatchingelement;
import static org.junit.Assert.assertEquals;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import org.apache.commons.collections4.IterableUtils;
import org.junit.Test;
import com.google.common.collect.Iterables;
public class FirstMatchingElementUnitTest {
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";
@Test
public void whenUsingStream_thenFindFirstMatchingUserIndex() {
AtomicInteger counter = new AtomicInteger(-1);
int index = userList.stream()
.filter(user -> {
counter.getAndIncrement();
return searchName.equals(user.getUserName());
})
.mapToInt(user -> counter.get())
.findFirst()
.orElse(-1);
assertEquals(1, index);
}
@Test
public void whenUsingIntStream_thenFindFirstMatchingUserIndex() {
int index = IntStream.range(0, userList.size() - 1)
.filter(streamIndex -> searchName.equals(userList.get(streamIndex).getUserName()))
.findFirst()
.orElse(-1);
assertEquals(1, index);
}
@Test
public void whenUsingTakeWhile_thenFindFirstMatchingUserIndex() {
long predicateIndex = userList.stream()
.takeWhile(user -> !user.getUserName().equals(searchName))
.count();
assertEquals(1, predicateIndex);
}
@Test
public void whenUsingTakeWhile_thenFindIndexFromNoMatchingElement() {
List<User> userList = List.of(new User(1, "David"), new User(2, "Vick"), new User(3, "Roger"), new User(4, "James"));
long predicateIndex = userList.stream()
.takeWhile(user -> !user.getUserName().equals(searchName))
.count();
assertEquals(4, predicateIndex);
}
@Test
public void whenUsingGoogleGuava_thenFindFirstMatchingUserIndex() {
int index = Iterables.indexOf(userList, user -> searchName.equals(user.getUserName()));
assertEquals(1, index);
}
@Test
public void whenUsingApacheCommons_thenFindFirstMatchingUserIndex() {
int index = IterableUtils.indexOf(userList, user -> searchName.equals(user.getUserName()));
assertEquals(1, index);
}
}