From eb956fb7afc8872f9260aef441a9bc1ddac8c26e Mon Sep 17 00:00:00 2001 From: Gurpreet Kaur Bagga Date: Thu, 4 Oct 2018 23:53:55 -0400 Subject: [PATCH 1/2] Add Solution in Java --- challenges/reverse/solutions/ReverseAMap.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 challenges/reverse/solutions/ReverseAMap.java diff --git a/challenges/reverse/solutions/ReverseAMap.java b/challenges/reverse/solutions/ReverseAMap.java new file mode 100644 index 0000000..9ad4b61 --- /dev/null +++ b/challenges/reverse/solutions/ReverseAMap.java @@ -0,0 +1,73 @@ +// https://github.com/WomenWhoCodeNYC/Algorithms/blob/master/challenges/reverse/reverse.md + +/* + +Level 6 + +Create a function called reverse() that takes in a set of key-value pairs +(object in Javascript, hash in Ruby, dictionary in Python, etc.) as the input and returns an object +with the same keys and values inverted. If multiple keys have the same value, it should assign of array of keys. + +Example: inputObject: {'a':1, 'b':2} + +reverse(inputObject) should return {1:'b', 2:'c'} + +inputObject2 = { "a": "1", "b": "2", "c": "1", "d": "3" } + +reverse(inputObject2) should return { "1": ["a", "c"], "2": ["b"] "3": ["d"] } + + +*/ + +import java.util.*; +import static java.util.Map.Entry; + +public class ReverseAMap { + + // imperative style of coding + public static Map reverseMapImperatively(Map inputMap){ + Map> outputMap = new HashMap<>(); + String newKey, newValue; + List list; + for(Map.Entry entry : inputMap.entrySet()) { + newKey = entry.getValue(); + newValue = entry.getKey(); + if(outputMap.containsKey(newKey)) + list = outputMap.get(newKey); + else { + list = new ArrayList(); + outputMap.put(newKey, list); + } + list.add(newValue); + } + return outputMap; + } + + // declarative style of coding + public static Map reverseMapDeclaratively(Map inputMap) { + Map> outputMap = new HashMap<>(); + inputMap.forEach((key,value) -> outputMap.merge(value, List.of(key), ReverseAMap::mergeListsOfValues)); + return outputMap; + } + + private static List mergeListsOfValues(List key1, List key2) { + List valuesList = new ArrayList<>(key1); + valuesList.addAll(key2); + return valuesList; + /* + // if need the merged list to be in a sorted order + Set set = new TreeSet<>(key1); + set.addAll(key2); + return new ArrayList<>(set); + */ + } + + public static void main(String[] args) { + + Map map = Map.of("a","1", "b","2", "c", "1", "d", "3", "as", "3"); // Java 9 + //map.put("5", "10"); + System.out.println(reverseMapImperatively(map)); + System.out.println(reverseMapDeclaratively(map)); + + } +} \ No newline at end of file From 2146312e35437a149ce0538fdce0cc754671010b Mon Sep 17 00:00:00 2001 From: Gurpreet Kaur Bagga Date: Fri, 5 Oct 2018 00:27:15 -0400 Subject: [PATCH 2/2] Update for static import Map.Entry --- challenges/reverse/solutions/ReverseAMap.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/challenges/reverse/solutions/ReverseAMap.java b/challenges/reverse/solutions/ReverseAMap.java index 9ad4b61..d972d7d 100644 --- a/challenges/reverse/solutions/ReverseAMap.java +++ b/challenges/reverse/solutions/ReverseAMap.java @@ -29,7 +29,7 @@ public static Map reverseMapImperatively(Map inputMap){ Map> outputMap = new HashMap<>(); String newKey, newValue; List list; - for(Map.Entry entry : inputMap.entrySet()) { + for(Entry entry : inputMap.entrySet()) { newKey = entry.getValue(); newValue = entry.getKey(); if(outputMap.containsKey(newKey)) @@ -63,11 +63,8 @@ private static List mergeListsOfValues(List key1, List k } public static void main(String[] args) { - Map map = Map.of("a","1", "b","2", "c", "1", "d", "3", "as", "3"); // Java 9 - //map.put("5", "10"); System.out.println(reverseMapImperatively(map)); System.out.println(reverseMapDeclaratively(map)); - } } \ No newline at end of file