// collectionsindepth/StreamFillMaps.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. import java.util.*; import java.util.function.*; import java.util.stream.*; import onjava.*; class Letters implements Supplier> { private int number = 1; private char letter = 'A'; @Override public Pair get() { return new Pair<>(number++, "" + letter++); } } public class StreamFillMaps { public static void main(String[] args) { Map m = Stream.generate(new Letters()) .limit(11) .collect(Collectors.toMap(Pair::key, Pair::value)); System.out.println(m); // Two separate Suppliers: Rand.String rs = new Rand.String(3); Count.Character cc = new Count.Character(); Map mcs = Stream.generate( () -> Pair.make(cc.get(), rs.get())) .limit(8) .collect(Collectors.toMap(Pair::key, Pair::value)); System.out.println(mcs); // A key Supplier and a single value: Map mcs2 = Stream.generate( () -> Pair.make(cc.get(), "Val")) .limit(8) .collect(Collectors.toMap(Pair::key, Pair::value)); System.out.println(mcs2); } } /* Output: {1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I, 10=J, 11=K} {b=btp, c=enp, d=ccu, e=xsz, f=gvg, g=mei, h=nne, i=elo} {p=Val, q=Val, j=Val, k=Val, l=Val, m=Val, n=Val, o=Val} */