I have a list of String elements that i would like to transform using FluentIterables.transform. For the sake of the example let's say it is:
List<String> l = Arrays.asList("a", "b", "c");
Now I would like to add index number to each element so the result would be:
"0a", "1b", "2c"
Is there any way to acomplish this in nice way using Guava?
FluentIterable.from(list).transform(new Function<String, String>(){
private int ct = 0;
@Override
public String apply(String input){
return ct++ + input;
}
})
While this is easy, I wouldn't necessarily call it "nice", since it's a stateful function, whereas functions should usually be stateless. But it works well enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With