forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilledMap.java
More file actions
65 lines (64 loc) · 1.92 KB
/
FilledMap.java
File metadata and controls
65 lines (64 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// onjava/FilledMap.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Fill a Map with data using a generator object
package onjava;
import java.util.*;
import java.util.function.*;
public class FilledMap<K, V> extends LinkedHashMap<K, V> {
// A single Pair Supplier:
public FilledMap(Supplier<Pair<K, V>> gen, int quantity) {
for(int i = 0; i < quantity; i++) {
Pair<K, V> p = gen.get();
put(p.key, p.value);
}
}
// Two separate Suppliers:
public FilledMap(Supplier<K> genK, Supplier<V> genV,
int quantity) {
for(int i = 0; i < quantity; i++) {
put(genK.get(), genV.get());
}
}
// A key Supplier and a single value:
public
FilledMap(Supplier<K> genK, V value, int quantity) {
for(int i = 0; i < quantity; i++) {
put(genK.get(), value);
}
}
// An Iterable and a value Supplier:
public FilledMap(Iterable<K> genK, Supplier<V> genV) {
for(K key : genK) {
put(key, genV.get());
}
}
// An Iterable and a single value:
public FilledMap(Iterable<K> genK, V value) {
for(K key : genK) {
put(key, value);
}
}
// Generic convenience methods:
public static <K, V> FilledMap<K, V>
map(Supplier<Pair<K, V>> gen, int quantity) {
return new FilledMap<>(gen, quantity);
}
public static <K, V> FilledMap<K, V>
map(Supplier<K> genK, Supplier<V> genV, int quantity) {
return new FilledMap<>(genK, genV, quantity);
}
public static <K, V> FilledMap<K, V>
map(Supplier<K> genK, V value, int quantity) {
return new FilledMap<>(genK, value, quantity);
}
public static <K, V> FilledMap<K, V>
map(Iterable<K> genK, Supplier<V> genV) {
return new FilledMap<>(genK, genV);
}
public static <K, V> FilledMap<K, V>
map(Iterable<K> genK, V value) {
return new FilledMap<>(genK, value);
}
}