forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-1584 : Finding an element in list (eugenp#4067)
* BAEL-1584 : Find an Element in Given List
- Loading branch information
Showing
5 changed files
with
272 additions
and
187 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
core-java-8/src/main/java/com/baeldung/findanelement/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.baeldung.findanelement; | ||
|
||
public class Customer { | ||
|
||
private int id; | ||
private String name; | ||
|
||
public Customer(int id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return id * 20; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof Customer) { | ||
Customer otherCustomer = (Customer) obj; | ||
if (id == otherCustomer.id) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
} |
77 changes: 77 additions & 0 deletions
77
core-java-8/src/main/java/com/baeldung/findanelement/FindACustomerInGivenList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.baeldung.findanelement; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.apache.commons.collections4.IterableUtils; | ||
|
||
import com.google.common.base.Predicate; | ||
import com.google.common.collect.Iterables; | ||
|
||
public class FindACustomerInGivenList { | ||
|
||
public Customer findUsingGivenIndex(int indexOfCustomer, List<Customer> customers) { | ||
if (indexOfCustomer >= 0 && indexOfCustomer < customers.size()) | ||
return customers.get(indexOfCustomer); | ||
return null; | ||
} | ||
|
||
public int findUsingIndexOf(Customer customer, List<Customer> customers) { | ||
return customers.indexOf(customer); | ||
} | ||
|
||
public boolean findUsingContains(Customer customer, List<Customer> customers) { | ||
return customers.contains(customer); | ||
} | ||
|
||
public Customer findUsingIterator(String name, List<Customer> customers) { | ||
Iterator<Customer> iterator = customers.iterator(); | ||
while (iterator.hasNext()) { | ||
Customer customer = iterator.next(); | ||
if (customer.getName().equals(name)) { | ||
return customer; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public Customer findUsingEnhancedForLoop(String name, List<Customer> customers) { | ||
for (Customer customer : customers) { | ||
if (customer.getName().equals(name)) { | ||
return customer; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public Customer findUsingStream(String name, List<Customer> customers) { | ||
return customers.stream() | ||
.filter(customer -> customer.getName().equals(name)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
public Customer findUsingParallelStream(String name, List<Customer> customers) { | ||
return customers.parallelStream() | ||
.filter(customer -> customer.getName().equals(name)) | ||
.findAny() | ||
.orElse(null); | ||
} | ||
|
||
public Customer findUsingGuava(String name, List<Customer> customers) { | ||
return Iterables.tryFind(customers, new Predicate<Customer>() { | ||
public boolean apply(Customer customer) { | ||
return customer.getName().equals(name); | ||
} | ||
}).orNull(); | ||
} | ||
|
||
public Customer findUsingApacheCommon(String name, List<Customer> customers) { | ||
return IterableUtils.find(customers, new org.apache.commons.collections4.Predicate<Customer>() { | ||
public boolean evaluate(Customer customer) { | ||
return customer.getName().equals(name); | ||
} | ||
}); | ||
} | ||
|
||
} |
71 changes: 0 additions & 71 deletions
71
core-java-8/src/main/java/com/baeldung/findanelement/FindElementInAList.java
This file was deleted.
Oops, something went wrong.
158 changes: 158 additions & 0 deletions
158
core-java-8/src/test/java/com/baeldung/findanelement/FindACustomerInGivenListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package com.baeldung.findanelement; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
public class FindACustomerInGivenListTest { | ||
|
||
private static List<Customer> customers = new ArrayList<>(); | ||
|
||
static { | ||
customers.add(new Customer(1, "Jack")); | ||
customers.add(new Customer(2, "James")); | ||
customers.add(new Customer(3, "Sam")); | ||
} | ||
|
||
private static FindACustomerInGivenList findACustomerInGivenList = new FindACustomerInGivenList(); | ||
|
||
@Test | ||
public void givenAnIndex_whenFoundUsingGivenIndex_thenReturnCustomer() { | ||
Customer customer = findACustomerInGivenList.findUsingGivenIndex(0, customers); | ||
|
||
assertEquals(1, customer.getId()); | ||
} | ||
|
||
@Test | ||
public void givenAnIndex_whenNotFoundUsingGivenIndex_thenReturnNull() { | ||
Customer customer = findACustomerInGivenList.findUsingGivenIndex(5, customers); | ||
|
||
assertNull(customer); | ||
} | ||
|
||
@Test | ||
public void givenACustomer_whenFoundUsingContains_thenReturnTrue() { | ||
Customer james = new Customer(2, "James"); | ||
boolean isJamesPresent = findACustomerInGivenList.findUsingContains(james, customers); | ||
|
||
assertEquals(true, isJamesPresent); | ||
} | ||
|
||
@Test | ||
public void givenACustomer_whenNotFoundUsingContains_thenReturnFalse() { | ||
Customer john = new Customer(5, "John"); | ||
boolean isJohnPresent = findACustomerInGivenList.findUsingContains(john, customers); | ||
|
||
assertEquals(false, isJohnPresent); | ||
} | ||
|
||
@Test | ||
public void givenACustomer_whenFoundUsingIndexOf_thenReturnItsIndex() { | ||
Customer james = new Customer(2, "James"); | ||
int indexOfJames = findACustomerInGivenList.findUsingIndexOf(james, customers); | ||
|
||
assertEquals(1, indexOfJames); | ||
} | ||
|
||
@Test | ||
public void givenACustomer_whenNotFoundUsingIndexOf_thenReturnMinus1() { | ||
Customer john = new Customer(5, "John"); | ||
int indexOfJohn = findACustomerInGivenList.findUsingIndexOf(john, customers); | ||
|
||
assertEquals(-1, indexOfJohn); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameFoundUsingIterator_thenReturnCustomer() { | ||
Customer james = findACustomerInGivenList.findUsingIterator("James", customers); | ||
|
||
assertEquals("James", james.getName()); | ||
assertEquals(2, james.getId()); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameNotFoundUsingIterator_thenReturnNull() { | ||
Customer john = findACustomerInGivenList.findUsingIterator("John", customers); | ||
|
||
assertNull(john); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameFoundUsingEnhancedFor_thenReturnCustomer() { | ||
Customer james = findACustomerInGivenList.findUsingEnhancedForLoop("James", customers); | ||
|
||
assertEquals("James", james.getName()); | ||
assertEquals(2, james.getId()); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameNotFoundUsingEnhancedFor_thenReturnNull() { | ||
Customer john = findACustomerInGivenList.findUsingEnhancedForLoop("John", customers); | ||
|
||
assertNull(john); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameFoundUsingStream_thenReturnCustomer() { | ||
Customer james = findACustomerInGivenList.findUsingStream("James", customers); | ||
|
||
assertEquals("James", james.getName()); | ||
assertEquals(2, james.getId()); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameNotFoundUsingStream_thenReturnNull() { | ||
Customer john = findACustomerInGivenList.findUsingStream("John", customers); | ||
|
||
assertNull(john); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameFoundUsingParallelStream_thenReturnCustomer() { | ||
Customer james = findACustomerInGivenList.findUsingParallelStream("James", customers); | ||
|
||
assertEquals("James", james.getName()); | ||
assertEquals(2, james.getId()); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameNotFoundUsingParallelStream_thenReturnNull() { | ||
Customer john = findACustomerInGivenList.findUsingParallelStream("John", customers); | ||
|
||
assertNull(john); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameFoundUsingApacheCommon_thenReturnCustomer() { | ||
Customer james = findACustomerInGivenList.findUsingApacheCommon("James", customers); | ||
|
||
assertEquals("James", james.getName()); | ||
assertEquals(2, james.getId()); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameNotFoundUsingApacheCommon_thenReturnNull() { | ||
Customer john = findACustomerInGivenList.findUsingApacheCommon("John", customers); | ||
|
||
assertNull(john); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameFoundUsingGuava_thenReturnCustomer() { | ||
Customer james = findACustomerInGivenList.findUsingGuava("James", customers); | ||
|
||
assertEquals("James", james.getName()); | ||
assertEquals(2, james.getId()); | ||
} | ||
|
||
@Test | ||
public void givenName_whenCustomerWithNameNotFoundUsingGuava_thenReturnNull() { | ||
Customer john = findACustomerInGivenList.findUsingGuava("John", customers); | ||
|
||
assertNull(john); | ||
} | ||
|
||
} |
Oops, something went wrong.