From 02badf65f6e80200cd135319e4031f346f8a20b7 Mon Sep 17 00:00:00 2001 From: Sumeet Agrawal Date: Mon, 21 Oct 2019 21:56:16 +0530 Subject: [PATCH 1/2] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index bf5b831..7531ec3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# This is forked reposoitory from https://github.com/stuart-marks/LambdaHOLv2.git +# Solutions will be updated # Oracle Code One Lambda Programming Laboratory ## Introduction From 64b106496ebd2da7932b92ea967185e8dddfb899 Mon Sep 17 00:00:00 2001 From: Sumeet Agrawal Date: Thu, 31 Oct 2019 09:46:00 +0530 Subject: [PATCH 2/2] Solutions updated for C_DefaultMethods --- .../test/exercises/C_DefaultMethods.java | 59 +++++++++---------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/LambdaLab/test/exercises/C_DefaultMethods.java b/LambdaLab/test/exercises/C_DefaultMethods.java index b721320..494bd7e 100644 --- a/LambdaLab/test/exercises/C_DefaultMethods.java +++ b/LambdaLab/test/exercises/C_DefaultMethods.java @@ -1,5 +1,8 @@ package exercises; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -11,10 +14,6 @@ import org.junit.Ignore; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - /** * This set of exercises covers new default methods on * the Collections and related APIs. @@ -25,7 +24,7 @@ public class C_DefaultMethods { * Given a list of StringBuilders, modify each StringBuilder * in-place by appending the string "new" to each one. */ - @Test @Ignore + @Test public void c01_appendNew() { List sbList = List.of( new StringBuilder("alfa"), @@ -33,7 +32,7 @@ public void c01_appendNew() { new StringBuilder("charlie")); // TODO write code to modify sbList - + sbList.stream().forEach(x->x.append("new")); assertEquals(List.of("alfanew", "bravonew", "charlienew"), sbList.stream() .map(StringBuilder::toString) @@ -48,13 +47,13 @@ public void c01_appendNew() { /** * Remove the words that have odd lengths from the list. */ - @Test @Ignore + @Test public void c02_removeOddLengthWords() { List list = new ArrayList<>(Arrays.asList( "alfa", "bravo", "charlie", "delta", "echo", "foxtrot")); // TODO write code to modify list - + list.removeIf(x->x.length()%2!=0); assertEquals(List.of("alfa", "echo"), list); } // Hint: @@ -66,13 +65,12 @@ public void c02_removeOddLengthWords() { /** * Replace every word in the list with its upper case equivalent. */ - @Test @Ignore + @Test public void c03_upcaseAllWords() { List list = Arrays.asList( "alfa", "bravo", "charlie", "delta", "echo", "foxtrot"); - + list.replaceAll(String::toUpperCase); // TODO code to modify list - assertEquals(List.of("ALFA", "BRAVO", "CHARLIE", "DELTA", "ECHO", "FOXTROT"), list); } @@ -87,7 +85,7 @@ public void c03_upcaseAllWords() { * append to each StringBuilder the string representation of its corresponding * Integer key. This should mutate each StringBuilder value in-place. */ - @Test @Ignore + @Test public void c04_appendToMapValues() { Map map = new TreeMap<>(); map.put(1, new StringBuilder("alfa")); @@ -95,7 +93,7 @@ public void c04_appendToMapValues() { map.put(3, new StringBuilder("charlie")); // TODO write code to modify map - + map.forEach((k,v)->v.append(k)); assertEquals(3, map.size()); assertTrue(map.values().stream().allMatch(x -> x instanceof StringBuilder)); assertEquals("alfa1", map.get(1).toString()); @@ -113,7 +111,7 @@ public void c04_appendToMapValues() { * append to each String the string representation of its corresponding * Integer key. */ - @Test @Ignore + @Test public void c05_replaceMapValues() { Map map = new TreeMap<>(); map.put(1, "alfa"); @@ -121,7 +119,7 @@ public void c05_replaceMapValues() { map.put(3, "charlie"); // TODO write code to modify map - + map.replaceAll((k,v)->v+k); assertEquals(Map.of(1, "alfa1", 2, "bravo2", 3, "charlie3"), @@ -137,16 +135,15 @@ public void c05_replaceMapValues() { * Given a list of words, populate a map whose keys are the lengths of * each word, and whose values are list of words with that length. */ - @Test @Ignore + @Test public void c06_mapOfListOfStringsByLength() { List list = List.of( "aardvark", "bison", "capybara", "alligator", "bushbaby", "chimpanzee", "avocet", "bustard", "capuchin"); Map> result = new TreeMap<>(); - + list.forEach(s->result.computeIfAbsent(s.length(), k -> new ArrayList<>()).add(s)); // TODO write code to populate result - assertEquals(Map.of( 5, List.of("bison"), 6, List.of("avocet"), 7, List.of("bustard"), @@ -165,16 +162,15 @@ public void c06_mapOfListOfStringsByLength() { * initial character. When concatenating the words, they should be * separated by a colon (':'). */ - @Test @Ignore + @Test public void c07_mapOfStringByInitialCharacter() { List list = List.of( "aardvark", "bison", "capybara", "alligator", "bushbaby", "chimpanzee", "avocet", "bustard", "capuchin"); Map result = new TreeMap<>(); - // TODO write code to populate result - + list.forEach(x->result.merge(x.charAt(0), x, (y1,y2)->y1+":"+y2)); assertEquals(Map.of('a', "aardvark:alligator:avocet", 'b', "bison:bushbaby:bustard", 'c', "capybara:chimpanzee:capuchin"), @@ -192,7 +188,7 @@ public void c07_mapOfStringByInitialCharacter() { * null, and we need to add checks to protect against NullPointerException. * Write code to ensure that all missing keys are mapped to the empty string. */ - @Test @Ignore + @Test public void c08_mapWithMissingValues() { List keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g"); Map map = new HashMap<>(Map.of("a", "alfa", @@ -201,7 +197,7 @@ public void c08_mapWithMissingValues() { "d", "delta")); // TODO write code to fix the map - + keys.forEach(k->map.putIfAbsent(k, "")); assertEquals(Map.of("a", "alfa", "b", "bravo", "c", "charlie", @@ -222,7 +218,7 @@ public void c08_mapWithMissingValues() { * We've now determined that's incorrect, and we want to undo that. This * time, we want to remove the entry if the value is the empty string. */ - @Test @Ignore + @Test public void c09_mapRemoveEntriesWithEmptyValues() { List keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g"); Map map = new HashMap<>(Map.of("a", "alfa", @@ -234,7 +230,7 @@ public void c09_mapRemoveEntriesWithEmptyValues() { "g", "")); // TODO write code to fix the map - + keys.forEach(k->map.remove(k,"")); assertEquals(Map.of("a", "alfa", "b", "bravo", "c", "charlie", @@ -253,7 +249,7 @@ public void c09_mapRemoveEntriesWithEmptyValues() { * to replace the empty-string values with a value that's the key itself. * Write the code to do that. */ - @Test @Ignore + @Test public void c10_mapReplaceEmptyValues() { List keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g"); Map map = new HashMap<>(Map.of("a", "alfa", @@ -265,7 +261,7 @@ public void c10_mapReplaceEmptyValues() { "g", "")); // TODO write code to fix the map - + keys.forEach(k->map.replace(k, "", k)); assertEquals(Map.of("a", "alfa", "b", "bravo", "c", "charlie", @@ -287,16 +283,15 @@ public void c10_mapReplaceEmptyValues() { * that are not present, we want to add an entry where the value is the * same as the key. */ - @Test @Ignore + @Test public void c11_computeWithMissingEntries() { List keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g"); Map map = new HashMap<>(Map.of("a", "alfa", "b", "bravo", "c", "charlie", "d", "delta")); - // TODO write code transform the map - + keys.forEach(k->map.compute(k, (key,value)->value==null?k:value.toUpperCase())); assertEquals(Map.of("a", "ALFA", "b", "BRAVO", "c", "CHARLIE", @@ -319,7 +314,7 @@ public void c11_computeWithMissingEntries() { * the non-empty values to upper case, but we want to remove the entries * for which the values are the empty string. */ - @Test @Ignore + @Test public void c12_computeAndRemoveSomeEntries() { List keys = Arrays.asList("a", "b", "c", "d", "e", "f", "g"); Map map = new HashMap<>(Map.of("a", "alfa", @@ -331,7 +326,7 @@ public void c12_computeAndRemoveSomeEntries() { "g", "")); // TODO write code transform the map - + keys.forEach(key->map.compute(key, (k,v)->v.isEmpty()?null:v.toUpperCase())); assertEquals(Map.of("a", "ALFA", "b", "BRAVO", "c", "CHARLIE",