Adding collection utils guide
Adding Apache Commons 4 Collection Utils guide
This commit is contained in:
parent
5b9a125581
commit
e5dc539a4f
|
@ -0,0 +1,33 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>baeldung</groupId>
|
||||
<artifactId>collectionutils</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>collectionutils-guide</name>
|
||||
<description>A guide to Apache Commons CollectionUtils</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.collectionutilsguide.model;
|
||||
|
||||
public class Address {
|
||||
|
||||
String locality;
|
||||
String city;
|
||||
String zip;
|
||||
|
||||
public String getLocality() {
|
||||
return locality;
|
||||
}
|
||||
|
||||
public void setLocality(String locality) {
|
||||
this.locality = locality;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getZip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(String zip) {
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("Address [locality=").append(locality).append(", city=").append(city).append(", zip=").append(zip).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public Address(String locality, String city, String zip) {
|
||||
super();
|
||||
this.locality = locality;
|
||||
this.city = city;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
package com.baeldung.collectionutilsguide.model;
|
||||
|
||||
public class Customer implements Comparable<Customer> {
|
||||
|
||||
Integer id;
|
||||
String name;
|
||||
Long phone;
|
||||
String locality;
|
||||
String city;
|
||||
String zip;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(Long phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public String getLocality() {
|
||||
return locality;
|
||||
}
|
||||
|
||||
public void setLocality(String locality) {
|
||||
this.locality = locality;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getZip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(String zip) {
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public Customer(Integer id, String name, Long phone, String locality, String city, String zip) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.phone = phone;
|
||||
this.locality = locality;
|
||||
this.city = city;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public Customer(Integer id, String name, Long phone) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public Customer(String name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Customer other = (Customer) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public int compareTo(Customer o) {
|
||||
return this.name.compareTo(o.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("Customer [id=").append(id).append(", name=").append(name).append(", phone=").append(phone).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
package collectionutils;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections4.Closure;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.Predicate;
|
||||
import org.apache.commons.collections4.Transformer;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.collectionutilsguide.model.Address;
|
||||
import com.baeldung.collectionutilsguide.model.Customer;
|
||||
|
||||
public class CollectionUtilsGuideTest {
|
||||
|
||||
Customer customer1 = new Customer(1, "Daniel", 123456l, "locality1", "city1", "1234");
|
||||
Customer customer2 = new Customer(2, "Fredrik", 234567l, "locality2", "city2", "2345");
|
||||
Customer customer3 = new Customer(3, "Kyle", 345678l, "locality3", "city3", "3456");
|
||||
Customer customer4 = new Customer(4, "Bob", 456789l, "locality4", "city4", "4567");
|
||||
Customer customer5 = new Customer(5, "Cat", 567890l, "locality5", "city5", "5678");
|
||||
Customer customer6 = new Customer(6, "John", 678901l, "locality6", "city6", "6789");
|
||||
|
||||
List<Customer> list1 = Arrays.asList(customer1, customer2, customer3);
|
||||
List<Customer> list2 = Arrays.asList(customer4, customer5, customer6);
|
||||
List<Customer> list3 = Arrays.asList(customer1, customer2);
|
||||
|
||||
List<Customer> linkedList1 = new LinkedList<Customer>(list1);
|
||||
|
||||
@Test
|
||||
public void givenList_WhenAddIgnoreNull_thenNoNullAdded() {
|
||||
List<String> list = Arrays.asList("x", "y", "z");
|
||||
CollectionUtils.addIgnoreNull(list, null);
|
||||
assertFalse(list.contains(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSortedLists_WhenCollated_thenSorted() {
|
||||
List<Customer> sortedList = CollectionUtils.collate(list1, list2);
|
||||
assertTrue(sortedList.get(0).getName().equals("Bob"));
|
||||
assertTrue(sortedList.get(2).getName().equals("Daniel"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfCustomers_whenTransformed_thenListOfAddress() {
|
||||
Collection<Address> addressCol = CollectionUtils.collect(list1, new Transformer<Customer, Address>() {
|
||||
public Address transform(Customer customer) {
|
||||
return new Address(customer.getLocality(), customer.getCity(), customer.getZip());
|
||||
}
|
||||
});
|
||||
|
||||
List<Address> addressList = new ArrayList<Address>(addressCol);
|
||||
assertTrue(addressList.size() == 3);
|
||||
assertTrue(addressList.get(0).getLocality().equals("locality1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomerList_WhenCountMatches_thenCorrect() {
|
||||
int result = CollectionUtils.countMatches(list1, new Predicate<Customer>() {
|
||||
public boolean evaluate(Customer customer) {
|
||||
return Arrays.asList("Daniel","Kyle").contains(customer.getName());
|
||||
}
|
||||
});
|
||||
assertTrue(result == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomerList_WhenFiltered_thenCorrectSize() {
|
||||
|
||||
boolean isModified = CollectionUtils.filter(linkedList1, new Predicate<Customer>() {
|
||||
public boolean evaluate(Customer customer) {
|
||||
return Arrays.asList("Daniel","Kyle").contains(customer.getName());
|
||||
}
|
||||
});
|
||||
|
||||
//filterInverse does the opposite. It removes the element from the list if the Predicate returns true
|
||||
//select and selectRejected work the same way except that they do not remove elements from the given collection and return a new collection
|
||||
|
||||
assertTrue(isModified && linkedList1.size() == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomerList_WhenForAllDoSetNameNull_thenNameNull() {
|
||||
CollectionUtils.forAllDo(list1, new Closure<Customer>() {
|
||||
public void execute(Customer customer) {
|
||||
customer.setName(null);
|
||||
}
|
||||
});
|
||||
|
||||
// forAllButLast does the same except for the last element in the collection
|
||||
assertTrue(list1.get(0).getName() == null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyList_WhenCheckedIsEmpty_thenTrue() {
|
||||
List<Customer> emptyList = new ArrayList<Customer>();
|
||||
List<Customer> nullList = null;
|
||||
|
||||
//Very handy at times where we want to check if a collection is not null and not empty too.
|
||||
//isNotEmpty does the opposite. Handy because using ! operator on isEmpty makes it missable while reading
|
||||
assertTrue(CollectionUtils.isEmpty(emptyList));
|
||||
assertTrue(CollectionUtils.isEmpty(nullList));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomerListAndASubcollection_WhenChecked_thenTrue() {
|
||||
assertTrue(CollectionUtils.isSubCollection(list3, list1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_WhenIntersected_thenCheckSize() {
|
||||
Collection<Customer> intersection = CollectionUtils.intersection(list1, list3);
|
||||
assertTrue(intersection.size() == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_WhenSubtracted_thenCheckElementNotPresentInA() {
|
||||
Collection<Customer> result = CollectionUtils.subtract(list1, list3);
|
||||
assertFalse(result.contains(customer1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_WhenUnioned_thenCheckElementPresentInResult() {
|
||||
Collection<Customer> union = CollectionUtils.union(list1, list2);
|
||||
assertTrue(union.contains(customer1));
|
||||
assertTrue(union.contains(customer4));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue