From 3caca638defb364835f0b9ae6a99e4805c4c99f0 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Thu, 12 Oct 2023 02:58:49 +0530 Subject: [PATCH] Review Comment Implementation --- .../streams/firstmatchingelement/User.java | 21 ++++++++ .../FirstMatchingElementUnitTest.java | 52 ++++++++++--------- 2 files changed, 48 insertions(+), 25 deletions(-) create mode 100644 core-java-modules/core-java-streams-5/src/main/java/com/baeldung/streams/firstmatchingelement/User.java diff --git a/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/streams/firstmatchingelement/User.java b/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/streams/firstmatchingelement/User.java new file mode 100644 index 0000000000..cb29af529a --- /dev/null +++ b/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/streams/firstmatchingelement/User.java @@ -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; + } + +} diff --git a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java index 43feea0074..d1ca588047 100644 --- a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java +++ b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java @@ -15,26 +15,27 @@ import com.google.common.collect.Iterables; public class FirstMatchingElementUnitTest { - private final List dataList = Lists.newArrayList("String", Boolean.TRUE, Integer.valueOf(10), Boolean.FALSE, Double.valueOf(20.0)); + private List userList = Lists.newArrayList(new User(1, "David"), new User(2, "John"), new User(3, "Roger"), new User(4, "John")); + private String searchName = "John"; @Test - public void whenCalled_thenFindIndexUsingStream() { - int index = dataList.stream() - .filter(data -> data instanceof Boolean) - .mapToInt(data -> dataList.indexOf(data)) + public void whenUsingIndexOf_thenFindFirstMatchingUserIndex() { + int index = userList.stream() + .filter(user -> searchName.equals(user.getUserName())) + .mapToInt(user -> userList.indexOf(user)) .findFirst() .orElse(-1); assertEquals(1, index); } @Test - public void whenCalled_thenFindIndexUsingStreamIterator() { + public void whenUsingIterator_thenFindFirstMatchingUserIndex() { int index = -1; - Iterator iterator = dataList.iterator(); + Iterator iterator = userList.iterator(); while (iterator.hasNext()) { - Object data = iterator.next(); - if (data instanceof Boolean) { - index = dataList.indexOf(data); + User user = iterator.next(); + if (searchName.equals(user.getUserName())) { + index = userList.indexOf(user); break; } } @@ -42,11 +43,12 @@ public class FirstMatchingElementUnitTest { } @Test - public void whenCalled_thenFindIndexUsingStreamListIterator() { + public void whenUsingListIterator_thenFindFirstMatchingUserIndex() { int index = -1; - ListIterator listIterator = dataList.listIterator(); + ListIterator listIterator = userList.listIterator(); while (listIterator.hasNext()) { - if (listIterator.next() instanceof Boolean) { + if (searchName.equals(listIterator.next() + .getUserName())) { index = listIterator.previousIndex(); break; } @@ -55,20 +57,20 @@ public class FirstMatchingElementUnitTest { } @Test - public void whenCalled_thenFindIndexUsingIntStream() { - int index = IntStream.range(0, dataList.size() - 1) - .filter(streamIndex -> dataList.get(streamIndex) instanceof Boolean) + 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 whenCalled_thenFindIndexUsingStreamTakeWhile() { - int lastIndex = dataList.size() - 1; - int predicateIndex = dataList.stream() - .takeWhile(data -> !(data instanceof Boolean)) - .mapToInt(dataList::indexOf) + public void whenUsingTakeWhile_thenFindFirstMatchingUserIndex() { + int lastIndex = userList.size() - 1; + int predicateIndex = userList.stream() + .takeWhile(user -> !(searchName.equals(user.getUserName()))) + .mapToInt(userList::indexOf) .max() .orElse(-1); @@ -80,14 +82,14 @@ public class FirstMatchingElementUnitTest { } @Test - public void whenCalled_thenFindIndexUsingGoogleGuava() { - int index = Iterables.indexOf(dataList, data -> data instanceof Boolean); + public void whenUsingGoogleGuava_thenFindFirstMatchingUserIndex() { + int index = Iterables.indexOf(userList, user -> searchName.equals(user.getUserName())); assertEquals(1, index); } @Test - public void whenCalled_thenFindIndexUsingApacheCommons() { - int index = IterableUtils.indexOf(dataList, data -> data instanceof Boolean); + public void whenUsingApacheCommons_thenFindFirstMatchingUserIndex() { + int index = IterableUtils.indexOf(userList, user -> searchName.equals(user.getUserName())); assertEquals(1, index); } }