public static void main(String[] args) { // Suppliers class from the Generics chapter: Set<String> set = Suppliers.create( LinkedHashSet::new, new Government(), 15); System.out.println(set); List<String> 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 can 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); } }
public static void main(String[] args) { BerylliumSphere[] spheres = new BerylliumSphere[10]; for(int i = 0; i < 5; i++) spheres[i] = new BerylliumSphere(); show(spheres); System.out.println(spheres[4]); List<BerylliumSphere> sphereList = Suppliers.create( ArrayList::new, BerylliumSphere::new, 5); System.out.println(sphereList); System.out.println(sphereList.get(4)); int[] integers = { 0, 1, 2, 3, 4, 5 }; show(integers); System.out.println(integers[4]); List<Integer> intList = new ArrayList<>( Arrays.asList(0, 1, 2, 3, 4, 5)); intList.add(97); System.out.println(intList); System.out.println(intList.get(4)); } }
public static void main(String[] args) throws Exception { List<Shape> shapes = Suppliers.create(ArrayList::new, Shape::new, 3); Apply.apply(shapes, Shape.class.getMethod("rotate")); Apply.apply(shapes, Shape.class.getMethod("resize", int.class), 7); List<Square> squares = Suppliers.create(ArrayList::new, Square::new, 3); Apply.apply(squares, Shape.class.getMethod("rotate")); Apply.apply(squares, Shape.class.getMethod("resize", int.class), 7); Apply.apply(new FilledList<>(Shape::new, 3), Shape.class.getMethod("rotate")); Apply.apply(new FilledList<>(Square::new, 3), Shape.class.getMethod("rotate")); SimpleQueue<Shape> shapeQ = Suppliers.fill( new SimpleQueue<>(), SimpleQueue::add, Shape::new, 3); Suppliers.fill(shapeQ, SimpleQueue::add, Square::new, 3); Apply.apply(shapeQ, Shape.class.getMethod("rotate")); } }
public static void main(String[] args) { // Demonstrate create(): RandomList<Teller> tellers = Suppliers.create( RandomList::new, Teller::new, 4); // Demonstrate fill(): List<Customer> customers = Suppliers.fill( new ArrayList<>(), Customer::new, 12); customers.forEach(c -> serve(tellers.select(), c)); // Demonstrate assisted latent typing: Bank bank = Suppliers.fill( new Bank(), Bank::put, BankTeller::new, 3); // Can also use second version of fill(): List<Customer> customers2 = Suppliers.fill( new ArrayList<>(), List::add, Customer::new, 12); } }