I have the following Groovy script:
def n = ["1","2","3"]
println n.collect { v -> v.toInteger()*2 }
And I would like to translate it to equivalent Java code (abstracting from the meaning of "collect" --- does not matter what it does for now).
I wrote the following:
class X {
X() {
object[] n = new object[]{"1","2","3"};
object anonymousBlock(object v) { return v.toInteger()*2; }
System.out.println(n.collect(???));
}
}
What should I pass as argument to collect using the representation above?
With Java 8, you can do:
List<String> strings = Arrays.asList( "1","2","3" ) ;
List<Integer> numbers = strings.stream()
.map( Integer::parseInt )
.map( (i) -> i * 2 )
.collect( Collectors.toList() ) ;
With Java 7, one approach is something like this:
First, define an interface to convert from one type to another:
static interface Mapper<T,U> {
U apply( T value ) ;
}
Then, we can define a class that delegates to an iterator and applies the Mapper.apply
method to each element as it is returned:
static class Collector<T,U> implements Iterator<U> {
Iterator<T> delegate ;
Mapper<T,U> mapper ;
public Collector( Iterable<T> elements, Mapper<T,U> c ) {
delegate = elements.iterator() ;
this.mapper = c ;
}
@Override
public void remove() { delegate.remove() ; }
@Override
public boolean hasNext() { return delegate.hasNext() ; }
@Override
public U next() { return mapper.apply( delegate.next() ) ; }
}
Then, we can call it, using something like:
List<String> strings = Arrays.asList( "1","2","3" ) ;
// Create our mapping Iterator
Iterator<Integer> iter = new Collector<>( strings, new Mapper<String,Integer>() {
@Override
public Integer apply( String v ) {
return Integer.parseInt( v ) * 2 ;
}
} ) ;
// Collect back from iterator into a List
List<Integer> numbers = new ArrayList<>() ;
while( iter.hasNext() ) {
numbers.add( iter.next() ) ;
}
Java 8 and Groovy FTW ;-)