MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / FillMap

Class FillMap

onjava/FillMap.java:10–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8import java.util.stream.*;
9
10public class FillMap {
11 public static <K, V> Map<K,V>
12 basic(Supplier<Pair<K,V>> pairGen, int size) {
13 return Stream.generate(pairGen)
14 .limit(size)
15 .collect(Collectors
16 .toMap(Pair::key, Pair::value));
17 }
18 public static <K, V> Map<K,V>
19 basic(Supplier<K> keyGen,
20 Supplier<V> valueGen, int size) {
21 return Stream.generate(
22 () -> Pair.make(keyGen.get(), valueGen.get()))
23 .limit(size)
24 .collect(Collectors
25 .toMap(Pair::key, Pair::value));
26 }
27 public static <K, V, M extends Map<K,V>>
28 M create(Supplier<K> keyGen,
29 Supplier<V> valueGen,
30 Supplier<M> mapSupplier, int size) {
31 return Stream.generate( () ->
32 Pair.make(keyGen.get(), valueGen.get()))
33 .limit(size)
34 .collect(Collectors
35 .toMap(Pair::key, Pair::value,
36 (k, v) -> k, mapSupplier));
37 }
38}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected