I would like to apply a function to a Java collection, in this particular case a map. Is there a nice way to do this? I have a map and would like to just run trim() on all t
I have come up with a "Mapper" class
public static abstract class Mapper {
private Collection source;
// Mapping methods
public abstract ToClass map(FromClass source);
// Constructors
public Mapper(Collection source) {
this.source = source;
}
public Mapper(FromClass ... source) {
this.source = Arrays.asList(source);
}
// Apply map on every item
public Collection apply() {
ArrayList result = new ArrayList();
for (FromClass item : this.source) {
result.add(this.map(item));
}
return result;
}
}
That I use like that :
Collection loaders = new Mapper(files) {
@Override public Loader map(File source) {
return new Loader(source);
}
}.apply();