-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathMapFunc.java
More file actions
24 lines (20 loc) · 872 Bytes
/
MapFunc.java
File metadata and controls
24 lines (20 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package MapFunc;
import java.util.ArrayList;
import java.util.function.Function;
/**
* Create a function called `map` that takes an ArrayList and a `Function<T,R>` object,
* and returns an ArrayList with all of the elements of the first after the function is applied to them.
*/
public class MapFunc {
//okay katrice, so public method is because this test does not create a instance of MapFunc
//The <T, R> when its static you have to define the generic so you can pass it into a static
//We are returning an Arraylist of the results and they take in an ArrayList of type T
public static <T,R> ArrayList<R> map(ArrayList<T> lists, Function<T,R> function){
ArrayList<R> result = new ArrayList<>();
for(T item: lists){
R entry = function.apply(item);
result.add(entry);
}
return result;
}
}