瀏覽代碼

Added the map microlab

Zach Marcin 7 年之前
父節點
當前提交
4cdf175d5f
共有 3 個檔案被更改,包括 53 行新增2 行删除
  1. 5
    2
      README.md
  2. 12
    0
      src/main/java/MapFunc/MapFunc.java
  3. 36
    0
      src/test/java/MapFunc/MapFuncTest.java

+ 5
- 2
README.md 查看文件

@@ -14,7 +14,10 @@ Implement Table<K,V> so that it manages an ArrayList of Entry<K,V>.  You must im
14 14
     * `remove` which takes a key and removes it from the ArrayList if it's in there.  It's a void method; no return type.
15 15
 4. TableNested -- Take the previous microlab, and make Entry a nested class.  Think about if it'll need to be generic
16 16
 or not.
17
-5. Swap -- Get the test to pass.  Look at the specific values being passed in to help you figure it out.
17
+5. Swap -- Get the test to pass.  Look at the specific values being passed in to help you figure it out.  Modify the
18
+tests, not the code.
18 19
 6. ArrayListCombiner -- Write two methods, `superCombiner` and `extendCombiner`, which each take two arraylists and append
19 20
 all of the items from the second to the first.  `superCombiner` should use `? super E` and `extendCombiner` should use 
20
-`? extends E`.
21
+`? extends E`.
22
+7. MapFunc -- MAke a `map` method that takes an ArrayList and a `Function<T,R>` object and returns an arraylist 
23
+containing all of the elements of the first with the function applied to them.

+ 12
- 0
src/main/java/MapFunc/MapFunc.java 查看文件

@@ -0,0 +1,12 @@
1
+package MapFunc;
2
+
3
+import java.util.ArrayList;
4
+import java.util.function.Function;
5
+
6
+/**
7
+ * Create a function called `map` that takes an ArrayList and a `Function<T,R>` object,
8
+ * and returns an ArrayList with all of the elements of the first after the function is applied to them.
9
+ */
10
+public class MapFunc {
11
+
12
+}

+ 36
- 0
src/test/java/MapFunc/MapFuncTest.java 查看文件

@@ -0,0 +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
+//}