// collectionsindepth/SuppliersCollectionTest.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 Government implements Supplier { static String[] foundation = ( "strange women lying in ponds distributing swords " + "is no basis for a system of government").split(" "); private int index; @Override public String get() { return foundation[index++]; } } public class SuppliersCollectionTest { public static void main(String[] args) { // The Suppliers class from the Generics chapter: Set set = Suppliers.create( LinkedHashSet::new, new Government(), 15); System.out.println(set); List list = Suppliers.create( LinkedList::new, new Government(), 15); System.out.println(list); list = new ArrayList<>(); Suppliers.fill(list, new Government(), 15); System.out.println(list); // Or we could just use Streams: set = Arrays.stream(Government.foundation).collect( Collectors.toSet()); System.out.println(set); list = Arrays.stream(Government.foundation).collect( Collectors.toList()); System.out.println(list); list = Arrays.stream(Government.foundation).collect( Collectors.toCollection(LinkedList::new)); System.out.println(list); set = Arrays.stream(Government.foundation).collect( Collectors.toCollection(LinkedHashSet::new)); System.out.println(set); } } /* Output: [strange, women, lying, in, ponds, distributing, swords, is, no, basis, for, a, system, of, government] [strange, women, lying, in, ponds, distributing, swords, is, no, basis, for, a, system, of, government] [strange, women, lying, in, ponds, distributing, swords, is, no, basis, for, a, system, of, government] [ponds, no, a, in, swords, for, is, basis, strange, system, government, distributing, of, women, lying] [strange, women, lying, in, ponds, distributing, swords, is, no, basis, for, a, system, of, government] [strange, women, lying, in, ponds, distributing, swords, is, no, basis, for, a, system, of, government] [strange, women, lying, in, ponds, distributing, swords, is, no, basis, for, a, system, of, government] */