vvmk 6 年 前
コミット
c9022fbf40
共有3 個のファイルを変更した45 個の追加37 個の削除を含む
  1. 2
    1
      src/main/java/ArrayListCombiner/ArrayListCombiner.java
  2. 8
    0
      src/main/java/MapFunc/MapFunc.java
  3. 35
    36
      src/test/java/MapFunc/MapFuncTest.java

+ 2
- 1
src/main/java/ArrayListCombiner/ArrayListCombiner.java ファイルの表示

@@ -10,9 +10,10 @@ import java.util.ArrayList;
10 10
  */
11 11
 public class ArrayListCombiner {
12 12
     public static <E> void superCombiner(ArrayList<? super E> parent, ArrayList<E> child) {
13
-        
13
+        parent.addAll(child);
14 14
     }
15 15
 
16 16
     public static <E> void extendCombiner(ArrayList<E> parent, ArrayList<? extends E> child) {
17
+        parent.addAll(child);
17 18
     }
18 19
 }

+ 8
- 0
src/main/java/MapFunc/MapFunc.java ファイルの表示

@@ -9,4 +9,12 @@ import java.util.function.Function;
9 9
  */
10 10
 public class MapFunc {
11 11
 
12
+    public static <T, R> ArrayList<R> map(ArrayList<T> a, Function<T, R> f) {
13
+
14
+        ArrayList<R> result = new ArrayList<>(a.size());
15
+        a.forEach(
16
+                t -> result.add(f.apply(t))
17
+        );
18
+        return result;
19
+    }
12 20
 }

+ 35
- 36
src/test/java/MapFunc/MapFuncTest.java ファイルの表示

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