Review Comment Implementation
This commit is contained in:
parent
a78b189d69
commit
3caca638de
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,26 +15,27 @@ import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
public class FirstMatchingElementUnitTest {
|
public class FirstMatchingElementUnitTest {
|
||||||
|
|
||||||
private final List<Object> dataList = Lists.newArrayList("String", Boolean.TRUE, Integer.valueOf(10), Boolean.FALSE, Double.valueOf(20.0));
|
private List<User> userList = Lists.newArrayList(new User(1, "David"), new User(2, "John"), new User(3, "Roger"), new User(4, "John"));
|
||||||
|
private String searchName = "John";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalled_thenFindIndexUsingStream() {
|
public void whenUsingIndexOf_thenFindFirstMatchingUserIndex() {
|
||||||
int index = dataList.stream()
|
int index = userList.stream()
|
||||||
.filter(data -> data instanceof Boolean)
|
.filter(user -> searchName.equals(user.getUserName()))
|
||||||
.mapToInt(data -> dataList.indexOf(data))
|
.mapToInt(user -> userList.indexOf(user))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(-1);
|
.orElse(-1);
|
||||||
assertEquals(1, index);
|
assertEquals(1, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalled_thenFindIndexUsingStreamIterator() {
|
public void whenUsingIterator_thenFindFirstMatchingUserIndex() {
|
||||||
int index = -1;
|
int index = -1;
|
||||||
Iterator<Object> iterator = dataList.iterator();
|
Iterator<User> iterator = userList.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
Object data = iterator.next();
|
User user = iterator.next();
|
||||||
if (data instanceof Boolean) {
|
if (searchName.equals(user.getUserName())) {
|
||||||
index = dataList.indexOf(data);
|
index = userList.indexOf(user);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,11 +43,12 @@ public class FirstMatchingElementUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalled_thenFindIndexUsingStreamListIterator() {
|
public void whenUsingListIterator_thenFindFirstMatchingUserIndex() {
|
||||||
int index = -1;
|
int index = -1;
|
||||||
ListIterator<Object> listIterator = dataList.listIterator();
|
ListIterator<User> listIterator = userList.listIterator();
|
||||||
while (listIterator.hasNext()) {
|
while (listIterator.hasNext()) {
|
||||||
if (listIterator.next() instanceof Boolean) {
|
if (searchName.equals(listIterator.next()
|
||||||
|
.getUserName())) {
|
||||||
index = listIterator.previousIndex();
|
index = listIterator.previousIndex();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -55,20 +57,20 @@ public class FirstMatchingElementUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalled_thenFindIndexUsingIntStream() {
|
public void whenUsingIntStream_thenFindFirstMatchingUserIndex() {
|
||||||
int index = IntStream.range(0, dataList.size() - 1)
|
int index = IntStream.range(0, userList.size() - 1)
|
||||||
.filter(streamIndex -> dataList.get(streamIndex) instanceof Boolean)
|
.filter(streamIndex -> searchName.equals(userList.get(streamIndex).getUserName()))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(-1);
|
.orElse(-1);
|
||||||
assertEquals(1, index);
|
assertEquals(1, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalled_thenFindIndexUsingStreamTakeWhile() {
|
public void whenUsingTakeWhile_thenFindFirstMatchingUserIndex() {
|
||||||
int lastIndex = dataList.size() - 1;
|
int lastIndex = userList.size() - 1;
|
||||||
int predicateIndex = dataList.stream()
|
int predicateIndex = userList.stream()
|
||||||
.takeWhile(data -> !(data instanceof Boolean))
|
.takeWhile(user -> !(searchName.equals(user.getUserName())))
|
||||||
.mapToInt(dataList::indexOf)
|
.mapToInt(userList::indexOf)
|
||||||
.max()
|
.max()
|
||||||
.orElse(-1);
|
.orElse(-1);
|
||||||
|
|
||||||
|
@ -80,14 +82,14 @@ public class FirstMatchingElementUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalled_thenFindIndexUsingGoogleGuava() {
|
public void whenUsingGoogleGuava_thenFindFirstMatchingUserIndex() {
|
||||||
int index = Iterables.indexOf(dataList, data -> data instanceof Boolean);
|
int index = Iterables.indexOf(userList, user -> searchName.equals(user.getUserName()));
|
||||||
assertEquals(1, index);
|
assertEquals(1, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCalled_thenFindIndexUsingApacheCommons() {
|
public void whenUsingApacheCommons_thenFindFirstMatchingUserIndex() {
|
||||||
int index = IterableUtils.indexOf(dataList, data -> data instanceof Boolean);
|
int index = IterableUtils.indexOf(userList, user -> searchName.equals(user.getUserName()));
|
||||||
assertEquals(1, index);
|
assertEquals(1, index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue