ahsonali пре 6 година
родитељ
комит
a6dd279b94
2 измењених фајлова са 60 додато и 37 уклоњено
  1. 24
    1
      src/main/java/MapFunc/MapFunc.java
  2. 36
    36
      src/test/java/MapFunc/MapFuncTest.java

+ 24
- 1
src/main/java/MapFunc/MapFunc.java Прегледај датотеку

@@ -7,6 +7,29 @@ import java.util.function.Function;
7 7
  * Create a function called `map` that takes an ArrayList and a `Function<T,R>` object,
8 8
  * and returns an ArrayList with all of the elements of the first after the function is applied to them.
9 9
  */
10
-public class MapFunc {
10
+public class MapFunc
11
+{
12
+    /*
13
+
14
+    MapFunc -- Make a map method that takes an ArrayList and a
15
+    Function<T,R> object and returns an arraylist containing all of the elements
16
+    of the first with the function applied to them.
17
+
18
+     */
19
+
20
+    public static<T,R> ArrayList map(ArrayList<T> inputArrayList, Function<T,R> function)
21
+    {
22
+           ArrayList<R> values = new ArrayList<R>();
23
+
24
+           for (int i = 0; i < inputArrayList.size(); i++)
25
+           {
26
+               R output = function.apply(inputArrayList.get(i));
27
+               values.add(output);
28
+
29
+           }
30
+
31
+           return values;
32
+
33
+    }
11 34
 
12 35
 }

+ 36
- 36
src/test/java/MapFunc/MapFuncTest.java Прегледај датотеку

@@ -1,36 +1,36 @@
1
-//package MapFunc;
2
-//
3
-//import MapFunc.MapFunc;
4
-//import org.junit.Test;
5
-//
6
-//import java.util.ArrayList;
7
-//import org.junit.Assert;
8
-//
9
-//public class MapFuncTest {
10
-//    @Test
11
-//    public void testSingleTypeMap() throws Exception {
12
-//        // Given an integer array list
13
-//        ArrayList<Integer> intList = new ArrayList<>();
14
-//        intList.add(1);
15
-//        intList.add(2);
16
-//        // When it's mapped with a function to double the value
17
-//        ArrayList<Integer> mappedList = MapFunc.map(intList, num -> num*2);
18
-//        // Then all the values are doubled
19
-//        Assert.assertEquals(new Integer(2), mappedList.get(0));
20
-//        Assert.assertEquals(new Integer(4), mappedList.get(1));
21
-//    }
22
-//
23
-//    @Test
24
-//    public void testMultipleTypeMap() throws Exception {
25
-//        // Given an integer array list
26
-//        ArrayList<Integer> intList = new ArrayList<>();
27
-//        intList.add(1);
28
-//        intList.add(2);
29
-//        // When it's mapped with to string
30
-//        ArrayList<String> mappedList = MapFunc.map(intList, num -> num.toString());
31
-//        // Then all the values are doubled
32
-//        Assert.assertEquals("1", mappedList.get(0));
33
-//        Assert.assertEquals("2", mappedList.get(1));
34
-//    }
35
-//
36
-//}
1
+package MapFunc;
2
+
3
+import MapFunc.MapFunc;
4
+import org.junit.Test;
5
+
6
+import java.util.ArrayList;
7
+import org.junit.Assert;
8
+
9
+public class MapFuncTest {
10
+    @Test
11
+    public void testSingleTypeMap() throws Exception {
12
+        // Given an integer array list
13
+        ArrayList<Integer> intList = new ArrayList<>();
14
+        intList.add(1);
15
+        intList.add(2);
16
+        // When it's mapped with a function to double the value
17
+        ArrayList<Integer> mappedList = MapFunc.map(intList, num -> num*2);
18
+        // Then all the values are doubled
19
+        Assert.assertEquals(new Integer(2), mappedList.get(0));
20
+        Assert.assertEquals(new Integer(4), mappedList.get(1));
21
+    }
22
+
23
+    @Test
24
+    public void testMultipleTypeMap() throws Exception {
25
+        // Given an integer array list
26
+        ArrayList<Integer> intList = new ArrayList<>();
27
+        intList.add(1);
28
+        intList.add(2);
29
+        // When it's mapped with to string
30
+        ArrayList<String> mappedList = MapFunc.map(intList, num -> num.toString());
31
+        // Then all the values are doubled
32
+        Assert.assertEquals("1", mappedList.get(0));
33
+        Assert.assertEquals("2", mappedList.get(1));
34
+    }
35
+
36
+}