diff options
author | Pacien TRAN-GIRARD | 2016-05-11 13:41:07 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-05-11 13:41:07 +0200 |
commit | f213ff8993588a9a0d63946e8a4f9b6975de3c95 (patch) | |
tree | 6a572116a400cdd53be96ffa59bce1ff8e0fb23e /src | |
parent | 26458eafb530582b744929e6b03b0042c977f7cd (diff) | |
download | xblast-f213ff8993588a9a0d63946e8a4f9b6975de3c95.tar.gz |
Add lists utils
Diffstat (limited to 'src')
-rw-r--r-- | src/ch/epfl/xblast/Lists.java | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java index 777fab2..7e3ba5e 100644 --- a/src/ch/epfl/xblast/Lists.java +++ b/src/ch/epfl/xblast/Lists.java | |||
@@ -2,7 +2,6 @@ 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; | ||
6 | import java.util.stream.Stream; | 5 | import java.util.stream.Stream; |
7 | 6 | ||
8 | /** | 7 | /** |
@@ -207,10 +206,36 @@ public final class Lists { | |||
207 | if (Objects.isNull(kl) || Objects.isNull(vl) || kl.size() != vl.size()) | 206 | if (Objects.isNull(kl) || Objects.isNull(vl) || kl.size() != vl.size()) |
208 | throw new IllegalArgumentException(); | 207 | throw new IllegalArgumentException(); |
209 | 208 | ||
210 | return Collections.unmodifiableMap(IntStream | 209 | Map<K, V> m = new HashMap<>(); |
211 | .range(0, kl.size()).mapToObj(i -> i) | 210 | Iterator<K> ki = kl.iterator(); |
212 | .filter(i -> Objects.nonNull(vl.get(i))) | 211 | Iterator<V> vi = vl.iterator(); |
213 | .collect(Collectors.toMap(kl::get, vl::get))); | 212 | |
213 | while (ki.hasNext() && vi.hasNext()) | ||
214 | m.put(ki.next(), vi.next()); | ||
215 | |||
216 | return Collections.unmodifiableMap(m); | ||
217 | } | ||
218 | |||
219 | /** | ||
220 | * Maps linearly two lists, adjusting their respective size by truncating the key list or completing the value list | ||
221 | * with null values. | ||
222 | * | ||
223 | * @param kl the keys list | ||
224 | * @param vl the values list | ||
225 | * @param <K> the key type | ||
226 | * @param <V> the value type | ||
227 | * @return the map | ||
228 | */ | ||
229 | public static <K, V> Map<K, V> linearAdjustedMap(List<K> kl, List<V> vl) { | ||
230 | if (Objects.isNull(kl) || Objects.isNull(vl)) | ||
231 | throw new IllegalArgumentException(); | ||
232 | |||
233 | if (kl.size() <= vl.size()) | ||
234 | return Lists.linearMap(kl, vl.subList(0, kl.size())); | ||
235 | else | ||
236 | return Lists.linearMap( | ||
237 | kl, | ||
238 | concatenated(Arrays.asList(vl, Collections.nCopies(kl.size() - vl.size(), null)))); | ||
214 | } | 239 | } |
215 | 240 | ||
216 | /** | 241 | /** |