From 766114c05bcd0b0388988aaf606e046857e6946a Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 10 May 2016 17:10:31 +0200 Subject: Add lists and maps functions --- src/ch/epfl/xblast/Lists.java | 48 ++++++++++++++++++++++++++++++++++++++ test/ch/epfl/xblast/ListsTest.java | 47 +++++++++++++++++++++++++++++++++---- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java index cc6a1c8..bf0cafa 100644 --- a/src/ch/epfl/xblast/Lists.java +++ b/src/ch/epfl/xblast/Lists.java @@ -2,6 +2,7 @@ package ch.epfl.xblast; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; /** @@ -181,4 +182,51 @@ public final class Lists { .collect(Collectors.toList())); } + /** + * Maps linearly two lists. + * + * @param kl the keys list + * @param vl the values list + * @param the key type + * @param the value type + * @return the map + */ + public static Map linearMap(List kl, List vl) { + if (Objects.isNull(kl) || Objects.isNull(vl) || kl.size() != vl.size()) + throw new IllegalArgumentException(); + + return Collections.unmodifiableMap(IntStream + .range(0, kl.size()).mapToObj(i -> i) + .filter(i -> Objects.nonNull(vl.get(i))) + .collect(Collectors.toMap(kl::get, vl::get))); + } + + /** + * Merges two maps Map and Map -> Map. + * + * @param m1 key maps + * @param m2 values maps + * @param the keys type + * @param the intermediate keys type + * @param the values type + * @return the traversing map + */ + public static Map traverseMaps(Map m1, Map m2) { + return Collections.unmodifiableMap(m1.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> m2.get(e.getValue())))); + } + + /** + * Returns an inverted copy of a bijective map. + * + * @param m the map to invert + * @param the keys type + * @param the values type + * @return the inverted map + */ + public static Map invertMap(Map m) { + return Collections.unmodifiableMap(m.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))); + } + } diff --git a/test/ch/epfl/xblast/ListsTest.java b/test/ch/epfl/xblast/ListsTest.java index b18b05e..e62eede 100644 --- a/test/ch/epfl/xblast/ListsTest.java +++ b/test/ch/epfl/xblast/ListsTest.java @@ -2,10 +2,7 @@ package ch.epfl.xblast; import org.junit.Test; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -63,4 +60,46 @@ public class ListsTest { assertEquals(expected, Lists.permutations(sampleList)); } + @Test + public void areListsMapped() { + final List keyList = Arrays.asList('a', 'b'); + final List valueList = Arrays.asList(0, 1); + + final Map expectedMap = new HashMap<>(); + expectedMap.put('a', 0); + expectedMap.put('b', 1); + + assertEquals(expectedMap, Lists.linearMap(keyList, valueList)); + } + + @Test + public void areMapsTraversed() { + final Map m1 = new HashMap<>(); + m1.put('a', 0); + m1.put('b', 1); + + final Map m2 = new HashMap<>(); + m2.put(0, "Apple"); + m2.put(1, "Banana"); + + final Map expectedMap = new HashMap<>(); + expectedMap.put('a', "Apple"); + expectedMap.put('b', "Banana"); + + assertEquals(expectedMap, Lists.traverseMaps(m1, m2)); + } + + @Test + public void isMapInverted() { + final Map sampleMap = new HashMap<>(); + sampleMap.put('a', 0); + sampleMap.put('b', 1); + + final Map expectedMap = new HashMap<>(); + expectedMap.put(0, 'a'); + expectedMap.put(1, 'b'); + + assertEquals(expectedMap, Lists.invertMap(sampleMap)); + } + } -- cgit v1.2.3