├── .gitattributes ├── 9781484242773.jpg ├── Contributing.md ├── LICENSE.txt ├── README.md ├── chapter1 ├── C1.java ├── I1.java ├── I2.java ├── NamedStringProcessor.java ├── Pet.java ├── PlayfulPets.java ├── Receipt.java ├── ReceiptPrinter.java ├── Sect5_Ex1.java ├── StringProcessor.java ├── TestStringProcessor.java ├── TwoArgsProcessor.java ├── TwoIntsProcessor.java ├── incorrect │ └── StringProcessor.java └── version2 │ ├── CountyReceipt.java │ ├── Receipt.java │ ├── ReceiptPrinter.java │ ├── Sect6_Ex1.java │ ├── Sect7a_Ex1.java │ └── StringProcessor.java ├── chapter10 ├── RealEstateBroker.java ├── Sect10_Ex1.java ├── Sect1_Ex1.java ├── Sect1_Ex2.java ├── Sect1_Ex3.java ├── Sect2_Ex1.java ├── Sect2_Ex2.java ├── Sect2_Ex3.java ├── Sect2_Ex4.java ├── Sect2_Ex5.java ├── Sect3_Ex1.java ├── Sect3_Ex2.java ├── Sect3_Ex3.java ├── Sect3_Ex4.java ├── Sect3_Ex5.java ├── Sect4_Ex1.java ├── Sect4_Ex2.java ├── Sect5_Ex1.java ├── Sect5_Ex2.java ├── Sect5_Ex3.java ├── Sect5_Ex4.java ├── Sect5_Ex5.java ├── Sect5_Ex6.java ├── Sect6_Ex1.java ├── Sect6_Ex2.java ├── Sect7_Ex1.java ├── Sect7_Ex2.java ├── Sect7_Ex3.java ├── Sect8_Ex1.java ├── Sect8_Ex2.java ├── Sect8_Ex3.java ├── Sect9_Ex1.java ├── Sect9_Ex2.java ├── Sect9_Ex3.java ├── Sect9_Ex4.java └── Student.java ├── chapter11 ├── GuessANumber.java ├── Sect1_Ex1.java ├── Sect1_Ex2.java ├── Sect2_Ex1.java ├── Sect2_Ex2.java ├── Sect3_Ex1.java ├── Sect3_Ex2.java ├── Sect4_Ex1.java ├── Sect4_Ex2.java ├── Sect4_Ex3.java ├── Sect5_Ex1.java ├── Sect6_Ex1.java ├── Sect6_Ex2.java ├── Sect7_Ex1.java ├── Sect7_Ex2.java ├── Sect7_Ex3.java ├── Sect7_Ex4.java └── Sect7_Ex5.java ├── chapter12 ├── Inventory.java ├── Sect10_Ex1.java ├── Sect10_Ex2.java ├── Sect11_Ex1.java ├── Sect12_Ex1.java ├── Sect12_Ex2.java ├── Sect12_Ex3.java ├── Sect2_Ex1.java ├── Sect3_Ex1.java ├── Sect3_Ex3.java ├── Sect3_Ex4.java ├── Sect4_Ex1.java ├── Sect4_Ex2.java ├── Sect5_Ex1.java ├── Sect5_Ex2.java ├── Sect6_Ex1.java ├── Sect6_Ex2.java ├── Sect7_Ex1.java ├── Sect7_Ex2.java ├── Sect8_Ex1.java ├── Sect8_Ex2.java ├── Sect9_Ex1.java ├── Sect9_Ex2.java ├── Sect9_Ex3.java ├── Sect9a_Ex1.java ├── Sect9a_Ex2.java ├── Sect9a_Ex3.java ├── Sect9a_Ex4.java ├── Sect9a_Ex5.java ├── Sect9a_Ex6.java ├── Sect9a_Ex7.java ├── Sect9a_Ex8.java └── Sect9a_Ex9.java ├── chapter13 ├── AllOf.java ├── MySentence.java ├── Sect10_Ex1.java ├── Sect1_Ex1.java ├── Sect1_Ex2.java ├── Sect1_Ex3.java ├── Sect1_Ex4.java ├── Sect1a_Ex1.java ├── Sect2_Ex1.java ├── Sect4_Ex1.java ├── Sect4_Ex2.java ├── Sect4_Ex3.java ├── Sect5_Ex1.java ├── Sect5_Ex2.java ├── Sect6_Ex1.java ├── Sect7_Ex1.java ├── Sect8_Ex1.java ├── Sect8_Ex2.java ├── Sect8_Ex3.java ├── Sect9_Ex1.java ├── Sect9_Ex2.java └── SentenceBuilder.java ├── chapter14 ├── BankAccount.java ├── Safe.java ├── Sect1a_Ex1.java ├── Sect1a_Ex2.java ├── Sect1b_Ex1.java ├── Sect1b_Ex2.java ├── Sect1c_Ex1.java ├── Sect2_Ex1.java ├── Sect3_Ex1.java ├── Sect4_Ex1.java ├── Sect5_Ex1.java ├── Sect5_Ex2.java ├── ShareAnInt.java └── Unsafe.java ├── chapter15 ├── DMVGUI.java ├── TestBuilder.java ├── TestBuilderFactory.java ├── TestEventHandler.java ├── TestInvalidation.java ├── TestListChangeListener.java ├── TestMapChangeListener.java └── TestSetChangeListener.java ├── chapter2 ├── ComputeSquare.java ├── FI3.java ├── FIVoid.java ├── Receipt.java ├── ReceiptPrinter.java ├── Sect2_Ex1.java ├── Sect2_Ex2.java ├── Sect4_Ex1.java ├── Sect4_Ex2.java ├── Sect5_Ex1.java ├── Sect5_Ex2.java ├── Sect5_Ex4.java ├── Sect5_Ex6.java ├── Sect5_Ex7.java ├── Sect6_Ex1.java ├── Sect6_Ex3.java ├── StringProcessor.java ├── TestLambdaScope.java ├── TwoArgsProcessor.java ├── incorrect │ ├── FIVoid.java │ ├── Sect5_Ex3.java │ └── TestLambdaScope.java └── version2 │ ├── Sect6_Ex2.java │ └── StringProcessor.java ├── chapter3 └── Sect5e_Ex1.java ├── chapter4 ├── NumberParser.java ├── SalesPromotion.java ├── Sect10_Ex1.java ├── Sect1_Ex1.java ├── Sect3a_Ex1.java ├── Sect3b_Ex1.java ├── Sect4_Ex1.java ├── Sect5_Ex1.java ├── Sect6_Ex1.java ├── Sect7_Ex1.java ├── Sect8_Ex1.java ├── Sect9_Ex1.java └── Transformer.java ├── chapter5 ├── Calculator.java ├── Sect1_Ex1.java ├── Sect2_Ex1.java ├── Sect2a_Ex1.java ├── Sect3_Ex1.java └── Sect4_Ex1.java ├── chapter6 ├── BankTransactions.java ├── ComputePolynomial.java ├── ConsumerSpecials.java ├── Sect6_Ex1.java ├── TestBiConsumer.java ├── TestConsumer.java └── TestConsumerAndThen.java ├── chapter7 ├── Sect2_Ex1.java ├── TestSpecials.java ├── TestSupplier.java └── TicketingSystem.java ├── chapter8 ├── Car.java ├── DoubleIterator.java ├── IntIterator.java ├── LongIterator.java ├── Payroll.java ├── Sect1_Ex1.java ├── Sect1_Ex2.java ├── Sect3_Ex1.java ├── Sect4_Ex1.java ├── Sect6_Ex1.java ├── TestMyInts.java ├── TestMyIntsP.java ├── TestPrimitiveIteratorGen.java └── TestPrimitiveIteratorSpecializations.java ├── chapter9 ├── DMV.java ├── Sect1_Ex1.java ├── Sect2_Ex1.java ├── Sect2_Ex2.java ├── Sect2_Ex3.java ├── Sect2_Ex4.java ├── Sect3_Ex1.java ├── Sect3_Ex2.java ├── Sect4_Ex1.java ├── Sect5_Ex1.java ├── Sect5_Ex2.java ├── Sect5_Ex3.java └── Sect6_Ex1.java └── errata.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484242773.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/functional-interfaces-in-java/6d10939f5c0caf74a31dee5f489990a1827331a2/9781484242773.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2019 Ralph Lecessi 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Functional Interfaces in Java*](https://www.apress.com/9781484242773) by Ralph Lecessi (Apress, 2019). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484242773.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /chapter1/C1.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | public class C1 implements I1 3 | { 4 | @Override 5 | public void method1() {} 6 | @Override 7 | public String method2(String x) { return x; } 8 | } 9 | -------------------------------------------------------------------------------- /chapter1/I1.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | public interface I1 3 | { 4 | void method1(); 5 | String method2(String x); 6 | } 7 | -------------------------------------------------------------------------------- /chapter1/I2.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | public interface I2 3 | { 4 | final String s = "I2"; 5 | static void method1() 6 | { 7 | System.out.println(s); 8 | } 9 | 10 | default String method2(String x) 11 | { 12 | return s + x; 13 | } 14 | } 15 | 16 | class C2 implements I2 17 | { 18 | @Override 19 | public String method2(String x) { return x; } 20 | } 21 | 22 | class C3 implements I2 {} 23 | 24 | class TestI2 25 | { 26 | public static void main(String[] args) 27 | { 28 | I2.method1(); 29 | I2 objC2 = new C2(); 30 | I2 objC3 = new C3(); 31 | 32 | System.out.println(objC2.method2("Hello")); 33 | System.out.println(objC3.method2("World")); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /chapter1/NamedStringProcessor.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | public class NamedStringProcessor implements StringProcessor 3 | { 4 | @Override 5 | public String process(String s) { return s; } 6 | } 7 | -------------------------------------------------------------------------------- /chapter1/Pet.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | import java.util.*; 3 | public class Pet 4 | { 5 | String name; 6 | String animal; 7 | String breed; 8 | String color; 9 | double price; 10 | static List pets = new ArrayList<>(); 11 | public Pet(String n, String a, String b, String c, double p) 12 | { 13 | name = n; 14 | animal = a; 15 | breed = b; 16 | color = c; 17 | price = p; 18 | } 19 | @Override 20 | public String toString() 21 | { 22 | return name + ":" + " a " + color + " " + breed 23 | + " " + animal + " for $" + price; 24 | } 25 | 26 | @Override 27 | public boolean equals(Object o) 28 | { 29 | Pet p = (Pet)o; 30 | return animal.equals(p.animal) && breed.equals(p.breed); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /chapter1/PlayfulPets.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | import java.util.*; 3 | 4 | @FunctionalInterface 5 | interface PetMatcher 6 | { 7 | List match(Pet pet); 8 | default Pet first(Pet pet) 9 | { 10 | int index = Pet.pets.indexOf(pet); 11 | return index > -1? Pet.pets.get(index) : null; 12 | } 13 | } 14 | 15 | 16 | 17 | 18 | public class PlayfulPets 19 | { 20 | private static void matchPet(String criteria, 21 | PetMatcher matcher, Pet pet) 22 | { 23 | System.out.println("\n" + criteria + ":"); 24 | System.out.println("First: " + matcher.first(pet)); 25 | System.out.println("All matches:"); 26 | List matches = matcher.match(pet); 27 | for (Pet p : matches) 28 | System.out.println(p); 29 | } 30 | 31 | public static void main(String[] args) 32 | { 33 | Pet.pets.add(new Pet("Scruffy","dog","poodle", 34 | "white",895.00)); 35 | Pet.pets.add(new Pet("Meow","cat","siamese","white",740.25)); 36 | Pet.pets.add(new Pet("Max","dog","poodle","black",540.50)); 37 | Pet.pets.add(new Pet("Cuddles","dog","pug","black",1282.75)); 38 | Pet.pets.add(new Pet("Slider","snake","garden", 39 | "green",320.00)); 40 | 41 | PetMatcher breedMatcher = new PetMatcher() { 42 | public List match(Pet pet) 43 | { 44 | List matches = new ArrayList<>(); 45 | for (Pet p : Pet.pets) 46 | if (p.equals(pet)) 47 | matches.add(p); 48 | return matches; 49 | } 50 | }; 51 | 52 | PetMatcher priceMatcher = new PetMatcher() { 53 | public List match(Pet pet) 54 | { 55 | List matches = new ArrayList<>(); 56 | for (Pet p : Pet.pets) 57 | if (p.price <= pet.price) 58 | matches.add(p); 59 | return matches; 60 | } 61 | public Pet first(Pet pet) 62 | { 63 | int index = -1; 64 | for(Pet p : Pet.pets) 65 | if (p.price <= pet.price) 66 | return p; 67 | return null; 68 | } 69 | }; 70 | 71 | matchPet("Poodles",breedMatcher, 72 | new Pet(null, "dog", "poodle", null, 0.0)); 73 | matchPet("Pets for $800 or less",priceMatcher, 74 | new Pet(null, null, null, null, 800.0)); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /chapter1/Receipt.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | public class Receipt 3 | { 4 | String item; 5 | double price; 6 | double discount; 7 | double tax; 8 | public Receipt(String i, double a, double d, double s) 9 | { 10 | item = i; 11 | price = a; 12 | discount = d; 13 | tax = s; 14 | } 15 | public Receipt(Receipt r) 16 | { 17 | item = r.item; 18 | price = r.price; 19 | discount = r.discount; 20 | tax = r.tax; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chapter1/ReceiptPrinter.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | @FunctionalInterface 3 | public interface ReceiptPrinter 4 | { 5 | void print(Receipt receipt); 6 | 7 | default double computeTotal(Receipt receipt) 8 | { 9 | double discountedPrice = receipt.price 10 | - (receipt.price * receipt.discount); 11 | return discountedPrice + (discountedPrice * receipt.tax); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /chapter1/Sect5_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | public class Sect5_Ex1 3 | { 4 | public static void main(String[] args) 5 | { 6 | 7 | ReceiptPrinter simpleReceiptPrinter = new ReceiptPrinter() { 8 | @Override 9 | public void print(Receipt receipt) 10 | { 11 | System.out.println("\nItem :\t" + receipt.item); 12 | System.out.println("Price:\t" + receipt.price); 13 | System.out.println("Disc:\t" + receipt.discount); 14 | System.out.println("Tax:\t" + receipt.tax); 15 | System.out.println("Total:\t" + computeTotal(receipt)); 16 | } 17 | }; 18 | 19 | ReceiptPrinter exemptReceiptPrinter = new ReceiptPrinter() { 20 | @Override 21 | public void print(Receipt receipt) 22 | { 23 | System.out.println("\nItem :\t" + receipt.item); 24 | System.out.println("Price:\t" + receipt.price); 25 | System.out.println("Disc:\t" + receipt.discount); 26 | System.out.println("Total:\t" + computeTotal(receipt)); 27 | } 28 | public double computeTotal(Receipt receipt) 29 | { 30 | return receipt.price - (receipt.price * receipt.discount); 31 | } 32 | }; 33 | 34 | Receipt receipt = new Receipt("shirt", 20.00, 0.05, 0.07); 35 | simpleReceiptPrinter.print(receipt); 36 | exemptReceiptPrinter.print(receipt); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /chapter1/StringProcessor.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | @FunctionalInterface 3 | public interface StringProcessor 4 | { 5 | String process(String x); 6 | } 7 | -------------------------------------------------------------------------------- /chapter1/TestStringProcessor.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | public class TestStringProcessor 3 | { 4 | public static void main(String[] args) 5 | { 6 | NamedStringProcessor namedSP = new NamedStringProcessor(); 7 | 8 | StringProcessor anonSP = new StringProcessor() { 9 | @Override 10 | public String process(String x) 11 | { 12 | return x.toUpperCase(); 13 | } 14 | }; 15 | 16 | System.out.println(namedSP.process("hello")); 17 | System.out.println(anonSP.process("hello")); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /chapter1/TwoArgsProcessor.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | @FunctionalInterface 3 | public interface TwoArgsProcessor { 4 | X process(X arg1, X arg2); 5 | } 6 | 7 | class TestTwoArgsProcessor { 8 | public static void main(String[] args) 9 | { 10 | TwoArgsProcessor multiplyInts 11 | = new TwoArgsProcessor<>() { 12 | @Override 13 | public Integer process(Integer arg1, Integer arg2) 14 | { 15 | return arg1 * arg2; 16 | } 17 | }; 18 | 19 | TwoArgsProcessor addDoubles 20 | = new TwoArgsProcessor<>() { 21 | @Override 22 | public Double process(Double arg1, Double arg2) 23 | { 24 | return arg1 + arg2; 25 | } 26 | }; 27 | 28 | TwoArgsProcessor compareStrings 29 | = new TwoArgsProcessor<>() { 30 | @Override 31 | public String process(String arg1, String arg2) 32 | { 33 | return arg1.compareTo(arg2) > 0? arg1: arg2; 34 | } 35 | }; 36 | 37 | System.out.println(multiplyInts.process(2,3)); 38 | System.out.println(addDoubles.process(4.1,5.2)); 39 | System.out.println(compareStrings.process("ace","age")); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /chapter1/TwoIntsProcessor.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | @FunctionalInterface 3 | public interface TwoIntsProcessor 4 | extends TwoArgsProcessor 5 | { 6 | } 7 | 8 | abstract class TwoIntsProcessorAbstract 9 | implements TwoArgsProcessor 10 | { 11 | } 12 | 13 | class TestTwoIntsProcessor { 14 | public static void main(String[] args) 15 | { 16 | TwoIntsProcessor multiplyInts = new TwoIntsProcessor() { 17 | @Override public Integer process(Integer arg1, 18 | Integer arg2) 19 | { 20 | return arg1 * arg2; 21 | } 22 | }; 23 | 24 | TwoIntsProcessor subtractInts = new TwoIntsProcessor() { 25 | @Override public Integer process(Integer arg1, 26 | Integer arg2) 27 | { 28 | return arg1 - arg2; 29 | } 30 | }; 31 | 32 | TwoIntsProcessorAbstract divideInts 33 | = new TwoIntsProcessorAbstract() { 34 | @Override public Integer process(Integer arg1, 35 | Integer arg2) 36 | { 37 | return arg1 / arg2; 38 | } 39 | }; 40 | 41 | System.out.println(multiplyInts.process(2,3)); 42 | System.out.println(subtractInts.process(5,2)); 43 | System.out.println(divideInts.process(10,2)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /chapter1/incorrect/StringProcessor.java: -------------------------------------------------------------------------------- 1 | package chapter1.incorrect; 2 | @FunctionalInterface //ERROR: Not a functional interface. 3 | // Contains more than one abstract 4 | // method. 5 | public interface StringProcessor 6 | { 7 | String process(String x); 8 | String process2(String x); 9 | } 10 | -------------------------------------------------------------------------------- /chapter1/version2/CountyReceipt.java: -------------------------------------------------------------------------------- 1 | package chapter1.version2; 2 | public class CountyReceipt extends Receipt 3 | { 4 | double countyTax; 5 | public CountyReceipt(Receipt r, double c) 6 | { 7 | super(r); 8 | countyTax = c; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /chapter1/version2/Receipt.java: -------------------------------------------------------------------------------- 1 | package chapter1.version2; 2 | public class Receipt 3 | { 4 | String item; 5 | double price; 6 | double discount; 7 | double tax; 8 | public Receipt(String i, double a, double d, double s) 9 | { 10 | item = i; 11 | price = a; 12 | discount = d; 13 | tax = s; 14 | } 15 | public Receipt(Receipt r) 16 | { 17 | item = r.item; 18 | price = r.price; 19 | discount = r.discount; 20 | tax = r.tax; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chapter1/version2/ReceiptPrinter.java: -------------------------------------------------------------------------------- 1 | package chapter1.version2; 2 | @FunctionalInterface 3 | public interface ReceiptPrinter 4 | { 5 | void print(X receipt); 6 | 7 | default double computeTotal(X receipt) 8 | { 9 | double discountedPrice = receipt.price 10 | - (receipt.price * receipt.discount); 11 | return discountedPrice + (discountedPrice * receipt.tax); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /chapter1/version2/Sect6_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter1.version2; 2 | public class Sect6_Ex1 3 | { 4 | public static void main(String[]args) 5 | { 6 | StringProcessor toLowerCase = new StringProcessor() { 7 | @Override 8 | public String process(String s) 9 | { 10 | return s.toLowerCase(); 11 | } 12 | }; 13 | 14 | StringProcessor toUpperCase = new StringProcessor() { 15 | @Override 16 | public String process(String s) 17 | { 18 | return s.toUpperCase(); 19 | } 20 | }; 21 | 22 | String s = toLowerCase.process("FUNCTIONALINTERFACES"); 23 | System.out.println(s); 24 | System.out.println("Lower case: " + StringProcessor.isLowerCase(s)); 25 | System.out.println("Upper case: " + StringProcessor.isUpperCase(s)); 26 | 27 | String t = toUpperCase.process(s); 28 | System.out.println("\n" + t); 29 | System.out.println("Lower case: " + StringProcessor.isLowerCase(t)); 30 | System.out.println("Upper case: " + StringProcessor.isUpperCase(t)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /chapter1/version2/Sect7a_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter1.version2; 2 | public class Sect7a_Ex1 3 | { 4 | public static void main(String[] args) 5 | { 6 | ReceiptPrinter simpleReceiptPrinter 7 | = new ReceiptPrinter<> () { 8 | @Override 9 | public void print(Receipt receipt) 10 | { 11 | System.out.println("\nItem :\t" + receipt.item); 12 | System.out.println("Price:\t" + receipt.price); 13 | System.out.println("Disc:\t" + receipt.discount); 14 | System.out.println("Tax:\t" + receipt.tax); 15 | System.out.println("Total:\t" + computeTotal(receipt)); 16 | } 17 | }; 18 | 19 | ReceiptPrinter countyReceiptPrinter 20 | = new ReceiptPrinter<> () { 21 | @Override 22 | public void print(CountyReceipt receipt) 23 | { 24 | System.out.println("\nItem :\t" + receipt.item); 25 | System.out.println("Price:\t" + receipt.price); 26 | System.out.println("Disc:\t" + receipt.discount); 27 | System.out.println("Tax:\t" + receipt.tax); 28 | System.out.println("CnTax:\t" + receipt.countyTax); 29 | System.out.println("Total:\t" + computeTotal(receipt)); 30 | } 31 | 32 | public double computeTotal(CountyReceipt receipt) 33 | { 34 | double discountedPrice = receipt.price 35 | - (receipt.price * receipt.discount); 36 | return discountedPrice 37 | + (discountedPrice * receipt.tax) 38 | + (discountedPrice * receipt.countyTax); 39 | } 40 | }; 41 | 42 | Receipt receipt = new Receipt("shirt", 20.00, 0.05, 0.07); 43 | simpleReceiptPrinter.print(receipt); 44 | 45 | CountyReceipt countyReceipt = new CountyReceipt(receipt, 0.04); 46 | countyReceiptPrinter.print(countyReceipt); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /chapter1/version2/StringProcessor.java: -------------------------------------------------------------------------------- 1 | package chapter1.version2; 2 | @FunctionalInterface 3 | public interface StringProcessor 4 | { 5 | String process(String s); 6 | 7 | static boolean isLowerCase(String s) 8 | { 9 | boolean result = true; 10 | for (int i = 0; i < s.length() && result; ++i) 11 | result &= Character.isLowerCase(s.charAt(i)); 12 | return result; 13 | } 14 | 15 | static boolean isUpperCase(String s) 16 | { 17 | boolean result = true; 18 | for (int i = 0; i < s.length() && result; ++i) 19 | result &= Character.isUpperCase(s.charAt(i)); 20 | return result; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chapter10/RealEstateBroker.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.*; 3 | import java.util.function.*; 4 | class Home 5 | { 6 | String community; 7 | double price; 8 | int numBedrooms; 9 | double acres; 10 | 11 | 12 | 13 | double schoolDistance; 14 | double trainDistance; 15 | public Home(String co, double pr, int bed, double ac, 16 | double sd, double td) 17 | { 18 | community = co; 19 | price = pr; 20 | numBedrooms = bed; 21 | acres = ac; 22 | schoolDistance = sd; 23 | trainDistance = td; 24 | } 25 | @Override 26 | public String toString() 27 | { 28 | return numBedrooms + " bedroom house in " + community 29 | + " for $" + price + " on " + acres 30 | + " acres\n " + schoolDistance + " miles from school " 31 | + trainDistance + " miles from train"; 32 | } 33 | } 34 | 35 | public class RealEstateBroker 36 | { 37 | public static void main(String[] args) 38 | { 39 | System.out.println("Home Listings for Forest Acres," 40 | + " Happy Gardens, and Comfy Condos\n"); 41 | 42 | List homes = Arrays.asList( 43 | new Home( "Forest Acres" , 425000.0, 4, 1.7, 5.1, 4.5), 44 | new Home( "Happy Gardens", 510000.0, 4, 2.3, 5.1, 4.5), 45 | new Home( "Comfy Condos" , 190000.0, 2, 0.9, 2.1, 4.5), 46 | new Home( "Comfy Condos" , 190000.0, 2, 0.9, 0.7, 4.5), 47 | new Home( "Happy Gardens", 470000.0, 4, 2.1, 5.1, 4.5), 48 | new Home( "Forest Acres" , 345000.0, 3, 1.5, 3.2, 5.9), 49 | new Home( "Happy Gardens", 375000.0, 3, 1.5, 2.3, 2.4), 50 | new Home( "Comfy Condos" , 190000.0, 2, 0.3, 0.5, 2.4) 51 | ); 52 | 53 | 54 | 55 | 56 | Comparator byPrice = 57 | Comparator.comparing(x -> x.price); 58 | 59 | Comparator byBedrooms = 60 | Comparator.comparing(x -> x.numBedrooms); 61 | 62 | Comparator byBedroomsRev = 63 | byBedrooms.reversed(); 64 | 65 | Comparator byProperty = 66 | Comparator.comparing( x -> x.acres, 67 | (x,y) -> (int)(Math.ceil(y) - Math.ceil(x))); 68 | 69 | Comparator bySchool = 70 | Comparator.comparing( x -> x.schoolDistance, 71 | (x,y) -> (int)(Math.ceil(x) - Math.ceil(y))); 72 | Comparator byTrain = 73 | Comparator.comparing( x -> x.trainDistance, 74 | (x,y) -> (int)(Math.ceil(x) - Math.ceil(y))); 75 | 76 | Supplier priorityNumber = () -> { 77 | Scanner input = new Scanner(System.in); 78 | int prio = -1; 79 | while (prio < 0 || prio > 4) 80 | { 81 | System.out.println("Select a Priority:"); 82 | System.out.println("0 - price"); 83 | System.out.println("1 - number of bedrooms"); 84 | System.out.println("2 - property size"); 85 | System.out.println("3 - distance to nearest school"); 86 | System.out.print("4 - distance to train station:"); 87 | prio = Integer.parseInt(input.nextLine()); 88 | if (prio < 0 || prio > 4) 89 | System.out.println("Invalid priority"); 90 | } 91 | return prio; 92 | }; 93 | 94 | Supplier comm = () -> { 95 | Scanner input = new Scanner(System.in); 96 | 97 | 98 | 99 | String[] communities = {"Forest Acres", "Happy Gardens", 100 | "Comfy Condos", "any"}; 101 | int selection = -1; 102 | while (selection < 0 || selection > 3) 103 | { 104 | System.out.println("Select community:"); 105 | System.out.println("0 - Forest Acres"); 106 | System.out.println("1 - Happy Gardens"); 107 | System.out.println("2 - Comfy Condos"); 108 | System.out.print("3 - any:"); 109 | selection = Integer.parseInt(input.nextLine()); 110 | if ((selection < 0 || selection > 3)) 111 | System.out.println("Invalid community"); 112 | } 113 | return communities[selection]; 114 | }; 115 | 116 | Supplier > priority = () -> { 117 | Comparator pr; 118 | int prNumber = priorityNumber.get(); 119 | switch (prNumber) 120 | { 121 | case 0 : pr = byPrice; break; 122 | case 1 : pr = byBedroomsRev; break; 123 | case 2 : pr = byProperty; break; 124 | case 3 : pr = bySchool; break; 125 | default: pr = byTrain; 126 | } 127 | return pr; 128 | }; 129 | 130 | Comparator priority1, priority2, priority3; 131 | String community = comm.get(); 132 | 133 | List homesTemp = new ArrayList<>(homes); 134 | if (!community.equals("any")) 135 | homesTemp.removeIf(x -> !x.community.equals(community)); 136 | 137 | priority1 = priority.get(); 138 | priority2 = priority.get(); 139 | 140 | 141 | 142 | priority3 = priority.get(); 143 | 144 | homesTemp.sort(priority1 145 | .thenComparing(priority2) 146 | .thenComparing(priority3)); 147 | 148 | homesTemp.forEach(x -> System.out.println(x)); 149 | }; 150 | } -------------------------------------------------------------------------------- /chapter10/Sect10_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | import java.util.function.BinaryOperator; 4 | public class Sect10_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Comparator abscompare = 9 | Comparator.comparing(x -> Math.abs(x)); 10 | 11 | BinaryOperator bigint = 12 | BinaryOperator.maxBy(abscompare); 13 | BinaryOperator smallint = 14 | BinaryOperator.minBy(abscompare); 15 | 16 | System.out.println(bigint.apply(2,-5)); 17 | System.out.println(smallint.apply(2,-5)); 18 | } 19 | } -------------------------------------------------------------------------------- /chapter10/Sect1_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect1_Ex1 4 | { 5 | public static String removeVowels(String s) 6 | { 7 | return s.replaceAll("[aeiou]",""); 8 | } 9 | public static void main(String[] args) 10 | { 11 | Comparator byConsonants = (x,y) -> 12 | removeVowels(x).compareTo(removeVowels(y)); 13 | System.out.println(byConsonants.compare("Larry", "Libby")); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /chapter10/Sect1_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect1_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator byIntCompareTo = (x,y) -> x.compareTo(y); 8 | 9 | System.out.println(byIntCompareTo.compare(1000,1002)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /chapter10/Sect1_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect1_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator byIntDifference = (x,y) -> x - y; 8 | 9 | System.out.println(byIntDifference.compare(1000,1002)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /chapter10/Sect2_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect2_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator natural = Comparator.naturalOrder(); 8 | 9 | System.out.println(natural.compare("Larry", "Libby")); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /chapter10/Sect2_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect2_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator reversed = Comparator.reverseOrder(); 8 | 9 | System.out.println(reversed.compare("Larry", "Libby")); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /chapter10/Sect2_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.Comparator; 4 | public class Sect2_Ex3 5 | { 6 | public static void main(String[] args) 7 | { 8 | Comparator byConsonants = (x,y) -> 9 | removeVowels(x).compareTo(removeVowels(y)); 10 | 11 | System.out.println(Comparator.nullsFirst(byConsonants) 12 | .compare("Larry", null)); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter10/Sect2_Ex4.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.Comparator; 4 | public class Sect2_Ex4 5 | { 6 | public static void main(String[] args) 7 | { 8 | Comparator byConsonants = (x,y) -> 9 | removeVowels(x).compareTo(removeVowels(y)); 10 | 11 | System.out.println(Comparator.nullsLast(byConsonants) 12 | .compare("Larry", null)); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter10/Sect2_Ex5.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.Comparator; 4 | public class Sect2_Ex5 5 | { 6 | public static void main(String[] args) 7 | { 8 | Comparator byConsonants = (x,y) -> 9 | removeVowels(x).compareTo(removeVowels(y)); 10 | 11 | System.out.println(byConsonants.reversed() 12 | .compare("Larry", "Libby")); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /chapter10/Sect3_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.function.Function; 3 | import java.util.Comparator; 4 | public class Sect3_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Function gpaKey = x -> x.gpa; 9 | 10 | Comparator byGpa = Comparator.comparing(gpaKey); 11 | 12 | Student s1 = new Student("Larry", 1000, 3.82); 13 | Student s2 = new Student("Libby", 1001, 3.76); 14 | System.out.println(byGpa.compare(s1,s2)); 15 | } 16 | } -------------------------------------------------------------------------------- /chapter10/Sect3_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect3_Ex2 4 | { 5 | public static void main(String []args) 6 | { 7 | Student s1 = new Student("Larry", 1000, 3.82); 8 | Student s2 = new Student("Libby", 1001, 3.76); 9 | 10 | Comparator byId = Comparator.comparing(x -> x.id); 11 | System.out.println(byId.compare(s1,s2)); 12 | } 13 | } -------------------------------------------------------------------------------- /chapter10/Sect3_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect3_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | Student s1 = new Student("Larry", 1000, 3.82); 8 | Student s2 = new Student("Libby", 1001, 3.76); 9 | 10 | Comparator byName 11 | = Comparator.comparing(x -> x.name); 12 | System.out.println(byName.compare(s1,s2)); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter10/Sect3_Ex4.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.Comparator; 4 | public class Sect3_Ex4 5 | { 6 | public static void main(String[] args) 7 | { 8 | Student s1 = new Student("Larry", 1000, 3.82); 9 | Student s2 = new Student("Libby", 1001, 3.76); 10 | 11 | Comparator byNameConsonants = 12 | Comparator.comparing( x -> x.name, 13 | (x,y) -> removeVowels(x).compareTo( 14 | removeVowels(y))); 15 | 16 | System.out.println(byNameConsonants.compare(s1,s2)); 17 | } 18 | } -------------------------------------------------------------------------------- /chapter10/Sect3_Ex5.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect3_Ex5 4 | { 5 | public static void main(String[] args) 6 | { 7 | Student s1 = new Student("Larry", 1000, 3.82); 8 | Student s2 = new Student("Libby", 1001, 3.76); 9 | 10 | Comparator byGpaCeil = 11 | Comparator.comparing( x -> x.gpa, 12 | (x,y) -> (int)(Math.ceil(x) 13 | - Math.ceil(y))); 14 | 15 | System.out.println(byGpaCeil.compare(s1,s2)); 16 | } 17 | } -------------------------------------------------------------------------------- /chapter10/Sect4_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | import java.util.function.ToDoubleFunction; 4 | public class Sect4_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Student s1 = new Student("Larry", 1000, 3.82); 9 | Student s2 = new Student("Libby", 1001, 3.76); 10 | 11 | ToDoubleFunction gpaKey2 = x -> x.gpa; 12 | System.out.println(Comparator.comparingDouble(gpaKey2) 13 | .compare(s1, s2)); 14 | } 15 | } -------------------------------------------------------------------------------- /chapter10/Sect4_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.function.ToIntFunction; 3 | import java.util.Comparator; 4 | public class Sect4_Ex2 { 5 | public static void main(String[] args) 6 | { 7 | Student s1 = new Student("Larry", 1000, 3.82); 8 | Student s2 = new Student("Libby", 1001, 3.76); 9 | 10 | ToIntFunction idKey2 = x -> x.id; 11 | System.out.println(Comparator.comparingInt(idKey2) 12 | .compare(s1, s2)); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter10/Sect5_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect5_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator byName = Comparator.comparing(x -> x.name); 8 | 9 | Student s1 = new Student("Joseph", 1000, 3.82); 10 | Student s2 = new Student("Joseph", 1002, 3.82); 11 | 12 | System.out.println(byName.compare(s1, s1)); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter10/Sect5_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect5_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator byName = Comparator.comparing(x -> x.name); 8 | Comparator byGpa = Comparator.comparing(x -> x.gpa); 9 | 10 | Student s1 = new Student("Joseph", 1000, 3.82); 11 | Student s2 = new Student("Joseph", 1002, 3.82); 12 | 13 | System.out.println(byName.thenComparing(byGpa) 14 | .compare(s1, s2)); 15 | } 16 | } -------------------------------------------------------------------------------- /chapter10/Sect5_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect5_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator byName = Comparator.comparing(x -> x.name); 8 | Comparator byId = Comparator.comparing(x -> x.id); 9 | Comparator byGpa = Comparator.comparing(x -> x.gpa); 10 | 11 | Student s1 = new Student("Joseph", 1000, 3.82); 12 | Student s2 = new Student("Joseph", 1002, 3.82); 13 | 14 | System.out.println(byName.thenComparing(byId) 15 | .thenComparing(byGpa) 16 | .compare(s1, s2)); 17 | } 18 | } -------------------------------------------------------------------------------- /chapter10/Sect5_Ex4.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect5_Ex4 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator byName = Comparator.comparing(x -> x.name); 8 | Comparator byId = Comparator.comparing(x -> x.id); 9 | Comparator byGpa = Comparator.comparing(x -> x.gpa); 10 | 11 | Student s1 = new Student("Joseph", 1000, 3.82); 12 | Student s2 = new Student("Joseph", 1002, 3.82); 13 | 14 | System.out.println(byName.thenComparing(byGpa) 15 | .thenComparing(byId) 16 | .compare(s1, s2)); 17 | } 18 | } -------------------------------------------------------------------------------- /chapter10/Sect5_Ex5.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect5_Ex5 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator byName = Comparator.comparing(x -> x.name); 8 | 9 | Student s1 = new Student("Joseph", 1000, 3.82); 10 | Student s2 = new Student("Joseph", 1002, 3.82); 11 | 12 | System.out.println(byName.thenComparing(x -> x.id) 13 | .thenComparing(x -> x.gpa) 14 | .compare(s1, s2)); 15 | } 16 | } -------------------------------------------------------------------------------- /chapter10/Sect5_Ex6.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.Comparator; 4 | public class Sect5_Ex6 5 | { 6 | public static void main(String[] args) 7 | { 8 | Comparator byNameConsonants 9 | = Comparator.comparing( x -> x.name, 10 | (x,y) -> 11 | removeVowels(x).compareTo(removeVowels(y))); 12 | 13 | Comparator byDifference = (x,y) -> x - y; 14 | 15 | Comparator byCeil = 16 | (x,y) -> (int)(Math.ceil(x) - Math.ceil(y)); 17 | 18 | Student s3 = new Student("Jean", 1003, 3.86); 19 | Student s4 = new Student("Jen" , 1005, 3.69); 20 | 21 | System.out.println(byNameConsonants.thenComparing(x -> x.id, 22 | byDifference) 23 | .thenComparing(x -> x.gpa, 24 | byCeil) 25 | .compare(s3, s4)); 26 | } 27 | } -------------------------------------------------------------------------------- /chapter10/Sect6_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect6_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator byGpa = Comparator.comparing(x -> x.gpa); 8 | Student s5 = new Student("Kaitlyn", 1006, 3.69); 9 | Student s6 = new Student("Jane" , 1007, 3.69); 10 | 11 | System.out.println(byGpa.thenComparingInt( x-> x.id ) 12 | .compare(s5,s6)); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter10/Sect6_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.Comparator; 3 | public class Sect6_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Comparator byName = Comparator.comparing(x -> x.name); 8 | 9 | Student s7 = new Student("Robert", 1008, 3.86); 10 | Student s8 = new Student("Robert", 1009, 3.69); 11 | 12 | System.out.println(byName.thenComparingDouble( x-> x.gpa ) 13 | .compare(s7,s8)); 14 | } 15 | } -------------------------------------------------------------------------------- /chapter10/Sect7_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.*; 3 | public class Sect7_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | List students = Arrays.asList( 8 | new Student("Joseph" , 1623, 3.54), 9 | new Student("Annie" , 1923, 2.94), 10 | new Student("Sharmila", 1874, 1.86), 11 | new Student("Harvey" , 1348, 1.78), 12 | new Student("Grace" , 1004, 3.90), 13 | new Student("Annie" , 1245, 2.87) 14 | ); 15 | 16 | Comparator byGpaCeil = 17 | Comparator.comparing( x -> x.gpa, 18 | (x,y) -> (int)(Math.ceil(x) 19 | - Math.ceil(y))); 20 | 21 | students.sort(byGpaCeil); 22 | students.forEach(x-> System.out.println(x)); 23 | } 24 | } -------------------------------------------------------------------------------- /chapter10/Sect7_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.*; 3 | public class Sect7_Ex2 { 4 | public static void main(String[] args) 5 | { 6 | List students = Arrays.asList( 7 | new Student("Joseph" , 1623, 3.54), 8 | new Student("Annie" , 1923, 2.94), 9 | new Student("Sharmila", 1874, 1.86), 10 | new Student("Harvey" , 1348, 1.78), 11 | new Student("Grace" , 1004, 3.90), 12 | new Student("Annie" , 1245, 2.87) 13 | ); 14 | 15 | Comparator byGpaCeil = 16 | Comparator.comparing( x -> x.gpa, 17 | (x,y) -> (int)(Math.ceil(x) 18 | - Math.ceil(y))); 19 | 20 | students.sort(byGpaCeil 21 | .thenComparing(x -> x.name)); 22 | students.forEach(x-> System.out.println(x)); 23 | } 24 | } -------------------------------------------------------------------------------- /chapter10/Sect7_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.*; 3 | public class Sect7_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | List students = Arrays.asList( 8 | new Student("Joseph" , 1623, 3.54), 9 | new Student("Annie" , 1923, 2.94), 10 | new Student("Sharmila", 1874, 1.86), 11 | new Student("Harvey" , 1348, 1.78), 12 | new Student("Grace" , 1004, 3.90), 13 | new Student("Annie" , 1245, 2.87) 14 | ); 15 | 16 | Comparator byGpaCeil = 17 | Comparator.comparing( x -> x.gpa, 18 | (x,y) -> (int)(Math.ceil(x) 19 | - Math.ceil(y))); 20 | 21 | students.sort(byGpaCeil 22 | .thenComparing(x -> x.id) 23 | .thenComparing(x -> x.name)); 24 | students.forEach(x-> System.out.println(x)); 25 | } 26 | } -------------------------------------------------------------------------------- /chapter10/Sect8_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.*; 3 | public class Sect8_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Student[] students = { 8 | new Student("Joseph" , 1623, 3.54), 9 | new Student("Annie" , 1923, 2.94), 10 | new Student("Sharmila", 1874, 1.86), 11 | new Student("Harvey" , 1348, 1.78), 12 | new Student("Grace" , 1004, 3.90), 13 | new Student("Annie" , 1245, 2.87) 14 | }; 15 | Student[] studentsCopy = Arrays.copyOf(students, students.length); 16 | Comparator byGpaCeil = 17 | Comparator.comparing( x -> x.gpa, 18 | (x,y) -> (int)(Math.ceil(x) 19 | - Math.ceil(y))); 20 | 21 | Arrays.sort(students, 22 | byGpaCeil 23 | .thenComparing(x -> x.id) 24 | .thenComparing(x -> x.name)); 25 | 26 | for (Student s : students) 27 | System.out.println(s); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /chapter10/Sect8_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.*; 3 | public class Sect8_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Student[] students = { 8 | new Student("Joseph" , 1623, 3.54), 9 | new Student("Annie" , 1923, 2.94), 10 | new Student("Sharmila", 1874, 1.86), 11 | new Student("Harvey" , 1348, 1.78), 12 | new Student("Grace" , 1004, 3.90), 13 | new Student("Annie" , 1245, 2.87) 14 | }; 15 | Student[] studentsCopy = Arrays.copyOf(students, students.length); 16 | 17 | Arrays.sort(studentsCopy, 2, 5, 18 | Comparator.comparing(x -> x.name)); 19 | 20 | for (Student s : studentsCopy) 21 | System.out.println(s); 22 | } 23 | } -------------------------------------------------------------------------------- /chapter10/Sect8_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import java.util.*; 3 | import java.text.NumberFormat; 4 | public class Sect8_Ex3 5 | { 6 | public static void main(String[] args) 7 | { 8 | final int NUM_STUDENTS = 1000; 9 | NumberFormat fmt = NumberFormat.getNumberInstance(); 10 | fmt.setMinimumIntegerDigits(3); 11 | Student[] studentBody = new Student[NUM_STUDENTS]; 12 | for (int i = 0; i < NUM_STUDENTS; ++i) 13 | studentBody[i] = new Student("S" + fmt.format(i), i, 0.0); 14 | 15 | int index = Arrays.binarySearch(studentBody, 16 | new Student("S647",0,0.0), 17 | Comparator.comparing(x -> x.name)); 18 | 19 | System.out.println("index = " + index + " " 20 | + studentBody[index]); 21 | } 22 | } -------------------------------------------------------------------------------- /chapter10/Sect9_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.*; 4 | public class Sect9_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Comparator byConsonants = (x,y) -> 9 | removeVowels(x).compareTo(removeVowels(y)); 10 | 11 | TreeMap pets = new TreeMap<>(byConsonants); 12 | 13 | pets.put("gerbil", "small cute rodents"); 14 | pets.put("guinea pig", "rodents, not pigs"); 15 | pets.put("cat", "have nine lives"); 16 | pets.put("chicken", "more populous than people"); 17 | 18 | pets.forEach((x,y) -> System.out.println(x + ", " + y)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /chapter10/Sect9_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.*; 4 | public class Sect9_Ex2 5 | { 6 | public static void main(String[] args) 7 | { 8 | Comparator byConsonants = (x,y) -> 9 | removeVowels(x).compareTo(removeVowels(y)); 10 | 11 | TreeMap pets = new TreeMap<>(byConsonants); 12 | 13 | pets.put("gerbil", "small cute rodents"); 14 | pets.put("guinea pig", "rodents, not pigs"); 15 | pets.put("cat", "have nine lives"); 16 | pets.put("chicken", "more populous than people"); 17 | 18 | Comparator> cmap = 19 | Map.Entry.comparingByKey(); 20 | 21 | Map.Entry cat = pets.ceilingEntry("cat"); 22 | Map.Entry chicken = pets.ceilingEntry("chicken"); 23 | 24 | System.out.println(cmap.compare(cat, chicken)); 25 | } 26 | } -------------------------------------------------------------------------------- /chapter10/Sect9_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.*; 4 | public class Sect9_Ex3 5 | { 6 | public static void main(String[] args) 7 | { 8 | Comparator byConsonants = (x,y) -> 9 | removeVowels(x).compareTo(removeVowels(y)); 10 | 11 | TreeMap pets = new TreeMap<>(byConsonants); 12 | 13 | pets.put("gerbil", "small cute rodents"); 14 | pets.put("guinea pig", "rodents, not pigs"); 15 | pets.put("cat", "have nine lives"); 16 | pets.put("chicken", "more populous than people"); 17 | 18 | Map.Entry cat = pets.ceilingEntry("cat"); 19 | Map.Entry chicken = pets.ceilingEntry("chicken"); 20 | 21 | Comparator> cmapCons = 22 | Map.Entry.comparingByKey(byConsonants); 23 | 24 | System.out.println(cmapCons.compare(cat, chicken)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /chapter10/Sect9_Ex4.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.*; 4 | public class Sect9_Ex4 5 | { 6 | public static void main(String[] args) 7 | { 8 | Comparator byConsonants = (x,y) -> 9 | removeVowels(x).compareTo(removeVowels(y)); 10 | 11 | TreeMap pets = new TreeMap<>(byConsonants); 12 | 13 | pets.put("gerbil", "small cute rodents"); 14 | pets.put("guinea pig", "rodents, not pigs"); 15 | pets.put("cat", "have nine lives"); 16 | pets.put("chicken", "more populous than people"); 17 | 18 | Map.Entry cat = pets.ceilingEntry("cat"); 19 | Map.Entry chicken = pets.ceilingEntry("chicken"); 20 | 21 | Comparator> cval = 22 | Map.Entry.comparingByValue(); 23 | 24 | System.out.println(cval.compare(cat, chicken)); 25 | } 26 | } -------------------------------------------------------------------------------- /chapter10/Student.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | public class Student 3 | { 4 | String name; 5 | Integer id; 6 | Double gpa; 7 | public Student(String n, int i, double g) 8 | { 9 | name = n; 10 | id = i; 11 | gpa = g; 12 | } 13 | @Override 14 | public String toString() { return name + " " + id + " " + gpa; } 15 | } -------------------------------------------------------------------------------- /chapter11/GuessANumber.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.*; 3 | import java.util.function.*; 4 | class Guess 5 | { 6 | int number; 7 | int guess; 8 | public Guess(int n) { number = n; } 9 | public Guess(int g, int n) { guess = g; number = n; } 10 | } 11 | public class GuessANumber 12 | { 13 | public static void main(String[] args) 14 | { 15 | UnaryOperator guessIt = x -> { 16 | System.out.print("Guess a number between 0 and 9:"); 17 | return new Guess(new Scanner(System.in).nextInt(), 18 | x.number); 19 | }; 20 | 21 | Predicate guessIsCorrect = x -> x.guess != x.number; 22 | 23 | Optional.of(new Guess(new Random().nextInt(10))) 24 | .map(guessIt) 25 | .filter(guessIsCorrect) 26 | .map(guessIt) 27 | .filter(guessIsCorrect) 28 | .map(guessIt) 29 | .filter(guessIsCorrect) 30 | .map(guessIt) 31 | .filter(guessIsCorrect) 32 | .map(guessIt) 33 | .filter(guessIsCorrect) 34 | .ifPresent(x -> System.out.println( 35 | "Sorry, couldn't guess " + x.number)); 36 | } 37 | } -------------------------------------------------------------------------------- /chapter11/Sect1_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect1_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | try { 8 | Optional o1 = Optional.of(null); 9 | } catch (NullPointerException e) { 10 | System.out.println("NullPointerException"); 11 | } 12 | Optional o2 = Optional.of("Hello"); 13 | System.out.println("OK"); 14 | } 15 | } -------------------------------------------------------------------------------- /chapter11/Sect1_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect1_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional o3 = Optional.ofNullable(null); 8 | System.out.println("OK"); 9 | 10 | Optional o4 = Optional.ofNullable("Hello"); 11 | System.out.println("OK"); 12 | } 13 | } -------------------------------------------------------------------------------- /chapter11/Sect2_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect2_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional o5 = Optional.empty(); 8 | 9 | if (o5.isPresent()) 10 | System.out.println("o4 is non-null"); 11 | } 12 | } -------------------------------------------------------------------------------- /chapter11/Sect2_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect2_Ex2 { 4 | public static void main(String[] args) 5 | { 6 | Optional imNull = Optional.ofNullable(null); 7 | if (imNull.isEmpty()) 8 | System.out.println("Empty"); 9 | } 10 | } -------------------------------------------------------------------------------- /chapter11/Sect3_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.*; 3 | public class Sect3_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional o5 = Optional.empty(); 8 | 9 | try { 10 | o5.get(); 11 | } catch (NoSuchElementException e) { 12 | System.out.println("NoSuchElementException"); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /chapter11/Sect3_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect3_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional o4 = Optional.ofNullable("Hello"); 8 | Optional o5 = Optional.empty(); 9 | 10 | if (o4.isPresent()) 11 | System.out.println(o4.get()); 12 | if (o5.isPresent()) 13 | System.out.println(o5.get()); 14 | else 15 | System.out.println("o5 is null"); 16 | } 17 | } -------------------------------------------------------------------------------- /chapter11/Sect4_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect4_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | 8 | String t=null, u="Hello"; 9 | 10 | String s = Optional.ofNullable(t) // Optional has null 11 | .orElse(u); // Optional has "Hello" 12 | 13 | String s2 = Optional.ofNullable(t) // null 14 | .orElseGet( () -> u); // "Hello" 15 | } 16 | } -------------------------------------------------------------------------------- /chapter11/Sect4_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect4_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | try { 8 | String s = null; 9 | String opt = Optional.ofNullable(s) 10 | .orElseThrow(() -> 11 | new Exception("Null Optional")); 12 | } 13 | catch (Exception e) 14 | { 15 | System.out.println(e.getMessage()); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /chapter11/Sect4_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect4_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | try { 8 | String s = null; 9 | String opt = Optional.ofNullable(s) 10 | .orElseThrow(); 11 | } 12 | catch (Exception e) 13 | { 14 | System.out.println(e.getMessage()); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /chapter11/Sect5_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.function.Supplier; 3 | import java.util.*; 4 | public class Sect5_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Supplier > supplier = () -> { 9 | System.out.print("Enter a string:"); 10 | return Optional.of((new Scanner(System.in)).nextLine()); 11 | }; 12 | 13 | String s = null; 14 | Optional.ofNullable(s) 15 | .or(supplier) 16 | .ifPresent(x -> System.out.println(x)); 17 | } 18 | } -------------------------------------------------------------------------------- /chapter11/Sect6_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect6_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional.of("Hello") // Optional(Hello) 8 | .filter(x -> x.charAt(0) == 'H') // Optional(Hello) 9 | .filter(x -> x.length() > 2) // Optional(Hello) 10 | .filter(x -> x.charAt(1) == 'e') // Optional(Hello) 11 | .ifPresent(x -> System.out.println(x)); // Prints "Hello" 12 | } 13 | } -------------------------------------------------------------------------------- /chapter11/Sect6_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect6_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional.of("Hello") // Optional(Hello) 8 | .filter(x -> x.charAt(0) == 'i') // Optional(null) 9 | .filter(x -> x.length() > 2) // doesn’t execute 10 | .filter(x -> x.charAt(1) == 'e') // doesn’t execute 11 | .ifPresent(x -> System.out.println(x)); // doesn’t execute 12 | } 13 | } -------------------------------------------------------------------------------- /chapter11/Sect7_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect7_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional.of("4") // Optional("4") 8 | .map(x -> Integer.parseInt(x)) // Optional(4) 9 | .filter(x -> x > 2) // Optional(4) 10 | .filter(x -> (x%2) == 0) // Optional(4) 11 | .ifPresent(x -> System.out.println(x)); // Prints 4 12 | } 13 | } -------------------------------------------------------------------------------- /chapter11/Sect7_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect7_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional o1 = Optional.of(2); 8 | o1.ifPresent(x -> ++x); 9 | o1.ifPresent(x -> System.out.println(x)); // Prints 2 10 | } 11 | } -------------------------------------------------------------------------------- /chapter11/Sect7_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect7_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional.of(2) // Optional(2) 8 | .map(x -> ++x) // Optional(3) 9 | .ifPresent(x -> System.out.println(x)); // Prints 3 10 | } 11 | } -------------------------------------------------------------------------------- /chapter11/Sect7_Ex4.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | public class Sect7_Ex4 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional.of("4") // Optional("4") 8 | .flatMap(x -> 9 | Optional.of(Integer.parseInt(x))) // Optional(4) 10 | .ifPresent(x -> System.out.println(x)); // Prints 4 11 | } 12 | } -------------------------------------------------------------------------------- /chapter11/Sect7_Ex5.java: -------------------------------------------------------------------------------- 1 | package chapter11; 2 | import java.util.Optional; 3 | class Resource 4 | { 5 | static int count = 3; 6 | public Resource() 7 | { 8 | count--; 9 | 10 | System.out.println("Resource consumed, " + count 11 | + " remaining."); 12 | } 13 | } 14 | 15 | public class Sect7_Ex5 16 | { 17 | public static void main(String[] args) 18 | { 19 | Optional.of(new Resource()) // Resource.count = 2 20 | .filter(x -> x.count > 0) 21 | .map(x -> new Resource()) // Resource.count = 1 22 | .filter(x -> x.count > 0) 23 | .map(x -> new Resource()) // Resource.count = 0 24 | .filter(x -> x.count > 0) // Optional(null) 25 | .map(x -> new Resource()); // doesn't execute 26 | } 27 | } -------------------------------------------------------------------------------- /chapter12/Inventory.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | import java.util.*; 4 | import java.util.function.*; 5 | class Item 6 | { 7 | String model; 8 | int quantity; 9 | double price; 10 | public Item(String m, int q, double p) 11 | { 12 | model = m; 13 | quantity = q; 14 | price = p; 15 | } 16 | 17 | @Override 18 | public String toString() 19 | { 20 | return model + ":" + quantity + " $" + price; 21 | } 22 | } 23 | 24 | class Part 25 | { 26 | String name; 27 | String manu; 28 | int hoursToInstall; 29 | List models; 30 | public Part(String n, String m, int h, Item... it) 31 | { 32 | name = n; 33 | manu = m; 34 | hoursToInstall = h; 35 | models = new ArrayList<>(); 36 | for (Item i : it) 37 | models.add(i); 38 | } 39 | @Override 40 | public String toString() 41 | { 42 | return manu + " " + hoursToInstall + "hrs " + models; 43 | } 44 | } 45 | public class Inventory 46 | { 47 | static String partName = null; 48 | static int quantity=0; 49 | static double price = 0.0; 50 | static int timeLimit = 0; 51 | 52 | public static void main(String[] args) 53 | { 54 | List parts = Arrays.asList( 55 | new Part("tires","Goodyear",2, 56 | new Item("RX2041",4,75.25), 57 | new Item("SX3355",2,90.50)), 58 | new Part("brakes","EBC",1, 59 | new Item("EX8426",2,125.35)), 60 | new Part("tires","Firestone",1, 61 | new Item("FS2112",3,60.47), 62 | new Item("FS2479",5,85.20)), 63 | new Part("brakes","Acme",3, 64 | new Item("AC25",1,90.74), 65 | new Item("AC26",4,130.22)) 66 | ); 67 | Supplier selectOperation = () -> { 68 | Scanner userInput = new Scanner(System.in); 69 | int operation = -1; 70 | while (operation < 0 || operation > 5) 71 | { 72 | System.out.println("Please select an operation:"); 73 | System.out.println( 74 | " 0 - List inventory"); 75 | System.out.println( 76 | " 1 - Find item by quantity and price"); 77 | System.out.println( 78 | " 2 - List items by price"); 79 | System.out.println( 80 | " 3 - Count items by part name"); 81 | System.out.println( 82 | " 4 - Find items by installation time"); 83 | System.out.print ( 84 | " 5 - End program:"); 85 | operation = Integer.parseInt(userInput.nextLine()); 86 | if (operation < 0 || operation > 5) 87 | System.out.println("Invalid operation"); 88 | } 89 | return operation; 90 | }; 91 | Supplier selectPartName = () -> 92 | (new Scanner(System.in)).nextLine(); 93 | Supplier selectQuantity = () -> 94 | Integer.parseInt((new Scanner(System.in)).nextLine()); 95 | Supplier selectPrice = () -> 96 | Double.parseDouble((new Scanner(System.in)).nextLine()); 97 | Supplier selectTimeLimit = () -> 98 | Integer.parseInt((new Scanner(System.in)).nextLine()); 99 | 100 | System.out.println( 101 | "Welcome to Discount Dave's Inventory Program."); 102 | boolean done = false; 103 | while (!done) 104 | { 105 | switch (selectOperation.get()) 106 | { 107 | case 0: 108 | parts.stream() 109 | .collect(Collectors.groupingBy(x -> x.name)) 110 | .forEach( 111 | (x,y) -> System.out.println(x + ": " + y)); 112 | break; 113 | 114 | case 1: 115 | System.out.print("Select a part name:"); 116 | partName = selectPartName.get(); 117 | System.out.print("Select a minimum quantity:"); 118 | quantity = selectQuantity.get(); 119 | System.out.print("Select a price limit:"); 120 | price = selectPrice.get(); 121 | 122 | parts.stream() 123 | .filter(x -> x.name.equals(partName)) 124 | .peek(x -> System.out.println( 125 | x.name + " by " + x.manu + ":")) 126 | .flatMap(x -> x.models.stream()) 127 | .filter(x -> x.price <= price) 128 | .filter(x -> x.quantity >= quantity) 129 | .forEach(x -> System.out.println(x)); 130 | break; 131 | 132 | case 2: 133 | System.out.print("Select a part name:"); 134 | partName = selectPartName.get(); 135 | 136 | parts.stream() 137 | .filter(x -> x.name.equals(partName)) 138 | .flatMap(x -> x.models.stream()) 139 | .sorted( Comparator.comparing(x -> x.price)) 140 | .forEach(x -> System.out.println(x)); 141 | break; 142 | 143 | case 3: 144 | System.out.print("Select a part name:"); 145 | partName = selectPartName.get(); 146 | 147 | Integer i = 148 | parts.stream() 149 | .filter(x -> x.name.equals(partName)) 150 | .flatMap(x -> x.models.stream()) 151 | .reduce(0, 152 | (x,z) -> x + z.quantity, 153 | (x,y) -> x += y); 154 | System.out.println(i); 155 | break; 156 | 157 | case 4: 158 | System.out.print("Select a part name:"); 159 | partName = selectPartName.get(); 160 | System.out.print( 161 | "Select installation time limit:"); 162 | timeLimit = selectTimeLimit.get(); 163 | 164 | parts.stream() 165 | .filter(x -> x.name.equals(partName)) 166 | .filter(x -> x.hoursToInstall < timeLimit) 167 | .forEach(x -> System.out.println(x)); 168 | break; 169 | 170 | default: // 5 171 | System.out.println("Bye Bye."); 172 | done = true; 173 | } 174 | } 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /chapter12/Sect10_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | public class Sect10_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.Builder bld = Stream.builder(); 8 | 9 | bld.accept("RED"); 10 | bld.accept("GREEN"); 11 | bld.accept("BLUE"); 12 | 13 | Stream st = bld.build(); 14 | st.forEach(x -> System.out.println(x)); 15 | } 16 | } -------------------------------------------------------------------------------- /chapter12/Sect10_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.Stream; 3 | public class Sect10_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.builder() 8 | .add("RED") 9 | .add("GREEN") 10 | .add("BLUE") 11 | .build() 12 | .forEach(x -> System.out.println(x)); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter12/Sect11_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.Stream; 3 | public class Sect11_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.of(1,2,3,4) 8 | .peek(x -> System.out.println(x) ) 9 | .reduce( (x,y) -> x += y) 10 | .ifPresent(x -> System.out.println(x)); 11 | } 12 | } -------------------------------------------------------------------------------- /chapter12/Sect12_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | public class Sect12_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | IntStream ints = 8 | Stream.of(new Car("Buick" ,"Regal" ,25), 9 | new Car("Hyundai","Elantra",27), 10 | new Car("Buick" ,"Skylark",26), 11 | new Car("Hyundai","Accent" ,30)) // Stream 12 | .mapToInt(x -> x.mpg); // IntStream 13 | 14 | ints.max() 15 | .ifPresent(x -> System.out.println(x)); 16 | 17 | LongStream.of(1,2,3,4) 18 | .min() 19 | .ifPresent(x -> System.out.println(x)); 20 | } 21 | } -------------------------------------------------------------------------------- /chapter12/Sect12_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.DoubleStream; 3 | public class Sect12_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | DoubleStream.of(1.1,2.2,3.3,4.4) 8 | .average() 9 | .ifPresent(x -> System.out.println(x)); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter12/Sect12_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.IntStream; 3 | public class Sect12_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | int sum = IntStream.of(1,2,3,4) 8 | .sum(); 9 | System.out.println(sum); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter12/Sect2_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.Stream; 3 | import java.util.Random; 4 | public class Sect2_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Stream tenRandomNumbers = 9 | Stream.generate( () -> (new Random()).nextInt(100)) 10 | .limit(10); 11 | 12 | tenRandomNumbers.forEach(x -> System.out.println(x)); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter12/Sect3_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.Stream; 3 | public class Sect3_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.of("RED", "GREEN", "BLUE", "RED") 8 | .filter(x -> x.equals("YELLOW")) 9 | .forEach(x -> System.out.println(x)); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter12/Sect3_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.Stream; 3 | public class Sect3_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.of("RED", "GREEN", "BLUE", "RED") 8 | .filter(x -> x.equals("RED")) 9 | .forEach(x -> System.out.println(x)); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter12/Sect3_Ex4.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.function.Predicate; 3 | import java.util.stream.Stream; 4 | public class Sect3_Ex4 5 | { 6 | public static void main(String[] args) 7 | { 8 | Predicate isRed = x -> x.equals("RED"); 9 | 10 | Stream.of("RED", "GREEN", "BLUE", "RED") 11 | .filter(isRed.or(x -> x.indexOf("R") > -1)) 12 | .forEach(x -> System.out.println(x)); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter12/Sect4_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.*; 3 | import java.util.stream.Stream; 4 | public class Sect4_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | List list = Arrays.asList("RED","GREEN"); 9 | 10 | list.stream() 11 | .filter(x -> x.equals("GREEN")) 12 | .forEach(x -> System.out.println(x)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /chapter12/Sect4_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.Optional; 3 | public class Sect4_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | Optional.of("RED") 8 | .stream() 9 | .forEach(x -> System.out.println(x)); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter12/Sect5_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.Stream; 3 | public class Sect5_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.of("Kyle", "Jaquiline", "Jimmy") 8 | .sorted() 9 | .forEach(x -> System.out.println(x)); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter12/Sect5_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.stream.Stream; 4 | public class Sect5_Ex2 5 | { 6 | public static void main(String[] args) 7 | { 8 | Stream.of("Kyle", "Jaquiline", "Jimmy") 9 | .sorted( (x,y) -> 10 | removeVowels(x).compareTo(removeVowels(y))) 11 | .forEach(x -> System.out.println(x)); 12 | } 13 | } -------------------------------------------------------------------------------- /chapter12/Sect6_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.stream.Stream; 4 | public class Sect6_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Stream.of("Kyle", "Jaquiline", "Jimmy") 9 | .min( (x,y) -> // Stream 10 | removeVowels(x).compareTo(removeVowels(y))) 11 | .ifPresent(x -> System.out.println(x)); // Optional 12 | } 13 | } -------------------------------------------------------------------------------- /chapter12/Sect6_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import static chapter10.Sect1_Ex1.removeVowels; 3 | import java.util.stream.Stream; 4 | public class Sect6_Ex2 5 | { 6 | public static void main(String[] args) 7 | { 8 | Stream.of("Kyle", "Jaquiline", "Jimmy") 9 | .max( (x,y) -> // Stream 10 | removeVowels(x).compareTo(removeVowels(y))) 11 | .ifPresent(x -> System.out.println(x)); // Optional 12 | } 13 | } -------------------------------------------------------------------------------- /chapter12/Sect7_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.*; 3 | import java.util.stream.Stream; 4 | import chapter10.Student; 5 | class Class 6 | { 7 | String subject; 8 | Collection students; 9 | public Class(String su, Student... st) 10 | { 11 | subject = su; 12 | students = Arrays.asList(st); 13 | } 14 | } 15 | 16 | public class Sect7_Ex1 17 | { 18 | public static void main(String[] args) 19 | { 20 | Stream.of(new Class("Biology", 21 | new Student("Joe" ,1001,3.81), 22 | new Student("Mary",1002,3.91)), 23 | new Class("Physics", 24 | new Student("Kalpana",1003,3.61), 25 | new Student("Javier" ,1004,3.71))) // Stream 26 | .flatMap(x -> x.students.stream()) // Stream 27 | .forEach(x -> System.out.println(x)); 28 | } 29 | } -------------------------------------------------------------------------------- /chapter12/Sect7_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.Stream; 3 | import chapter10.Student; 4 | public class Sect7_Ex2 5 | { 6 | public static void main(String[] args) 7 | { 8 | Stream.of( 9 | new Class("Biology", 10 | new Student("Joe" ,1001,3.81), 11 | new Student("Mary",1002,3.91)), 12 | new Class("Physics", 13 | new Student("Kalpana",1003,3.61), 14 | new Student("Javier" ,1004,3.71))) // Stream 15 | .map(x -> x.students) // Stream> 16 | .forEach(x -> System.out.println(x)); 17 | } 18 | } -------------------------------------------------------------------------------- /chapter12/Sect8_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.Stream; 3 | public class Sect8_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.of(1, 2, 3, 4, 5) // Stream(Integer) 8 | .reduce( (x,y) -> x * y) // Optional(Integer) 9 | .ifPresent(x -> System.out.println(x)); // Prints 120 10 | } 11 | } -------------------------------------------------------------------------------- /chapter12/Sect8_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.function.*; 3 | import java.util.stream.Stream; 4 | class TwoInts 5 | { 6 | Integer i1; 7 | Integer i2; 8 | public TwoInts(int z1, int z2) { i1=z1; i2=z2;} 9 | } 10 | 11 | public class Sect8_Ex2 12 | { 13 | public static void main(String[] args) 14 | { 15 | Stream two = Stream.of(new TwoInts(1,2),new TwoInts(8,9)); 16 | 17 | BiFunction accumulator = (x,y) -> x + y.i2; 18 | 19 | BinaryOperator combiner = (x,y) -> x += y; 20 | Integer j = two.reduce(0,accumulator,combiner); 21 | System.out.println(j); 22 | } 23 | } -------------------------------------------------------------------------------- /chapter12/Sect9_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.function.*; 3 | import java.util.*; 4 | import java.util.stream.*; 5 | public class Sect9_Ex1 6 | { 7 | public static void main(String[] args) 8 | { 9 | Supplier> supp = () -> new ArrayList(); 10 | BiConsumer, Character> acc = (x,y) -> { 11 | System.out.print("acc: x=" + x + " y=" + y+ " result="); 12 | if (Character.isAlphabetic(y)) 13 | x.add(0,y); 14 | else 15 | x.add(y); 16 | x.forEach(z -> System.out.print(z)); 17 | System.out.println(); 18 | }; 19 | 20 | BinaryOperator> comb1 = (x,y) -> { 21 | x.addAll(y); 22 | return x; 23 | }; 24 | 25 | Stream.of('1','a','b','2') // Stream 26 | .collect(Collector.of(supp, acc, comb1)) // List 27 | .forEach(x -> System.out.println(x)); 28 | } 29 | } -------------------------------------------------------------------------------- /chapter12/Sect9_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.function.*; 3 | import java.util.*; 4 | import java.util.stream.*; 5 | public class Sect9_Ex2 6 | { 7 | public static void main(String[] args) 8 | { 9 | Supplier> supp = () -> new ArrayList(); 10 | BiConsumer, Character> acc = (x,y) -> { 11 | System.out.print("acc: x=" + x + " y=" + y+ " result="); 12 | if (Character.isAlphabetic(y)) 13 | x.add(0,y); 14 | else 15 | x.add(y); 16 | x.forEach(z -> System.out.print(z)); 17 | System.out.println(); 18 | }; 19 | 20 | BiConsumer,List> comb2 = (x,y) -> 21 | x.addAll(y); 22 | 23 | Stream.of('1','a','b','2') 24 | .collect(supp, acc, comb2) 25 | .forEach(x -> System.out.println(x)); 26 | } 27 | } -------------------------------------------------------------------------------- /chapter12/Sect9_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.function.*; 3 | import java.util.stream.*; 4 | public class Sect9_Ex3 5 | { 6 | public static void main(String[] args) 7 | { 8 | Supplier supps = () -> new StringBuilder(); 9 | BiConsumer accs = (x,y) -> 10 | x.append(y.replaceAll("[aeiou]","")); 11 | 12 | BinaryOperator combs = (x,y) -> { 13 | x.append(y); 14 | return x; 15 | }; 16 | 17 | Function fins = x -> x.toString(); 18 | 19 | String s = Stream.of("Joe","Kalpana","Christopher") 20 | .collect(Collector.of(supps, accs, combs, fins)); 21 | System.out.println(s); 22 | } 23 | } -------------------------------------------------------------------------------- /chapter12/Sect9a_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | public class Sect9a_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.of('1', 'a' , 'b', '2') 8 | .collect(Collectors.toList()) 9 | .forEach(x -> System.out.println(x)); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter12/Sect9a_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.function.*; 3 | import java.util.*; 4 | import java.util.stream.*; 5 | public class Sect9a_Ex2 6 | { 7 | public static void main(String[] args) 8 | { 9 | Supplier> supp = () -> new ArrayList(); 10 | 11 | BinaryOperator> comb1 = (x,y) -> { 12 | x.addAll(y); 13 | return x; 14 | }; 15 | 16 | BiConsumer,Character> accc2 = (x,y) -> { 17 | if (Character.isAlphabetic(y)) 18 | x.add(0,y); 19 | else 20 | x.add(y); 21 | }; 22 | 23 | Function,String> fins2 = x -> { 24 | String t=""; 25 | for (Character c : x) 26 | t += c; 27 | return t; 28 | }; 29 | 30 | String t = 31 | Stream.of('1','a','b','2') // Stream 32 | .collect(Collectors.collectingAndThen( 33 | Collector.of(supp, accc2, comb1), // List 34 | fins2)); // String 35 | System.out.println(t); // Prints ba12 36 | } 37 | } -------------------------------------------------------------------------------- /chapter12/Sect9a_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | public class Sect9a_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.of(1,2,3,4,5) // Stream 8 | .collect(Collectors.reducing((x,y) -> 9 | x *= y)) // Optional 10 | .ifPresent(x -> System.out.println(x)); // Prints 120 11 | } 12 | } -------------------------------------------------------------------------------- /chapter12/Sect9a_Ex4.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | public class Sect9a_Ex4 4 | { 5 | public static void main(String[] args) 6 | { 7 | String s = Stream.of("RED", "GREEN", "BLUE") // Stream 8 | .collect(Collectors.joining()); // String 9 | 10 | System.out.println(s); 11 | } 12 | } -------------------------------------------------------------------------------- /chapter12/Sect9a_Ex5.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | public class Sect9a_Ex5 4 | { 5 | public static void main(String[] args) 6 | { 7 | String s = Stream.of("RED", "GREEN", "BLUE") // Stream 8 | .collect(Collectors.joining(",")); // String 9 | 10 | System.out.println(s); // Prints RED,GREEN,BLUE 11 | } 12 | } -------------------------------------------------------------------------------- /chapter12/Sect9a_Ex6.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | class Car 4 | { 5 | String manu; 6 | String model; 7 | int mpg; 8 | public Car(String ma, String mo, int mp) 9 | { 10 | manu = ma; 11 | model = mo; 12 | mpg = mp; 13 | } 14 | public String toString() 15 | { 16 | return manu + " " + model + " gets " + mpg + " mpg"; 17 | } 18 | } 19 | 20 | public class Sect9a_Ex6 21 | { 22 | public static void main(String[] args) 23 | { 24 | Stream.of(new Car("Buick" ,"Regal" ,25), 25 | new Car("Hyundai","Elantra",27), 26 | new Car("Buick" ,"Skylark",26), 27 | new Car("Hyundai","Accent" ,30)) // Stream 28 | .collect( 29 | Collectors.groupingBy(x -> x.manu))// Map> 30 | .forEach( (x,y) -> 31 | System.out.println(x + ": " +y )); 32 | } 33 | } -------------------------------------------------------------------------------- /chapter12/Sect9a_Ex7.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | public class Sect9a_Ex7 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.of(new Car("Buick" ,"Regal" ,25), 8 | new Car("Hyundai","Elantra",27), 9 | new Car("Buick" ,"Skylark",26), 10 | new Car("Hyundai","Accent" ,30))// Stream 11 | .collect(Collectors.groupingBy(x ->// Map> 12 | x.manu, 13 | Collectors.mapping(x -> x.mpg, 14 | Collectors.toList()))) // List> 15 | .forEach( (x,y) -> 16 | System.out.println(x + ": " +y )); 17 | } 18 | } -------------------------------------------------------------------------------- /chapter12/Sect9a_Ex8.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | public class Sect9a_Ex8 4 | { 5 | public static void main(String[] args) 6 | { 7 | Stream.of(new Car("Buick" ,"Regal" ,25), 8 | new Car("Hyundai","Elantra",27), 9 | new Car("Buick" ,"Skylark",26), 10 | new Car("Hyundai","Accent" ,30)) // Stream 11 | .collect( 12 | Collectors.partitioningBy(x -> 13 | x.mpg >= 30)) // Map> 14 | .forEach( (x,y) -> 15 | System.out.println(x + ": " +y )); 16 | } 17 | } -------------------------------------------------------------------------------- /chapter12/Sect9a_Ex9.java: -------------------------------------------------------------------------------- 1 | package chapter12; 2 | import java.util.stream.*; 3 | public class Sect9a_Ex9 4 | { 5 | public static void main(String[] args) 6 | { 7 | Integer sum = 8 | Stream.of(new Car("Buick" ,"Regal" ,25), 9 | new Car("Hyundai","Elantra",27), 10 | new Car("Buick" ,"Skylark",26), 11 | new Car("Hyundai","Accent" ,30)) 12 | .collect(Collectors.summingInt(x -> x.mpg)); 13 | 14 | System.out.println("sum of mpg = " + sum); 15 | } 16 | } -------------------------------------------------------------------------------- /chapter13/AllOf.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.CompletableFuture; 3 | public class AllOf 4 | { 5 | static String color1=null; 6 | static String color2=null; 7 | static String color3=null; 8 | public static void main(String[] args) 9 | { 10 | CompletableFuture cf1 11 | = CompletableFuture.supplyAsync( () -> { 12 | color1 = "RED"; return null;}); 13 | CompletableFuture cf2 14 | = CompletableFuture.supplyAsync( () -> { 15 | color2 = "GREEN"; return null;}); 16 | 17 | 18 | CompletableFuture cf3 19 | = CompletableFuture.supplyAsync( () -> { 20 | color3 = "BLUE"; return null;}); 21 | 22 | CompletableFuture 23 | .allOf(cf1,cf2,cf3) // CompletableFuture 24 | .join(); // Void 25 | 26 | System.out.println(color1 + "," + color2 + "," + color3); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /chapter13/MySentence.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.*; 3 | import java.util.function.*; 4 | import java.util.Scanner; 5 | public class MySentence 6 | { 7 | private static Semaphore sem = new Semaphore(0); 8 | private static String word = ""; 9 | public static void main(String[] args) 10 | { 11 | Supplier keyboard = () -> { 12 | Scanner scan = new Scanner(System.in); 13 | while (!word.equals(".")) 14 | { 15 | System.out.print( 16 | "Enter a word (or . when finished):"); 17 | word = scan.nextLine(); 18 | sem.release(); 19 | } 20 | return null; 21 | }; 22 | Supplier concatenator = () -> { 23 | String sentence = ""; 24 | while (!word.equals(".")) 25 | { 26 | 27 | try { 28 | sem.acquire(); 29 | } catch (InterruptedException e) {} 30 | if (!word.equals(".") && sentence.length() > 0) 31 | sentence += " "; 32 | sentence += word; 33 | } 34 | return sentence; 35 | }; 36 | 37 | CompletableFuture keyboardFuture 38 | = CompletableFuture.supplyAsync(keyboard); 39 | CompletableFuture concatFuture 40 | = CompletableFuture.supplyAsync(concatenator); 41 | 42 | BiFunction bi = (x,y) -> y; 43 | 44 | System.out.println(keyboardFuture 45 | .thenCombine(concatFuture, bi) 46 | .join()); 47 | } 48 | } -------------------------------------------------------------------------------- /chapter13/Sect10_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.function.BiFunction; 3 | import java.util.concurrent.CompletableFuture; 4 | public class Sect10_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | CompletableFuture cf4 9 | = CompletableFuture.supplyAsync( () -> "RED"); 10 | CompletableFuture cf5 11 | = CompletableFuture.supplyAsync( () -> "GREEN"); 12 | CompletableFuture cf6 13 | = CompletableFuture.supplyAsync( () -> "BLUE"); 14 | 15 | BiFunction bi = (x,y) -> x + "," + y; 16 | 17 | System.out.println(cf4.thenCombine(cf5, bi) 18 | .thenCombine(cf6, bi) 19 | .join()); 20 | } 21 | } -------------------------------------------------------------------------------- /chapter13/Sect1_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | public class Sect1_Ex1 3 | { 4 | public static void main(String[] args) 5 | { 6 | Runnable r = () -> System.out.println("RED"); 7 | r.run(); 8 | } 9 | } -------------------------------------------------------------------------------- /chapter13/Sect1_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | public class Sect1_Ex2 3 | { 4 | public static void main(String[] args) 5 | { 6 | Runnable r = () -> System.out.println("RED"); 7 | (new Thread(r)).start(); 8 | } 9 | } -------------------------------------------------------------------------------- /chapter13/Sect1_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.Callable; 3 | public class Sect1_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | Callable c1 = () -> "GREEN"; 8 | 9 | try { 10 | String s1 = c1.call(); 11 | System.out.println(s1); 12 | } catch (Exception e) {} 13 | } 14 | } -------------------------------------------------------------------------------- /chapter13/Sect1_Ex4.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.io.*; 3 | import java.util.concurrent.Callable; 4 | public class Sect1_Ex4 5 | { 6 | public static void main(String[] args) 7 | { 8 | Callable c2 = () -> { 9 | BufferedReader br = new BufferedReader( 10 | new FileReader("my.txt")); 11 | return br.readLine(); 12 | }; 13 | try { 14 | String s1 = c2.call(); 15 | System.out.println(s1); 16 | } catch (Exception e) { System.out.println(e.getClass());} 17 | } 18 | } -------------------------------------------------------------------------------- /chapter13/Sect1a_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.Optional; 3 | public class Sect1a_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | String s = null; 8 | Optional.ofNullable(s) 9 | .ifPresentOrElse(x -> System.out.println(x), 10 | () -> System.out.println("empty")); 11 | } 12 | } -------------------------------------------------------------------------------- /chapter13/Sect2_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.*; 3 | import java.util.Random; 4 | public class Sect2_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Callable c = () -> (new Random()).nextInt(100); 9 | Runnable r = () -> System.out.println("Runnable"); 10 | 11 | FutureTask fc = new FutureTask(c); 12 | FutureTask fr = new FutureTask(r,-1); 13 | 14 | fc.run(); 15 | fr.run(); 16 | 17 | try { 18 | int i = fc.get(); 19 | int j = fr.get(); 20 | System.out.println(i + " " + j); 21 | } catch(InterruptedException|ExecutionException e) {} 22 | } 23 | } -------------------------------------------------------------------------------- /chapter13/Sect4_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.*; 3 | public class Sect4_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | try { 8 | System.out.println( 9 | CompletableFuture.completedFuture("RED") // CompletableFuture 10 | .get()); // String 11 | } catch (InterruptedException 12 | |ExecutionException e) {} 13 | } 14 | } -------------------------------------------------------------------------------- /chapter13/Sect4_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.CompletableFuture; 3 | public class Sect4_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | System.out.println( 8 | CompletableFuture.completedFuture("BLUE") // CompletableFuture 9 | .join()); // String 10 | } 11 | } -------------------------------------------------------------------------------- /chapter13/Sect4_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.CompletableFuture; 3 | public class Sect4_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | CompletableFuture cf = CompletableFuture.supplyAsync( () -> { 8 | System.out.println("EXECUTING THE FUTURE"); 9 | return 4; 10 | }); 11 | 12 | System.out.println("NOT EXECUTED YET"); 13 | System.out.println(cf.join()); 14 | } 15 | } -------------------------------------------------------------------------------- /chapter13/Sect5_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.CompletableFuture; 3 | public class Sect5_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | CompletableFuture cf1 = CompletableFuture 8 | .supplyAsync( () -> "3") // CompletableFuture 9 | .thenApply(x -> Integer.parseInt(x)) // CompletableFuture 10 | .thenApply(x -> ++x); // CompletableFuture 11 | 12 | System.out.println(cf1.join()); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter13/Sect5_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.CompletableFuture; 3 | public class Sect5_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | CompletableFuture 8 | .supplyAsync( () -> "RED") // CompletableFuture 9 | .thenApply(x -> 10 | x.equals("RED")? "GREEN":"BLUE") // CompletableFuture 11 | .thenAccept(x -> System.out.println(x));// CompletableFuture 12 | } 13 | } -------------------------------------------------------------------------------- /chapter13/Sect6_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.function.Supplier; 3 | import java.util.concurrent.*; 4 | public class Sect6_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Supplier s1 = () -> { 9 | try { Thread.sleep(5); 10 | } catch (InterruptedException e) {} 11 | return 4; 12 | }; 13 | 14 | Supplier s2 = () -> { 15 | try { Thread.sleep(2); 16 | } catch (InterruptedException e) {} 17 | return 5; 18 | }; 19 | 20 | ExecutorService exec = Executors.newCachedThreadPool(); 21 | CompletableFuture cf1 22 | = CompletableFuture.supplyAsync(s1,exec); 23 | CompletableFuture cf2 24 | = CompletableFuture.supplyAsync(s2,exec); 25 | 26 | CompletableFuture cf3 27 | = cf1.acceptEitherAsync(cf2, 28 | x ->System.out.println(x),exec); 29 | } 30 | } -------------------------------------------------------------------------------- /chapter13/Sect7_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.function.Supplier; 3 | import java.util.concurrent.CompletableFuture; 4 | public class Sect7_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Supplier s1 = () -> 9 | { 10 | try { Thread.sleep(3000); 11 | } catch (InterruptedException e) {} 12 | return 4; 13 | }; 14 | 15 | CompletableFuture cf1 16 | = CompletableFuture.supplyAsync(s1); 17 | 18 | CompletableFuture cf2 = cf1.thenCompose( x -> { 19 | CompletableFuture c = new CompletableFuture(); 20 | c.complete("HI" + Integer.toString(x)); 21 | return c; 22 | }); 23 | 24 | System.out.println(cf2.join()); 25 | } 26 | } -------------------------------------------------------------------------------- /chapter13/Sect8_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.function.Supplier; 3 | import java.util.concurrent.CompletableFuture; 4 | public class Sect8_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Supplier s = () -> { 9 | try { Thread.sleep(10000); 10 | } catch (InterruptedException e) {} 11 | return 5; 12 | }; 13 | 14 | CompletableFuture cf1 15 | = CompletableFuture.supplyAsync(s); 16 | 17 | if (cf1.cancel(true)) 18 | System.out.println("Future cf1 cancelled."); 19 | } 20 | } -------------------------------------------------------------------------------- /chapter13/Sect8_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.*; 3 | import java.util.function.Supplier; 4 | public class Sect8_Ex2 5 | { 6 | public static void main(String[] args) 7 | { 8 | Supplier s = () -> { 9 | try { Thread.sleep(10000); 10 | } catch (InterruptedException e) {} 11 | return 5; 12 | }; 13 | 14 | CompletableFuture cf1 15 | = CompletableFuture.supplyAsync(s); 16 | 17 | cf1.cancel(true); 18 | 19 | try { 20 | System.out.println(cf1.join()); 21 | } catch (CancellationException e) { 22 | System.out.println("Cannot join cancelled future."); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /chapter13/Sect8_Ex3.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.concurrent.*; 3 | public class Sect8_Ex3 4 | { 5 | public static void main(String[] args) 6 | { 7 | CompletableFuture cf2 8 | = CompletableFuture.completedFuture(6); 9 | 10 | if (cf2.cancel(true)) 11 | System.out.println("Future cf2 cancelled."); 12 | 13 | try { 14 | System.out.println(cf2.join()); 15 | } catch (CancellationException e) { 16 | System.out.println("Cannot join cancelled future."); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /chapter13/Sect9_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.function.Supplier; 3 | import java.util.concurrent.CompletableFuture; 4 | public class Sect9_Ex1 5 | { 6 | public static void main(String[] args) 7 | { 8 | Supplier y = () -> { int[] a = {1,2,3}; return a[3]; }; 9 | CompletableFuture 10 | .supplyAsync(y) // CompletableFuture 11 | .exceptionally(x -> 0) // CompletableFuture 12 | .thenAccept(x -> 13 | System.out.println(x)); // CompletableFuture 14 | } 15 | } -------------------------------------------------------------------------------- /chapter13/Sect9_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter13; 2 | import java.util.function.Supplier; 3 | import java.util.concurrent.CompletableFuture; 4 | public class Sect9_Ex2 5 | { 6 | public static void main(String[] args) 7 | { 8 | Supplier z = () -> { int[] a = {1,2,3,4}; return a[3]; }; 9 | 10 | CompletableFuture 11 | .supplyAsync(z) // CompletableFuture 12 | .exceptionally(x -> 0) // CompletableFuture 13 | .thenAccept(x -> 14 | System.out.println(x)); // CompletableFuture 15 | } 16 | } -------------------------------------------------------------------------------- /chapter14/BankAccount.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.*; 3 | import java.util.concurrent.*; 4 | import java.util.function.*; 5 | enum TRANS { DEP, WITH } 6 | class AccountBalance 7 | { 8 | double balance; 9 | public AccountBalance(double d) { balance = d; } 10 | @Override 11 | public String toString() { return Double.toString(balance); } 12 | } 13 | public class BankAccount 14 | { 15 | static AtomicReference b = 16 | new AtomicReference(new AccountBalance(500.0)); 17 | static void accumulate(double value, BinaryOperator op) 18 | { 19 | b.accumulateAndGet(new AccountBalance(value), op); 20 | } 21 | public static void main(String[] args) 22 | { 23 | TRANS[] p1Trans = 24 | {TRANS.DEP, TRANS.WITH, TRANS.DEP, TRANS.WITH, TRANS.WITH}; 25 | TRANS[] p2Trans = 26 | {TRANS.WITH, TRANS.WITH, TRANS.WITH, TRANS.DEP, TRANS.WITH}; 27 | 28 | BinaryOperator deposit = (x,y) -> 29 | new AccountBalance(x.balance + y.balance); 30 | 31 | BinaryOperator withdrawal = (x,y) -> 32 | new AccountBalance(x.balance - y.balance); 33 | 34 | CompletableFuture p1 = 35 | CompletableFuture.supplyAsync( () -> { 36 | for (int i=0; i < p1Trans.length && b.get().balance > 0; 37 | ++i) 38 | { 39 | if (p1Trans[i] == TRANS.DEP) 40 | accumulate(50.0, deposit); 41 | else 42 | accumulate(70.50, withdrawal); 43 | System.out.println("p1: " + b.get().balance); 44 | } 45 | return null; 46 | }); 47 | 48 | CompletableFuture p2 = 49 | CompletableFuture.supplyAsync( () -> { 50 | for (int i=0; i < p2Trans.length && b.get().balance > 0; 51 | ++i) 52 | { 53 | if (p2Trans[i] == TRANS.DEP) 54 | accumulate(20.0, deposit); 55 | else 56 | accumulate(120.50, withdrawal); 57 | System.out.println("p2: " + b.get().balance); 58 | } 59 | return null; 60 | }); 61 | 62 | CompletableFuture.allOf(p1,p2) 63 | .join(); 64 | } 65 | } -------------------------------------------------------------------------------- /chapter14/Safe.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.*; 3 | import java.util.concurrent.atomic.*; 4 | public class Safe 5 | { 6 | static AtomicInteger num = new AtomicInteger(0); 7 | 8 | public static void main(String[] args) 9 | { 10 | 11 | CompletableFuture cf1 = 12 | CompletableFuture.supplyAsync( () -> { 13 | for (int i=0; i < 10000; ++i) 14 | { 15 | int x = num.addAndGet(10); 16 | } 17 | return num.get(); 18 | }); 19 | 20 | CompletableFuture cf2 = 21 | CompletableFuture.supplyAsync( () -> { 22 | for (int i=0; i < 10000; ++i) 23 | { 24 | int x = num.addAndGet(-10); 25 | } 26 | return num.get(); 27 | }); 28 | 29 | CompletableFuture.allOf(cf1, cf2) 30 | .join(); 31 | } 32 | } -------------------------------------------------------------------------------- /chapter14/Sect1a_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.AtomicInteger; 3 | public class Sect1a_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | AtomicInteger a1 = new AtomicInteger(4); 8 | System.out.println(a1.accumulateAndGet(2, (x,y) -> x * y)); 9 | System.out.println(a1.accumulateAndGet(7, (x,y) -> x + y/2)); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter14/Sect1a_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.AtomicInteger; 3 | public class Sect1a_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | AtomicInteger a2 = new AtomicInteger(4); 8 | System.out.println(a2.getAndAccumulate(2, (x,y) -> x * y)); 9 | System.out.println(a2.getAndAccumulate(7, (x,y) -> x + y/2)); 10 | System.out.println(a2.get()); 11 | } 12 | } -------------------------------------------------------------------------------- /chapter14/Sect1b_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.AtomicInteger; 3 | public class Sect1b_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | AtomicInteger a1 = new AtomicInteger(10); 8 | System.out.println(a1.updateAndGet(x -> x * 2)); 9 | System.out.println(a1.updateAndGet(x -> x - 5)); 10 | } 11 | } -------------------------------------------------------------------------------- /chapter14/Sect1b_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.AtomicInteger; 3 | public class Sect1b_Ex2 4 | { 5 | public static void main(String[] args) 6 | { 7 | AtomicInteger a2 = new AtomicInteger(10); 8 | System.out.println(a2.getAndUpdate(x -> x * 2)); 9 | System.out.println(a2.getAndUpdate(x -> x - 5)); 10 | System.out.println(a2.get()); 11 | } 12 | } -------------------------------------------------------------------------------- /chapter14/Sect1c_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.AtomicInteger; 3 | public class Sect1c_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | AtomicInteger a = new AtomicInteger(0); 8 | for (int i=0; i<9; ++i) 9 | { 10 | a.updateAndGet(x -> x + 10); 11 | a.compareAndExchange(50,0); 12 | System.out.println(a.get()); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /chapter14/Sect2_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.AtomicLong; 3 | public class Sect2_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | AtomicLong l = new AtomicLong(10L); 8 | System.out.println(l.accumulateAndGet(2, (x,y) -> x / y)); 9 | l.compareAndExchange(5,3); 10 | System.out.println(l.get()); 11 | } 12 | } -------------------------------------------------------------------------------- /chapter14/Sect3_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.AtomicBoolean; 3 | public class Sect3_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | AtomicBoolean bool = new AtomicBoolean(true); 8 | System.out.println(bool.get()); 9 | bool.compareAndExchange(true,false); 10 | System.out.println(bool.get()); 11 | bool.set(true); 12 | System.out.println(bool.get()); 13 | } 14 | } -------------------------------------------------------------------------------- /chapter14/Sect4_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.AtomicIntegerArray; 3 | public class Sect4_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | final int ARRAY_SIZE = 4; 8 | AtomicIntegerArray arr = new AtomicIntegerArray(ARRAY_SIZE); 9 | 10 | for (int i=0; i < ARRAY_SIZE; ++i) 11 | { 12 | System.out.println(i + ": " 13 | + arr.accumulateAndGet(i, i, 14 | (x,y) -> x + 2*y)); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /chapter14/Sect5_Ex1.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.AtomicReference; 3 | public class Sect5_Ex1 4 | { 5 | public static void main(String[] args) 6 | { 7 | AtomicReference ref = new AtomicReference<>("R"); 8 | for (int i=0; i < 5; ++i) 9 | { 10 | System.out.println(ref.accumulateAndGet(Integer.toString(i), 11 | (x,y) -> x + y)); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /chapter14/Sect5_Ex2.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.function.BinaryOperator; 3 | import java.util.concurrent.atomic.AtomicReference; 4 | class IntAndString 5 | { 6 | int i; 7 | String s; 8 | public IntAndString(int x, String y) { i=x; s=y; } 9 | @Override 10 | public String toString() { return i + " " + s; } 11 | } 12 | 13 | public class Sect5_Ex2 14 | { 15 | public static void main(String[] args) 16 | { 17 | AtomicReference refis = 18 | new AtomicReference<>(new IntAndString(2,"HI")); 19 | for (int i=0; i < 5; ++i) 20 | { 21 | BinaryOperator bi = 22 | (x,y) -> new IntAndString(x.i + y.i,null); 23 | System.out.println( 24 | refis.accumulateAndGet(new IntAndString(i,null), bi)); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /chapter14/ShareAnInt.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.atomic.*; 3 | import java.util.concurrent.*; 4 | public class ShareAnInt 5 | { 6 | public static void main(String[] args) 7 | { 8 | AtomicInteger num = new AtomicInteger(0); 9 | CompletableFuture cf1 = 10 | CompletableFuture.supplyAsync( () -> { 11 | int x = num.get(); 12 | for (int i=0; i<20; ++i) 13 | { 14 | x = num.updateAndGet(z -> z + 10); 15 | if (num.compareAndSet(100, 0)) 16 | { 17 | System.out.println("cf1 resets atomic variable"); 18 | x = num.get(); 19 | } 20 | System.out.println("cf1 a = " + x); 21 | } 22 | return x; 23 | }); 24 | 25 | CompletableFuture cf2 = 26 | CompletableFuture.supplyAsync( () -> { 27 | int x = num.get(); 28 | for (int i=0; i<20; ++i) 29 | { 30 | x = num.updateAndGet(z -> z + 10); 31 | if (num.compareAndSet(80, 0)) 32 | { 33 | System.out.println("cf2 resets atomic variable"); 34 | x = num.get(); 35 | } 36 | System.out.println("cf2 a = " + x); 37 | } 38 | return x; 39 | }); 40 | 41 | CompletableFuture.allOf(cf1,cf2) 42 | .join(); 43 | } 44 | } -------------------------------------------------------------------------------- /chapter14/Unsafe.java: -------------------------------------------------------------------------------- 1 | package chapter14; 2 | import java.util.concurrent.*; 3 | public class Unsafe 4 | { 5 | static int num = 0; 6 | public static void main(String[] args) 7 | { 8 | CompletableFuture cf1 = 9 | CompletableFuture.supplyAsync( () -> { 10 | for (int i=0; i < 10000; ++i) 11 | { 12 | int x = num; 13 | num += 10; 14 | if (num != (x+10)) 15 | System.out.println("cf1 THREAD READ ERROR: x= " 16 | + x +" num= " + num); 17 | } 18 | return num; 19 | }); 20 | 21 | CompletableFuture cf2 = 22 | CompletableFuture.supplyAsync( () -> { 23 | for (int i=0; i < 10000; ++i) 24 | { 25 | int x = num; 26 | num -= 10; 27 | if (num != (x-10)) 28 | System.out.println("cf2 THREAD READ ERROR: x= " 29 | + x +" num= " + num); 30 | } 31 | return num; 32 | }); 33 | 34 | CompletableFuture.allOf(cf1, cf2) 35 | .join(); 36 | } 37 | } -------------------------------------------------------------------------------- /chapter15/TestBuilder.java: -------------------------------------------------------------------------------- 1 | package chapter15; 2 | import javafx.application.Application; 3 | import javafx.scene.Scene; 4 | import javafx.scene.control.*; 5 | import javafx.scene.layout.*; 6 | import javafx.stage.Stage; 7 | import javafx.util.Builder; 8 | import java.util.Scanner; 9 | 10 | public class TestBuilder extends Application 11 | { 12 | @Override 13 | public void start(Stage primaryStage) 14 | { 15 | Builder