The JAS-mine platform‎ > ‎Documentation‎ > ‎Cookbook‎ > ‎

Collection filters

The most basic way to filter a collection (say, a list of workers) is by iterating over its elements and check whether the filtering condition (say, whether they are employed) applies:

List<Worker> employedWorkerList = new ArrayList<Worker>();
for (Worker w : workerList)
    if ( w.getEmployed() ) employedWorkerList.add(w);

 

Collections can also be filtered without the iteration, by using the Apache CollectionUtils and Predicate libraries:

import org.apache.commons.collections.CollectionUtils;

import org.apache.commons.collections.Predicate;

and applying a JAS-mine filtering class implementing the ICollectionFilter interface, as follows:

List<Worker> employedWorkerList = new ArrayList<Worker>();
CollectionUtils.select(
     workerList, filter, employedWorkerList
);


where the filter is implemented with the following Closure:

new Predicate() {
    public boolean evaluate(Object obj) {
        Worker w = (Worker) obj;
        return (w.getEmployed());
    }
}


RMK
: At present, it is not possible to schedule an event for a filtered collection with an automatic evaluation of the filter at each scheduled time. That is,

EventGroup eventGroup = new EventGroup(); eventGroup.addCollectionEvent(CollectionUtils.select(workerList, new Predicate(){...}),         Agent.Process.DoSomething);

filters the list at t=0 -when the schedule is built- based on the characteristics of the objects in the list at t=0. If the filter has to be reevaluated at each time the call for the event is broadcasted, this must be done as a separate process, as with

eventGroup.addEvent(this, Processes.UpdateEmployedWorkerList); eventGroup.addCollectionEvent(employedWorkerList, Agent.Processes.DoSomething);

 

Starting with Java 8 (which requires Eclipse version Luna or later), it is possible to simplify further by using a Stream. A Stream is a data structure that is computed on-demand. A Stream doesn’t store data, it operates on the source data structure (collection and array) and produce pipelined data that we can use and perform specific operations. As such, we can create a Stream from the list and filter it based on a condition:
List<Worker> employedWorkerList = workerList.stream().filter(
    w -> w.getEmployed()).collect(Collectors.toList()
);