diff --git a/core-java-8/src/main/java/com/baeldung/findanelement/Customer.java b/core-java-8/src/main/java/com/baeldung/findanelement/Customer.java new file mode 100644 index 0000000000..6807a6642b --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/findanelement/Customer.java @@ -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; + } + + +} diff --git a/core-java-8/src/main/java/com/baeldung/findanelement/FindACustomerInGivenList.java b/core-java-8/src/main/java/com/baeldung/findanelement/FindACustomerInGivenList.java new file mode 100644 index 0000000000..b2d4250f6b --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/findanelement/FindACustomerInGivenList.java @@ -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 customers) { + if (indexOfCustomer >= 0 && indexOfCustomer < customers.size()) + return customers.get(indexOfCustomer); + return null; + } + + public int findUsingIndexOf(Customer customer, List customers) { + return customers.indexOf(customer); + } + + public boolean findUsingContains(Customer customer, List customers) { + return customers.contains(customer); + } + + public Customer findUsingIterator(String name, List customers) { + Iterator 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 customers) { + for (Customer customer : customers) { + if (customer.getName().equals(name)) { + return customer; + } + } + return null; + } + + public Customer findUsingStream(String name, List customers) { + return customers.stream() + .filter(customer -> customer.getName().equals(name)) + .findFirst() + .orElse(null); + } + + public Customer findUsingParallelStream(String name, List customers) { + return customers.parallelStream() + .filter(customer -> customer.getName().equals(name)) + .findAny() + .orElse(null); + } + + public Customer findUsingGuava(String name, List customers) { + return Iterables.tryFind(customers, new Predicate() { + public boolean apply(Customer customer) { + return customer.getName().equals(name); + } + }).orNull(); + } + + public Customer findUsingApacheCommon(String name, List customers) { + return IterableUtils.find(customers, new org.apache.commons.collections4.Predicate() { + public boolean evaluate(Customer customer) { + return customer.getName().equals(name); + } + }); + } + +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/findanelement/FindElementInAList.java b/core-java-8/src/main/java/com/baeldung/findanelement/FindElementInAList.java deleted file mode 100644 index 2f402ee72b..0000000000 --- a/core-java-8/src/main/java/com/baeldung/findanelement/FindElementInAList.java +++ /dev/null @@ -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 { - - public T findUsingIndexOf(T element, List list) { - int index = list.indexOf(element); - if (index >= 0) { - return element; - } - return null; - } - - public boolean findUsingListIterator(T element, List list) { - ListIterator listIterator = list.listIterator(); - while (listIterator.hasNext()) { - T elementFromList = listIterator.next(); - if (elementFromList.equals(element)) { - return true; - } - } - return false; - } - - public boolean findUsingEnhancedForLoop(T element, List list) { - for (T elementFromList : list) { - if (element.equals(elementFromList)) { - return true; - } - } - return false; - } - - public T findUsingStream(T element, List list) { - return list.stream() - .filter(integer -> integer.equals(element)) - .findFirst() - .orElse(null); - } - - public T findUsingParallelStream(T element, List list) { - return list.parallelStream() - .filter(integer -> integer.equals(element)) - .findAny() - .orElse(null); - } - - public T findUsingGuava(T element, List list) { - T foundElement = Iterables.tryFind(list, new Predicate() { - public boolean apply(T input) { - return element.equals(input); - } - }).orNull(); - return foundElement; - } - - public T findUsingApacheCommon(T element, List list) { - T foundElement = IterableUtils.find(list, new org.apache.commons.collections4.Predicate() { - public boolean evaluate(T input) { - return element.equals(input); - } - }); - return foundElement; - } - -} \ No newline at end of file diff --git a/core-java-8/src/test/java/com/baeldung/findanelement/FindACustomerInGivenListTest.java b/core-java-8/src/test/java/com/baeldung/findanelement/FindACustomerInGivenListTest.java new file mode 100644 index 0000000000..45ee6eda33 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/findanelement/FindACustomerInGivenListTest.java @@ -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 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); + } + +} diff --git a/core-java-8/src/test/java/com/baeldung/findanelement/FindAnElementTest.java b/core-java-8/src/test/java/com/baeldung/findanelement/FindAnElementTest.java deleted file mode 100644 index 1fef2d98e7..0000000000 --- a/core-java-8/src/test/java/com/baeldung/findanelement/FindAnElementTest.java +++ /dev/null @@ -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 scores = new ArrayList<>(); - static { - scores.add(0); - scores.add(1); - scores.add(2); - } - - private static FindElementInAList 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); - } - -} \ No newline at end of file