Browse Source

refactored arraylistcombiner to meet requirements and completed mapfunc

Kaitrina High 6 years ago
parent
commit
55deb74897

+ 4
- 1
src/main/java/ArrayListCombiner/ArrayListCombiner.java View File

@@ -14,7 +14,7 @@ public class ArrayListCombiner {
14 14
         first.addAll(second);
15 15
     }
16 16
 
17
-    public static <E> void superCombiner (ArrayList<E> first, ArrayList<? extends E> second){
17
+    public static <E> void superCombiner (ArrayList<? super E> first, ArrayList<E> second){
18 18
         first.addAll(second);
19 19
     }
20 20
 }
@@ -24,4 +24,7 @@ public class ArrayListCombiner {
24 24
 //        append all of the items from the second to the first. superCombiner should use ? super E and extendCombiner
25 25
 //        should use ? extends E.
26 26
 
27
+//All the tests pass with this as the superCombiner but it's not done as asked
28
+//public static <E> void superCombiner (ArrayList<E> first, ArrayList<? extends E> second){
29
+//        first.addAll(second);
27 30
 

+ 13
- 0
src/main/java/MapFunc/MapFunc.java View File

@@ -9,4 +9,17 @@ import java.util.function.Function;
9 9
  */
10 10
 public class MapFunc {
11 11
 
12
+    public static <T, R> ArrayList<R> map(ArrayList<T> arr, Function<T, R> mapfunc) {
13
+        ArrayList outputArray = new ArrayList<>();
14
+        for (T val : arr) {
15
+            R applyFuncVal = mapfunc.apply(val);
16
+            outputArray.add(applyFuncVal);
17
+        }
18
+        return outputArray;
19
+    }
20
+
12 21
 }
22
+
23
+//MapFunc -- Make a map method that takes an ArrayList and a Function<T,R> object and returns an arraylist
24
+////        containing all of the elements of the first with the function applied to them.
25
+

+ 36
- 36
src/test/java/MapFunc/MapFuncTest.java View File

@@ -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
+}