diff options
author | Pacien TRAN-GIRARD | 2016-05-10 17:10:31 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-05-10 17:10:31 +0200 |
commit | 766114c05bcd0b0388988aaf606e046857e6946a (patch) | |
tree | 91ccdb1b82664c8e6d4cb5469f18da2ef08fe5e9 /src/ch/epfl | |
parent | e85032012f1d5ded1b6a49a88d345cc60a6ac88b (diff) | |
download | xblast-766114c05bcd0b0388988aaf606e046857e6946a.tar.gz |
Add lists and maps functions
Diffstat (limited to 'src/ch/epfl')
-rw-r--r-- | src/ch/epfl/xblast/Lists.java | 48 |
1 files changed, 48 insertions, 0 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; | |||
2 | 2 | ||
3 | import java.util.*; | 3 | import java.util.*; |
4 | import java.util.stream.Collectors; | 4 | import java.util.stream.Collectors; |
5 | import java.util.stream.IntStream; | ||
5 | import java.util.stream.Stream; | 6 | import java.util.stream.Stream; |
6 | 7 | ||
7 | /** | 8 | /** |
@@ -181,4 +182,51 @@ public final class Lists { | |||
181 | .collect(Collectors.toList())); | 182 | .collect(Collectors.toList())); |
182 | } | 183 | } |
183 | 184 | ||
185 | /** | ||
186 | * Maps linearly two lists. | ||
187 | * | ||
188 | * @param kl the keys list | ||
189 | * @param vl the values list | ||
190 | * @param <K> the key type | ||
191 | * @param <V> the value type | ||
192 | * @return the map | ||
193 | */ | ||
194 | public static <K, V> Map<K, V> linearMap(List<K> kl, List<V> vl) { | ||
195 | if (Objects.isNull(kl) || Objects.isNull(vl) || kl.size() != vl.size()) | ||
196 | throw new IllegalArgumentException(); | ||
197 | |||
198 | return Collections.unmodifiableMap(IntStream | ||
199 | .range(0, kl.size()).mapToObj(i -> i) | ||
200 | .filter(i -> Objects.nonNull(vl.get(i))) | ||
201 | .collect(Collectors.toMap(kl::get, vl::get))); | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * Merges two maps Map<K1, IK> and Map<IK, V> -> Map<K1, V>. | ||
206 | * | ||
207 | * @param m1 key maps | ||
208 | * @param m2 values maps | ||
209 | * @param <K> the keys type | ||
210 | * @param <IK> the intermediate keys type | ||
211 | * @param <V> the values type | ||
212 | * @return the traversing map | ||
213 | */ | ||
214 | public static <K, IK, V> Map<K, V> traverseMaps(Map<K, IK> m1, Map<IK, V> m2) { | ||
215 | return Collections.unmodifiableMap(m1.entrySet().stream() | ||
216 | .collect(Collectors.toMap(Map.Entry::getKey, e -> m2.get(e.getValue())))); | ||
217 | } | ||
218 | |||
219 | /** | ||
220 | * Returns an inverted copy of a bijective map. | ||
221 | * | ||
222 | * @param m the map to invert | ||
223 | * @param <K> the keys type | ||
224 | * @param <V> the values type | ||
225 | * @return the inverted map | ||
226 | */ | ||
227 | public static <K, V> Map<K, V> invertMap(Map<V, K> m) { | ||
228 | return Collections.unmodifiableMap(m.entrySet().stream() | ||
229 | .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))); | ||
230 | } | ||
231 | |||
184 | } | 232 | } |