Use Apache Commons Collections to implement the Java collection operation
Use Apache Commons Collections to implement the Java collection operation
Apache Commons Collections is a commonly used open source Java library that provides a series of sets and tool classes to simplify and enhance the Java collection.This article will introduce how to use Apache Commons Collections to achieve some common collection operations.
1. The creation and initialization of the collection
Using Apache Commons Collections can quickly create and initialize a collection of various types.Here are some common examples:
// Create an ArrayList
List<String> list = new ArrayList<>();
// Create a havehset
Set<Integer> set = new HashSet<>();
// Create a timeMap
Map<String, Integer> map = new TreeMap<>();
2. Cultivation
Apache Commons Collections provides a powerful iterator and traversal tool class, which can simplify the collection process of the collection.Below is an example traversing ArrayList:
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
// Use Iterator to traverse
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
// Use the foreach method to traverse
list.forEach(element -> System.out.println(element));
3. Collection filtering
Apache Commons Collections provides a wealth of tools that can easily filter the collection.Below is an example of using Predicate to filter ArrayList:
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
// Use Predicate to filter the collection
CollectionUtils.filter(list, new Predicate<String>() {
@Override
public boolean evaluate(String element) {
return element.startsWith("a");
}
});
// Use lambda expression filter collection
CollectionUtils.filter(list, element -> element.startsWith("a"));
// The result of the output filtration
list.forEach(System.out::println);
4. The conversion of the collection
Apache Commons Collections also provides a tool class for set conversion, which can easily convert one collection into another.Below is an example of converting ArrayList to HashSet:
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
// Use Transformer
Set<String> set = new HashSet<>(CollectionUtils.collect(list, new Transformer<String, String>() {
@Override
public String transform(String element) {
return element.toUpperCase();
}
}));
// Use lambda expression conversion set
Set<String> set = new HashSet<>(CollectionUtils.collect(list, element -> element.toUpperCase()));
// The result of the output conversion
set.forEach(System.out::println);
Summarize:
By using Apache Commons Collections, we can more conveniently operate and handle the Java collection.It provides a variety of powerful tool categories and methods, which can greatly improve the efficiency and convenience of collective operations.It is hoped that this article can help readers understand and apply Apache Commons Collections.