Skip to content

Commit

Permalink
BAEL-1584 : Finding an element in list (eugenp#4067)
Browse files Browse the repository at this point in the history
* BAEL-1584 : Find an Element in Given List
  • Loading branch information
shubhi22 authored and jzheaux committed Apr 30, 2018
1 parent 47a7aaa commit b559157
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 187 deletions.
37 changes: 37 additions & 0 deletions core-java-8/src/main/java/com/baeldung/findanelement/Customer.java
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;
}


}
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);
}
});
}

}

This file was deleted.

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);
}

}
Loading

0 comments on commit b559157

Please sign in to comment.