Skip to content

Commit b559157

Browse files
shubhi22jzheaux
authored andcommitted
BAEL-1584 : Finding an element in list (eugenp#4067)
* BAEL-1584 : Find an Element in Given List
1 parent 47a7aaa commit b559157

5 files changed

Lines changed: 272 additions & 187 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.findanelement;
2+
3+
public class Customer {
4+
5+
private int id;
6+
private String name;
7+
8+
public Customer(int id, String name) {
9+
this.id = id;
10+
this.name = name;
11+
}
12+
13+
public int getId() {
14+
return id;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
@Override
22+
public int hashCode() {
23+
return id * 20;
24+
}
25+
26+
@Override
27+
public boolean equals(Object obj) {
28+
if (obj instanceof Customer) {
29+
Customer otherCustomer = (Customer) obj;
30+
if (id == otherCustomer.id)
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
37+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.findanelement;
2+
3+
import java.util.Iterator;
4+
import java.util.List;
5+
6+
import org.apache.commons.collections4.IterableUtils;
7+
8+
import com.google.common.base.Predicate;
9+
import com.google.common.collect.Iterables;
10+
11+
public class FindACustomerInGivenList {
12+
13+
public Customer findUsingGivenIndex(int indexOfCustomer, List<Customer> customers) {
14+
if (indexOfCustomer >= 0 && indexOfCustomer < customers.size())
15+
return customers.get(indexOfCustomer);
16+
return null;
17+
}
18+
19+
public int findUsingIndexOf(Customer customer, List<Customer> customers) {
20+
return customers.indexOf(customer);
21+
}
22+
23+
public boolean findUsingContains(Customer customer, List<Customer> customers) {
24+
return customers.contains(customer);
25+
}
26+
27+
public Customer findUsingIterator(String name, List<Customer> customers) {
28+
Iterator<Customer> iterator = customers.iterator();
29+
while (iterator.hasNext()) {
30+
Customer customer = iterator.next();
31+
if (customer.getName().equals(name)) {
32+
return customer;
33+
}
34+
}
35+
return null;
36+
}
37+
38+
public Customer findUsingEnhancedForLoop(String name, List<Customer> customers) {
39+
for (Customer customer : customers) {
40+
if (customer.getName().equals(name)) {
41+
return customer;
42+
}
43+
}
44+
return null;
45+
}
46+
47+
public Customer findUsingStream(String name, List<Customer> customers) {
48+
return customers.stream()
49+
.filter(customer -> customer.getName().equals(name))
50+
.findFirst()
51+
.orElse(null);
52+
}
53+
54+
public Customer findUsingParallelStream(String name, List<Customer> customers) {
55+
return customers.parallelStream()
56+
.filter(customer -> customer.getName().equals(name))
57+
.findAny()
58+
.orElse(null);
59+
}
60+
61+
public Customer findUsingGuava(String name, List<Customer> customers) {
62+
return Iterables.tryFind(customers, new Predicate<Customer>() {
63+
public boolean apply(Customer customer) {
64+
return customer.getName().equals(name);
65+
}
66+
}).orNull();
67+
}
68+
69+
public Customer findUsingApacheCommon(String name, List<Customer> customers) {
70+
return IterableUtils.find(customers, new org.apache.commons.collections4.Predicate<Customer>() {
71+
public boolean evaluate(Customer customer) {
72+
return customer.getName().equals(name);
73+
}
74+
});
75+
}
76+
77+
}

‎core-java-8/src/main/java/com/baeldung/findanelement/FindElementInAList.java‎

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.baeldung.findanelement;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.Test;
9+
10+
public class FindACustomerInGivenListTest {
11+
12+
private static List<Customer> customers = new ArrayList<>();
13+
14+
static {
15+
customers.add(new Customer(1, "Jack"));
16+
customers.add(new Customer(2, "James"));
17+
customers.add(new Customer(3, "Sam"));
18+
}
19+
20+
private static FindACustomerInGivenList findACustomerInGivenList = new FindACustomerInGivenList();
21+
22+
@Test
23+
public void givenAnIndex_whenFoundUsingGivenIndex_thenReturnCustomer() {
24+
Customer customer = findACustomerInGivenList.findUsingGivenIndex(0, customers);
25+
26+
assertEquals(1, customer.getId());
27+
}
28+
29+
@Test
30+
public void givenAnIndex_whenNotFoundUsingGivenIndex_thenReturnNull() {
31+
Customer customer = findACustomerInGivenList.findUsingGivenIndex(5, customers);
32+
33+
assertNull(customer);
34+
}
35+
36+
@Test
37+
public void givenACustomer_whenFoundUsingContains_thenReturnTrue() {
38+
Customer james = new Customer(2, "James");
39+
boolean isJamesPresent = findACustomerInGivenList.findUsingContains(james, customers);
40+
41+
assertEquals(true, isJamesPresent);
42+
}
43+
44+
@Test
45+
public void givenACustomer_whenNotFoundUsingContains_thenReturnFalse() {
46+
Customer john = new Customer(5, "John");
47+
boolean isJohnPresent = findACustomerInGivenList.findUsingContains(john, customers);
48+
49+
assertEquals(false, isJohnPresent);
50+
}
51+
52+
@Test
53+
public void givenACustomer_whenFoundUsingIndexOf_thenReturnItsIndex() {
54+
Customer james = new Customer(2, "James");
55+
int indexOfJames = findACustomerInGivenList.findUsingIndexOf(james, customers);
56+
57+
assertEquals(1, indexOfJames);
58+
}
59+
60+
@Test
61+
public void givenACustomer_whenNotFoundUsingIndexOf_thenReturnMinus1() {
62+
Customer john = new Customer(5, "John");
63+
int indexOfJohn = findACustomerInGivenList.findUsingIndexOf(john, customers);
64+
65+
assertEquals(-1, indexOfJohn);
66+
}
67+
68+
@Test
69+
public void givenName_whenCustomerWithNameFoundUsingIterator_thenReturnCustomer() {
70+
Customer james = findACustomerInGivenList.findUsingIterator("James", customers);
71+
72+
assertEquals("James", james.getName());
73+
assertEquals(2, james.getId());
74+
}
75+
76+
@Test
77+
public void givenName_whenCustomerWithNameNotFoundUsingIterator_thenReturnNull() {
78+
Customer john = findACustomerInGivenList.findUsingIterator("John", customers);
79+
80+
assertNull(john);
81+
}
82+
83+
@Test
84+
public void givenName_whenCustomerWithNameFoundUsingEnhancedFor_thenReturnCustomer() {
85+
Customer james = findACustomerInGivenList.findUsingEnhancedForLoop("James", customers);
86+
87+
assertEquals("James", james.getName());
88+
assertEquals(2, james.getId());
89+
}
90+
91+
@Test
92+
public void givenName_whenCustomerWithNameNotFoundUsingEnhancedFor_thenReturnNull() {
93+
Customer john = findACustomerInGivenList.findUsingEnhancedForLoop("John", customers);
94+
95+
assertNull(john);
96+
}
97+
98+
@Test
99+
public void givenName_whenCustomerWithNameFoundUsingStream_thenReturnCustomer() {
100+
Customer james = findACustomerInGivenList.findUsingStream("James", customers);
101+
102+
assertEquals("James", james.getName());
103+
assertEquals(2, james.getId());
104+
}
105+
106+
@Test
107+
public void givenName_whenCustomerWithNameNotFoundUsingStream_thenReturnNull() {
108+
Customer john = findACustomerInGivenList.findUsingStream("John", customers);
109+
110+
assertNull(john);
111+
}
112+
113+
@Test
114+
public void givenName_whenCustomerWithNameFoundUsingParallelStream_thenReturnCustomer() {
115+
Customer james = findACustomerInGivenList.findUsingParallelStream("James", customers);
116+
117+
assertEquals("James", james.getName());
118+
assertEquals(2, james.getId());
119+
}
120+
121+
@Test
122+
public void givenName_whenCustomerWithNameNotFoundUsingParallelStream_thenReturnNull() {
123+
Customer john = findACustomerInGivenList.findUsingParallelStream("John", customers);
124+
125+
assertNull(john);
126+
}
127+
128+
@Test
129+
public void givenName_whenCustomerWithNameFoundUsingApacheCommon_thenReturnCustomer() {
130+
Customer james = findACustomerInGivenList.findUsingApacheCommon("James", customers);
131+
132+
assertEquals("James", james.getName());
133+
assertEquals(2, james.getId());
134+
}
135+
136+
@Test
137+
public void givenName_whenCustomerWithNameNotFoundUsingApacheCommon_thenReturnNull() {
138+
Customer john = findACustomerInGivenList.findUsingApacheCommon("John", customers);
139+
140+
assertNull(john);
141+
}
142+
143+
@Test
144+
public void givenName_whenCustomerWithNameFoundUsingGuava_thenReturnCustomer() {
145+
Customer james = findACustomerInGivenList.findUsingGuava("James", customers);
146+
147+
assertEquals("James", james.getName());
148+
assertEquals(2, james.getId());
149+
}
150+
151+
@Test
152+
public void givenName_whenCustomerWithNameNotFoundUsingGuava_thenReturnNull() {
153+
Customer john = findACustomerInGivenList.findUsingGuava("John", customers);
154+
155+
assertNull(john);
156+
}
157+
158+
}

0 commit comments

Comments
 (0)