2

I'm trying to create an equivalent to Javascript's Array#map in Java.

I have been able to do it with

ArrayList<String> data = myList.stream().map(e -> {
    return "test "+e;
}).collect(Collectors.toCollection(ArrayList::new));

Here, myList is the initial ArrayList and data is the resulting ArrayList.

However, I find it very tedious to do that every time.

So I tried to create a generic function that would make my life easier :

public static ArrayList<?> map(ArrayList<?> list, Function<? super Object,?> callback){
    return list.stream().map(callback).collect(Collectors.toCollection(ArrayList::new));
}

And then calling it with:

ArrayList<String> data = DEF.map(myList,e -> {
    return "test "+e;
});

But I get the error

[Java] The method map(ArrayList, Function) in the type DEF is not applicable for the arguments (List, ( e) -> {})

How can I edit my generic function to accept the lambda I'm using?

1
  • 2
    Ah, it's complaining about List vs. ArrayList. You should program to interfaces, i.e. change the parameter to List<?>. Commented May 16, 2018 at 8:04

1 Answer 1

10

You should define your method with two generic type parameters - the element type of the source list, and the element type of the target list :

public static <S,T> ArrayList<T> map(ArrayList<S> list, Function<S,T> callback)
{
    return list.stream().map(callback).collect(Collectors.toCollection(ArrayList::new));
}

It would also be better to use the List interface instead of ArrayList:

public static <S,T> List<T> map(List<S> list, Function<S,T> callback)
{
    return list.stream().map(callback).collect(Collectors.toList());
}

Example of usage:

List<Integer> myList = Arrays.asList (1,2,3,4);
List<String> datai = map(myList ,e -> "test " + e);

Output:

[test 1, test 2, test 3, test 4]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.