Skip to content

Commit 68771f4

Browse files
authored
BAEL-9449 CollectionUtils.exists (#18810)
* Update FindACustomerInGivenList.java * Update FindACustomerInGivenListUnitTest.java * Update FindACustomerInGivenList.java
1 parent 7467016 commit 68771f4

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

core-java-modules/core-java-collections-list/src/main/java/com/baeldung/findanelement/FindACustomerInGivenList.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.List;
55

66
import org.apache.commons.collections4.IterableUtils;
7-
7+
import org.apache.commons.collections4.CollectionUtils;
88
import com.google.common.base.Predicate;
99
import com.google.common.collect.Iterables;
1010

@@ -74,4 +74,11 @@ public boolean evaluate(Customer customer) {
7474
});
7575
}
7676

77-
}
77+
public boolean findUsingExists(String name, List<Customer> customers) {
78+
return CollectionUtils.exists(customers, new org.apache.commons.collections4.Predicate<Customer>() {
79+
public boolean evaluate(Customer customer) {
80+
return customer.getName().equals(name);
81+
}
82+
});
83+
}
84+
}

core-java-modules/core-java-collections-list/src/test/java/com/baeldung/findanelement/FindACustomerInGivenListUnitTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.baeldung.findanelement;
22

3-
import static org.junit.Assert.*;
4-
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.Assert.assertNotNull;
6+
import static org.junit.Assert.assertTrue;
7+
import static org.junit.Assert.assertFalse;
58
import java.util.ArrayList;
69
import java.util.List;
710

@@ -155,4 +158,16 @@ public void givenName_whenCustomerWithNameNotFoundUsingGuava_thenReturnNull() {
155158
assertNull(john);
156159
}
157160

161+
@Test
162+
public void givenName_whenCustomerWithNameFoundUsingExists_thenReturnTrue() {
163+
boolean isJamesPresent = findACustomerInGivenList.findUsingExists("James", customers);
164+
assertTrue(isJamesPresent);
165+
}
166+
167+
@Test
168+
public void givenName_whenCustomerWithNameNotFoundUsingExists_thenReturnFalse() {
169+
boolean isJohnPresent = findACustomerInGivenList.findUsingExists("John", customers);
170+
assertFalse(isJohnPresent);
171+
}
172+
158173
}

0 commit comments

Comments
 (0)