Created
September 22, 2013 10:18
-
-
Save mmarcon/6658651 to your computer and use it in GitHub Desktop.
An extension of the ArrayList class that allows mapping and filtering similarly to Array.prototype.map and Array.prototype.filter in JavaScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.cloudey.webanalyzer.util; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
public class MFArrayList<E> extends ArrayList<E> { | |
private static final long serialVersionUID = 7281673743494991943L; | |
public MFArrayList(){ | |
super(); | |
} | |
public MFArrayList(int capacity){ | |
super(capacity); | |
} | |
public MFArrayList(Collection<? extends E> collection) { | |
super(collection); | |
} | |
public <T> MFArrayList<T> map(Mapper<E, T> mapper) { | |
MFArrayList<T> result = new MFArrayList<T>(); | |
for(E item : this) { | |
result.add(mapper.map(item)); | |
} | |
return result; | |
} | |
public MFArrayList<E> filter(Filter<E> filter) { | |
MFArrayList<E> result = new MFArrayList<E>(); | |
for(E item : this) { | |
if(filter.filter(item)) { | |
result.add(item); | |
} | |
} | |
return result; | |
} | |
public static abstract class Mapper<E, T>{ | |
public abstract T map(E item); | |
} | |
public static abstract class Filter<E>{ | |
public abstract boolean filter(E item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment