// 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 extends LinkedHashMap { // A single Pair Supplier: public FilledMap(Supplier> gen, int quantity) { for(int i = 0; i < quantity; i++) { Pair p = gen.get(); put(p.key, p.value); } } // Two separate Suppliers: public FilledMap(Supplier genK, Supplier 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 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 genK, Supplier genV) { for(K key : genK) { put(key, genV.get()); } } // An Iterable and a single value: public FilledMap(Iterable genK, V value) { for(K key : genK) { put(key, value); } } // Generic convenience methods: public static FilledMap map(Supplier> gen, int quantity) { return new FilledMap<>(gen, quantity); } public static FilledMap map(Supplier genK, Supplier genV, int quantity) { return new FilledMap<>(genK, genV, quantity); } public static FilledMap map(Supplier genK, V value, int quantity) { return new FilledMap<>(genK, value, quantity); } public static FilledMap map(Iterable genK, Supplier genV) { return new FilledMap<>(genK, genV); } public static FilledMap map(Iterable genK, V value) { return new FilledMap<>(genK, value); } }