In -depth research on the iterator and iteration operation of the Ka Commons Collection frame
In -depth research on the iterator and iteration operation of the Ka Commons Collection frame
introduction:
The iterator is a common programming concept and tool that is used in Java to traverse the collection elements.Apache Commons Collections is a popular open source Java library that provides many convenient collection tools and data structures.This article will be thoroughly studied the use of iterators and iterative operations in Apache Commons Collections.
一、Apache Commons Collections
Apache Commons Collections is a Java -based open source tool library, which aims to provide more collection and tools than the Java standard library.It provides a variety of data structures, such as lists, gatherings, queues, stacks, etc., and also encapsulates a variety of convenient iterators and iterative operations.
Basic concept
1. Iterator: The iterator is an object that traverses the collection element.It provides a unified way to enable us to access the elements in the set in order without considering the underlying set type.
2. Iteraable interface: The Iterable interface is a Java built -in interface, indicating that the class that implements the interface can be traversed by Foreach.Various collection classes in the Apache Commons Collections, such as List, SET, and MAP, have implemented the ITERABLE interface, so they can be iterated.
3. iteration operation: iteration operation refers to operations performed when the iterator traverses the collection, such as obtaining the next element and deleting elements.
Third, the use of iterators
The Apache Commons Collections framework provides two main types of iterators: Iterator and Listitrator.
1. Use Iterator to traverse the collection
The following example demonstrates how to use the ITERATOR interface of Apache Commons Collections framework to traverse the collection.
import org.apache.commons.collections4.IteratorUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add ("Element 1");
list.add ("Element 2");
list.add ("Element 3");
Iterator<String> iterator = IteratorUtils.arrayIterator(list.toArray());
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
}
}
In the above example, we created an ArrayList and used the ITERTORUTILS.ARAYITERATOR method method to create an ITERATOR object of Apache Commons Collections.Then, we use the WHILE loop to traverse the collection with the Hasnext method, use the Next method to obtain each element, and print it to the console.
2. Use Listitrator to traverse the list
In addition to the conventional Iterator, Apache Commons Collections framework also provides the Listotrator interface, which expands the ITERATOR interface and provides the ability to traverse the elements in the list.The following example demonstrates how to use the Listotrator interface of Apache Commons Collections to traverse the list.
import org.apache.commons.collections4.iterators.ArrayListIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add ("Element 1");
list.add ("Element 2");
list.add ("Element 3");
ListIterator<String> listIterator = new ArrayListIterator<>(list);
while (listIterator.hasNext()) {
String nextElement = listIterator.next();
System.out.println(nextElement);
}
System.out.println ("Reverse traversal list:");
while (listIterator.hasPrevious()) {
String previousElement = listIterator.previous();
System.out.println(previousElement);
}
}
}
In the above example, we first created an ArrayList and used ArrayListIterator to create a Listitrator object of Apache Commons Collections.Then, we traversed the list with the WHILE loop and the Hasnext method, used the Next method to obtain each element, and printed it to the console.Subsequently, we used Hasprevious and Previous methods to traverse the list in the While loop.
4. Use of iteration operations
In addition to the iterators, the Apache Commons CollectionS framework also provides some convenient iterative operations, such as filters, converters and iteration tools.
1. Ieralize using filters (Filter)
The filter iteration operation allows us to filter the elements in the set and only process the elements that meet the conditions.The following example demonstrates how to use Apache Commons Collections Filter to iterate to filter the elements in the set.
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class FilterIteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add ("Element 1");
list.add ("Element 2");
list.add ("Element 3");
// Use a filter to filter out the string beginning with "element"
Predicate<String> filter = new Predicate<String>() {
@Override
public boolean evaluate(String s) {
Return S.Startswith ("Element");
}
};
Collection<String> filteredList = CollectionUtils.select(list, filter);
for (String element : filteredList) {
System.out.println(element);
}
}
}
In the above example, we first created an ArrayList and defined a filter, which only matches the string beginning with "element".Then, we filter the collection with the collection of the collection method and the filter to obtain a list of elements that meet the conditions and print it to the console.
2. Ievant operation of using converters (Transformer)
The iteration of the converter allows us to convert the elements in the set during the iteration.The following example demonstrates how to use the Apache Commons CollectionS framework to iterate iterative operation to convert the element in the collection into uppercase.
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class TransformIteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("element1");
list.add("element2");
list.add("element3");
// Define a converter to convert the element to a capital
Transformer<String, String> transformer = new Transformer<String, String>() {
@Override
public String transform(String s) {
return s.toUpperCase();
}
};
Collection<String> transformedList = CollectionUtils.collect(list, transformer);
for (String element : transformedList) {
System.out.println(element);
}
}
}
In the above example, we first created an ArrayList and defined a converter that converted the element into uppercase.Then, we use the collectionUtils.Collet method and the converter to convert the elements in the set to obtain the converted element list and print it to the console.
in conclusion:
This article deeply studies the use of iterators and iterative operations in Apache Commons Collection.We have learned how to use the iterator to traverse the set and how to use the listiterator for two -way traversal.In addition, we also introduced the use of the filter iteration operation and the use of converter iteration operations.By using the iterators and iterative operations provided by the Apache Commons Collections framework, we can more conveniently operate the elements in the set to improve the ease of readability and maintenance of the code.