BAEL-1584 : Finding an element in list (#4067)
* BAEL-1584 : Find an Element in Given List
This commit is contained in:
parent
47a7aaae5d
commit
b559157482
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
package com.baeldung.findanelement;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
public class FindElementInAList<T> {
|
||||
|
||||
public T findUsingIndexOf(T element, List<T> list) {
|
||||
int index = list.indexOf(element);
|
||||
if (index >= 0) {
|
||||
return element;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean findUsingListIterator(T element, List<T> list) {
|
||||
ListIterator<T> listIterator = list.listIterator();
|
||||
while (listIterator.hasNext()) {
|
||||
T elementFromList = listIterator.next();
|
||||
if (elementFromList.equals(element)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean findUsingEnhancedForLoop(T element, List<T> list) {
|
||||
for (T elementFromList : list) {
|
||||
if (element.equals(elementFromList)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public T findUsingStream(T element, List<T> list) {
|
||||
return list.stream()
|
||||
.filter(integer -> integer.equals(element))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public T findUsingParallelStream(T element, List<T> list) {
|
||||
return list.parallelStream()
|
||||
.filter(integer -> integer.equals(element))
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public T findUsingGuava(T element, List<T> list) {
|
||||
T foundElement = Iterables.tryFind(list, new Predicate<T>() {
|
||||
public boolean apply(T input) {
|
||||
return element.equals(input);
|
||||
}
|
||||
}).orNull();
|
||||
return foundElement;
|
||||
}
|
||||
|
||||
public T findUsingApacheCommon(T element, List<T> list) {
|
||||
T foundElement = IterableUtils.find(list, new org.apache.commons.collections4.Predicate<T>() {
|
||||
public boolean evaluate(T input) {
|
||||
return element.equals(input);
|
||||
}
|
||||
});
|
||||
return foundElement;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,116 +0,0 @@
|
|||
package com.baeldung.findanelement;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FindAnElementTest {
|
||||
|
||||
private static List<Integer> scores = new ArrayList<>();
|
||||
static {
|
||||
scores.add(0);
|
||||
scores.add(1);
|
||||
scores.add(2);
|
||||
}
|
||||
|
||||
private static FindElementInAList<Integer> findElementInAList = new FindElementInAList<>();
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingIndexOf_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingIndexOf(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingListIterator_thenReturnNull() {
|
||||
boolean found = findElementInAList.findUsingListIterator(5, scores);
|
||||
assertTrue(!found);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundListIterator_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
boolean found = findElementInAList.findUsingListIterator(scoreToFind, scores);
|
||||
assertTrue(found);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingIndexOf_thenReturnNull() {
|
||||
Integer score = findElementInAList.findUsingIndexOf(5, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingEnhancedForLoop_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
boolean found = findElementInAList.findUsingEnhancedForLoop(scoreToFind, scores);
|
||||
assertTrue(found);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingEnhancedForLoop_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
boolean found = findElementInAList.findUsingEnhancedForLoop(scoreToFind, scores);
|
||||
assertTrue(!found);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingStream_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingStream(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingStream_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
Integer score = findElementInAList.findUsingStream(scoreToFind, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingParallelStream_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingParallelStream(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingParallelStream_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
Integer score = findElementInAList.findUsingParallelStream(scoreToFind, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingGuava_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingGuava(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingGuava_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
Integer score = findElementInAList.findUsingGuava(scoreToFind, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenFoundUsingApacheCommons_thenReturnElement() {
|
||||
Integer scoreToFind = 1;
|
||||
Integer score = findElementInAList.findUsingApacheCommon(scoreToFind, scores);
|
||||
assertTrue(score.equals(scoreToFind));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenElement_whenNotFoundUsingApacheCommons_thenReturnNull() {
|
||||
Integer scoreToFind = 5;
|
||||
Integer score = findElementInAList.findUsingApacheCommon(scoreToFind, scores);
|
||||
assertNull(score);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue