Skip to content

Commit 6f198df

Browse files
BAEL-965 - CollectionUtil
1 parent 69c330e commit 6f198df

3 files changed

Lines changed: 293 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.commons.collectionutil;
2+
3+
public class Address {
4+
5+
String locality;
6+
String city;
7+
String zip;
8+
9+
public String getLocality() {
10+
return locality;
11+
}
12+
13+
public void setLocality(String locality) {
14+
this.locality = locality;
15+
}
16+
17+
public String getCity() {
18+
return city;
19+
}
20+
21+
public void setCity(String city) {
22+
this.city = city;
23+
}
24+
25+
public String getZip() {
26+
return zip;
27+
}
28+
29+
public void setZip(String zip) {
30+
this.zip = zip;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
StringBuilder builder = new StringBuilder();
36+
builder.append("Address [locality=").append(locality).append(", city=").append(city).append(", zip=").append(zip).append("]");
37+
return builder.toString();
38+
}
39+
40+
public Address(String locality, String city, String zip) {
41+
super();
42+
this.locality = locality;
43+
this.city = city;
44+
this.zip = zip;
45+
}
46+
47+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.baeldung.commons.collectionutil;
2+
3+
public class Customer implements Comparable<Customer> {
4+
5+
Integer id;
6+
String name;
7+
Long phone;
8+
String locality;
9+
String city;
10+
String zip;
11+
12+
public Integer getId() {
13+
return id;
14+
}
15+
16+
public void setId(Integer id) {
17+
this.id = id;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
28+
public Long getPhone() {
29+
return phone;
30+
}
31+
32+
public void setPhone(Long phone) {
33+
this.phone = phone;
34+
}
35+
36+
public String getLocality() {
37+
return locality;
38+
}
39+
40+
public void setLocality(String locality) {
41+
this.locality = locality;
42+
}
43+
44+
public String getCity() {
45+
return city;
46+
}
47+
48+
public void setCity(String city) {
49+
this.city = city;
50+
}
51+
52+
public String getZip() {
53+
return zip;
54+
}
55+
56+
public void setZip(String zip) {
57+
this.zip = zip;
58+
}
59+
60+
public Customer(Integer id, String name, Long phone, String locality, String city, String zip) {
61+
super();
62+
this.id = id;
63+
this.name = name;
64+
this.phone = phone;
65+
this.locality = locality;
66+
this.city = city;
67+
this.zip = zip;
68+
}
69+
70+
public Customer(Integer id, String name, Long phone) {
71+
super();
72+
this.id = id;
73+
this.name = name;
74+
this.phone = phone;
75+
}
76+
77+
public Customer(String name) {
78+
super();
79+
this.name = name;
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
final int prime = 31;
85+
int result = 1;
86+
result = prime * result + ((name == null) ? 0 : name.hashCode());
87+
return result;
88+
}
89+
90+
@Override
91+
public boolean equals(Object obj) {
92+
if (this == obj)
93+
return true;
94+
if (obj == null)
95+
return false;
96+
if (getClass() != obj.getClass())
97+
return false;
98+
Customer other = (Customer) obj;
99+
if (name == null) {
100+
if (other.name != null)
101+
return false;
102+
} else if (!name.equals(other.name))
103+
return false;
104+
return true;
105+
}
106+
107+
public int compareTo(Customer o) {
108+
return this.name.compareTo(o.getName());
109+
}
110+
111+
@Override
112+
public String toString() {
113+
StringBuilder builder = new StringBuilder();
114+
builder.append("Customer [id=").append(id).append(", name=").append(name).append(", phone=").append(phone).append("]");
115+
return builder.toString();
116+
}
117+
118+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.baeldung.commons.collections;
2+
3+
4+
import com.baeldung.commons.collectionutil.Address;
5+
import com.baeldung.commons.collectionutil.Customer;
6+
import org.apache.commons.collections4.Closure;
7+
import org.apache.commons.collections4.CollectionUtils;
8+
import org.apache.commons.collections4.Predicate;
9+
import org.apache.commons.collections4.Transformer;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import java.util.*;
14+
15+
import static org.junit.Assert.assertFalse;
16+
import static org.junit.Assert.assertTrue;
17+
18+
public class CollectionUtilsGuideTest {
19+
20+
21+
Customer customer1 = new Customer(1, "Daniel", 123456l, "locality1", "city1", "1234");
22+
Customer customer4 = new Customer(4, "Bob", 456789l, "locality4", "city4", "4567");
23+
List<Customer> list1, list2, list3, linkedList1;
24+
25+
@Before
26+
public void setup() {
27+
Customer customer2 = new Customer(2, "Fredrik", 234567l, "locality2", "city2", "2345");
28+
Customer customer3 = new Customer(3, "Kyle", 345678l, "locality3", "city3", "3456");
29+
Customer customer5 = new Customer(5, "Cat", 567890l, "locality5", "city5", "5678");
30+
Customer customer6 = new Customer(6, "John", 678901l, "locality6", "city6", "6789");
31+
32+
list1 = Arrays.asList(customer1, customer2, customer3);
33+
list2 = Arrays.asList(customer4, customer5, customer6);
34+
list3 = Arrays.asList(customer1, customer2);
35+
36+
linkedList1 = new LinkedList<>(list1);
37+
}
38+
39+
@Test
40+
public void givenList_whenAddIgnoreNull_thenNoNullAdded() {
41+
CollectionUtils.addIgnoreNull(list1, null);
42+
assertFalse(list1.contains(null));
43+
}
44+
45+
@Test
46+
public void givenTwoSortedLists_whenCollated_thenSorted() {
47+
List<Customer> sortedList = CollectionUtils.collate(list1, list2);
48+
assertTrue(sortedList.get(0).getName().equals("Bob"));
49+
assertTrue(sortedList.get(2).getName().equals("Daniel"));
50+
}
51+
52+
@Test
53+
public void givenListOfCustomers_whenTransformed_thenListOfAddress() {
54+
Collection<Address> addressCol = CollectionUtils.collect(list1, new Transformer<Customer, Address>() {
55+
public Address transform(Customer customer) {
56+
return new Address(customer.getLocality(), customer.getCity(), customer.getZip());
57+
}
58+
});
59+
60+
List<Address> addressList = new ArrayList<Address>(addressCol);
61+
assertTrue(addressList.size() == 3);
62+
assertTrue(addressList.get(0).getLocality().equals("locality1"));
63+
}
64+
65+
@Test
66+
public void givenCustomerList_whenCountMatches_thenCorrect() {
67+
int result = CollectionUtils.countMatches(list1, customer -> Arrays.asList("Daniel","Kyle").contains(customer.getName()));
68+
assertTrue(result == 2);
69+
}
70+
71+
@Test
72+
public void givenCustomerList_WhenFiltered_thenCorrectSize() {
73+
74+
boolean isModified = CollectionUtils.filter(linkedList1, customer -> Arrays.asList("Daniel","Kyle").contains(customer.getName()));
75+
76+
//filterInverse does the opposite. It removes the element from the list if the Predicate returns true
77+
//select and selectRejected work the same way except that they do not remove elements from the given collection and return a new collection
78+
79+
assertTrue(isModified && linkedList1.size() == 2);
80+
}
81+
82+
@Test
83+
public void givenCustomerList_whenForAllDoSetNameNull_thenNameNull() {
84+
CollectionUtils.forAllDo(list1, customer -> {
85+
customer.setName(null);
86+
});
87+
88+
// forAllButLast does the same except for the last element in the collection
89+
assertTrue(list1.get(0).getName() == null);
90+
}
91+
92+
@Test
93+
public void givenNonEmptyList_whenCheckedIsNotEmpty_thenTrue() {
94+
List<Customer> emptyList = new ArrayList<Customer>();
95+
List<Customer> nullList = null;
96+
97+
//Very handy at times where we want to check if a collection is not null and not empty too.
98+
//isNotEmpty does the opposite. Handy because using ! operator on isEmpty makes it missable while reading
99+
assertTrue(CollectionUtils.isNotEmpty(list1));
100+
assertTrue(CollectionUtils.isEmpty(nullList));
101+
assertTrue(CollectionUtils.isEmpty(emptyList));
102+
}
103+
104+
@Test
105+
public void givenCustomerListAndASubcollection_whenChecked_thenTrue() {
106+
assertTrue(CollectionUtils.isSubCollection(list3, list1));
107+
}
108+
109+
@Test
110+
public void givenTwoLists_whenIntersected_thenCheckSize() {
111+
Collection<Customer> intersection = CollectionUtils.intersection(list1, list3);
112+
assertTrue(intersection.size() == 2);
113+
}
114+
115+
@Test
116+
public void givenTwoLists_whenSubtracted_thenCheckElementNotPresentInA() {
117+
Collection<Customer> result = CollectionUtils.subtract(list1, list3);
118+
assertFalse(result.contains(customer1));
119+
}
120+
121+
@Test
122+
public void givenTwoLists_whenUnioned_thenCheckElementPresentInResult() {
123+
Collection<Customer> union = CollectionUtils.union(list1, list2);
124+
assertTrue(union.contains(customer1));
125+
assertTrue(union.contains(customer4));
126+
}
127+
128+
}

0 commit comments

Comments
 (0)