100% found this document useful (6 votes)
18 views

PDF Functional Interfaces in Java: Fundamentals and Examples 1st Edition Ralph Lecessi [Lecessi download

Fundamentals

Uploaded by

metilfikespa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (6 votes)
18 views

PDF Functional Interfaces in Java: Fundamentals and Examples 1st Edition Ralph Lecessi [Lecessi download

Fundamentals

Uploaded by

metilfikespa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Download the Full Version of textbook for Fast Typing at textbookfull.

com

Functional Interfaces in Java: Fundamentals and


Examples 1st Edition Ralph Lecessi [Lecessi

https://textbookfull.com/product/functional-interfaces-in-
java-fundamentals-and-examples-1st-edition-ralph-lecessi-
lecessi/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Microservices Patterns With examples in Java 1st Edition


Chris Richardson

https://textbookfull.com/product/microservices-patterns-with-examples-
in-java-1st-edition-chris-richardson/

textboxfull.com

Microservices Patterns With examples in Java 1st Edition


Chris Richardson

https://textbookfull.com/product/microservices-patterns-with-examples-
in-java-1st-edition-chris-richardson-2/

textboxfull.com

A Functional Approach to Java: Augmenting Object-Oriented


Java Code with Functional Principles 1st Edition Ben
Weidig
https://textbookfull.com/product/a-functional-approach-to-java-
augmenting-object-oriented-java-code-with-functional-principles-1st-
edition-ben-weidig/
textboxfull.com

Fundamentals of Information Systems 9th Edition Ralph


Stair

https://textbookfull.com/product/fundamentals-of-information-
systems-9th-edition-ralph-stair/

textboxfull.com
Learning Java Functional Programming 1st Edition Reese
Richard M

https://textbookfull.com/product/learning-java-functional-
programming-1st-edition-reese-richard-m/

textboxfull.com

Electrical Phenomena at Interfaces Second Edition


Fundamentals Measurements and Applications Ohshima

https://textbookfull.com/product/electrical-phenomena-at-interfaces-
second-edition-fundamentals-measurements-and-applications-ohshima/

textboxfull.com

Modern Java in Action Lambdas streams functional and


reactive programming Raoul-Gabriel Urma

https://textbookfull.com/product/modern-java-in-action-lambdas-
streams-functional-and-reactive-programming-raoul-gabriel-urma/

textboxfull.com

Design and Analysis of Connections in Steel Structures


Fundamentals and Examples Alfredo Boracchini

https://textbookfull.com/product/design-and-analysis-of-connections-
in-steel-structures-fundamentals-and-examples-alfredo-boracchini/

textboxfull.com

Cost Accounting Fundamentals: Essential Concepts and


Examples Steven M. Bragg

https://textbookfull.com/product/cost-accounting-fundamentals-
essential-concepts-and-examples-steven-m-bragg/

textboxfull.com
Functional
Interfaces
in Java
Fundamentals and Examples

Ralph Lecessi
Functional Interfaces
in Java
Fundamentals and Examples

Ralph Lecessi
Functional Interfaces in Java: Fundamentals and Examples
Ralph Lecessi
Kendall Park, NJ, USA

ISBN-13 (pbk): 978-1-4842-4277-3 ISBN-13 (electronic): 978-1-4842-4278-0


https://doi.org/10.1007/978-1-4842-4278-0
Library of Congress Control Number: 2019932794

Copyright © 2019 by Ralph Lecessi


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Coordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-
sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a
Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights,
please email [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484242773. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
Table of Contents
About the Author��������������������������������������������������������������������������������������������������� xiii

About the Technical Reviewer���������������������������������������������������������������������������������xv


Foreword���������������������������������������������������������������������������������������������������������������xvii

Chapter 1: Functional Interfaces������������������������������������������������������������������������������ 1


Section 1.1: Interfaces in Java������������������������������������������������������������������������������������������������������ 1
Section 1.2: Enhancements to Interfaces in Java 8 and Java 9���������������������������������������������������� 2
Section 1.3: Functional Interfaces Defined����������������������������������������������������������������������������������� 5
Section 1.4: Implementing Functional Interfaces with Pre-Java 8 Constructs����������������������������� 6
Section 1.5: Providing Default Methods in Functional Interfaces������������������������������������������������ 8
Section 1.6: Providing Static Methods in Functional Interfaces�������������������������������������������������� 11
Section 1.7: Generic Functional Interfaces��������������������������������������������������������������������������������� 13
Section 1.7.1: Generic Functional Interfaces with Type Restrictions������������������������������������� 14
Section 1.8: Specializing a Generic Functional Interface������������������������������������������������������������ 17
PROJECT 1: Playful Pets�������������������������������������������������������������������������������������������������������������� 20
Problem Statement���������������������������������������������������������������������������������������������������������������� 20
Solution��������������������������������������������������������������������������������������������������������������������������������� 20
Short Problems��������������������������������������������������������������������������������������������������������������������������� 25
Long Problems���������������������������������������������������������������������������������������������������������������������������� 26

Chapter 2: Lambda Expressions����������������������������������������������������������������������������� 29


Section 2.1: Lambda Expressions Defined���������������������������������������������������������������������������������� 29
Section 2.2: Using Lambda Expressions to Represent Functional Interfaces���������������������������� 29
Section 2.3: The Scope of a Lambda Expression������������������������������������������������������������������������ 31
Section 2.4: Lambda Argument List Variations��������������������������������������������������������������������������� 32
Section 2.5: Lambda Bodies in Block Form�������������������������������������������������������������������������������� 34

iii
Table of Contents

Section 2.6: Limitations of Lambda Expressions������������������������������������������������������������������������ 37


PROJECT 2: Compute Square������������������������������������������������������������������������������������������������������ 40
Problem Statement���������������������������������������������������������������������������������������������������������������� 40
Solution��������������������������������������������������������������������������������������������������������������������������������� 40
Short Problems��������������������������������������������������������������������������������������������������������������������������� 43
Long Problems���������������������������������������������������������������������������������������������������������������������������� 43

Chapter 3: Predicates��������������������������������������������������������������������������������������������� 45
Section 3.1: The java.util.function Package�������������������������������������������������������������������������������� 45
Section 3.2: The Predicate Interface������������������������������������������������������������������������������������������� 45
Section 3.3: Passing a Predicate to a Method����������������������������������������������������������������������������� 47
Section 3.4: Chains of Functional Interfaces������������������������������������������������������������������������������� 50
Section 3.5: Predicate Chaining Creates Complex Logical Expressions������������������������������������� 50
Section 3.5.1: Chains Involving the OR Operation����������������������������������������������������������������� 50
Section 3.5.2: Chains Involving the AND Operation��������������������������������������������������������������� 52
Section 3.5.3: Chains Involving the ! Operation��������������������������������������������������������������������� 53
Section 3.5.4: Using Predicate.isEqual���������������������������������������������������������������������������������� 54
Section 3.5.5: Using Predicate.not [JAVA 11]������������������������������������������������������������������������ 55
Section 3.6: Overriding Predicate Default Methods�������������������������������������������������������������������� 56
Section 3.7: Specializations of Predicates���������������������������������������������������������������������������������� 57
Section 3.8: Binary Predicates���������������������������������������������������������������������������������������������������� 59
PROJECT 3: Discount Dave���������������������������������������������������������������������������������������������������������� 59
Problem Statement���������������������������������������������������������������������������������������������������������������� 59
Solution��������������������������������������������������������������������������������������������������������������������������������� 60
Short Problems��������������������������������������������������������������������������������������������������������������������������� 67
Long Problems���������������������������������������������������������������������������������������������������������������������������� 67

Chapter 4: Functions���������������������������������������������������������������������������������������������� 69
Section 4.1: The Function Interface�������������������������������������������������������������������������������������������� 69
Section 4.2: Passing a Generic Function to a Method����������������������������������������������������������������� 70
Section 4.2.1: Passing a Function with Restricted or Known Type Parameters�������������������� 71

iv
Table of Contents

Section 4.3: Function Chaining��������������������������������������������������������������������������������������������������� 73


Section 4.3.1: Chains Involving the andThen Method������������������������������������������������������������ 73
Section 4.3.2: Chains Involving the compose Method����������������������������������������������������������� 73
Section 4.4: The Function.identity Method���������������������������������������������������������������������������������� 74
Section 4.5: Specializations of Functions Which Convert from Primitive Types�������������������������� 75
Section 4.6: Specializations of Functions Which Convert to Primitive Types������������������������������ 76
Section 4.7: Non-generic Specializations of Functions��������������������������������������������������������������� 77
Section 4.8: Binary Functions����������������������������������������������������������������������������������������������������� 77
Section 4.9: Creating Chains Using BiFunctions������������������������������������������������������������������������� 78
Section 4.10: Specializations of BiFunctions Which Convert to Primitive Types������������������������� 79
PROJECT 4: Sales Promotions����������������������������������������������������������������������������������������������������� 80
Problem Statement���������������������������������������������������������������������������������������������������������������� 80
Solution��������������������������������������������������������������������������������������������������������������������������������� 80
Short Problems��������������������������������������������������������������������������������������������������������������������������� 92
Long Problems���������������������������������������������������������������������������������������������������������������������������� 93

Chapter 5: Operators���������������������������������������������������������������������������������������������� 95
Section 5.1: The UnaryOperator Interface����������������������������������������������������������������������������������� 95
Section 5.2: Specializations of UnaryOperator���������������������������������������������������������������������������� 96
Section 5.2.1: Chains Involving UnaryOperator Specializations�������������������������������������������� 97
Section 5.3: The BinaryOperator Interface���������������������������������������������������������������������������������� 98
Section 5.4: Non-generic Specializations of BinaryOperator����������������������������������������������������� 99
PROJECT 5: Calculator�������������������������������������������������������������������������������������������������������������� 100
Problem Statement�������������������������������������������������������������������������������������������������������������� 100
Solution������������������������������������������������������������������������������������������������������������������������������� 100
Short Problems������������������������������������������������������������������������������������������������������������������������� 106
Long Problems�������������������������������������������������������������������������������������������������������������������������� 107

Chapter 6: Consumers������������������������������������������������������������������������������������������ 109


Section 6.1: The Consumer Interface���������������������������������������������������������������������������������������� 109
Section 6.2: Using Chains of Consumers to Compute Equations���������������������������������������������� 110

v
Table of Contents

Section 6.3: Using Consumers with println as the Terminal Operation������������������������������������� 112
Section 6.4: Non-generic Specializations of Consumers���������������������������������������������������������� 112
Section 6.5: The BiConsumer Interface������������������������������������������������������������������������������������� 114
Section 6.6: Specializations of BiConsumer������������������������������������������������������������������������������ 115
PROJECT 6: Bank Transactions������������������������������������������������������������������������������������������������� 116
Problem Statement�������������������������������������������������������������������������������������������������������������� 116
Solution������������������������������������������������������������������������������������������������������������������������������� 117
Short Problems������������������������������������������������������������������������������������������������������������������������� 128
Long Problems�������������������������������������������������������������������������������������������������������������������������� 129

Chapter 7: Suppliers��������������������������������������������������������������������������������������������� 131


Section 7.1: The Supplier Interface������������������������������������������������������������������������������������������� 131
Section 7.2: Wrapping User Prompts in a Supplier������������������������������������������������������������������� 133
Section 7.3: Non-generic Specializations of Suppliers������������������������������������������������������������� 135
PROJECT 7: Ticketing System��������������������������������������������������������������������������������������������������� 136
Problem Statement�������������������������������������������������������������������������������������������������������������� 136
Solution������������������������������������������������������������������������������������������������������������������������������� 137
Short Problems������������������������������������������������������������������������������������������������������������������������� 148
Long Problems�������������������������������������������������������������������������������������������������������������������������� 149

Chapter 8: Use in Traversing Objects�������������������������������������������������������������������� 151


Section 8.1: Traversing Objects Using Iterators������������������������������������������������������������������������ 151
Section 8.2: Traversing Java Arrays of Primitive Types������������������������������������������������������������� 152
Section 8.2.1: Using Specializations of PrimitiveIterator����������������������������������������������������� 156
Section 8.3: Traversing Objects Using Spliterators������������������������������������������������������������������� 160
Section 8.4: Traversing Iterable Objects������������������������������������������������������������������������������������ 164
Section 8.5: Traversing Iterable Objects That Contain Java Arrays of Primitives���������������������� 165
Section 8.5.1: Using Specializations of PrimitiveIterator����������������������������������������������������� 169
Section 8.6: Traversing Maps���������������������������������������������������������������������������������������������������� 171
Section 8.7: Traversing Sets������������������������������������������������������������������������������������������������������ 172
PROJECT 8: Payroll�������������������������������������������������������������������������������������������������������������������� 172

vi
Table of Contents

Problem Statement�������������������������������������������������������������������������������������������������������������� 172


Solution������������������������������������������������������������������������������������������������������������������������������� 173
Short Problems������������������������������������������������������������������������������������������������������������������������� 179
Long Problems�������������������������������������������������������������������������������������������������������������������������� 180

Chapter 9: Use in Collections�������������������������������������������������������������������������������� 181


Section 9.1: Removing Elements from a Collection������������������������������������������������������������������ 181
Section 9.2: Populating an Array����������������������������������������������������������������������������������������������� 182
Section 9.3: Replacing the Elements of a List or a Map������������������������������������������������������������ 184
Section 9.4: Parallel Computations on Arrays��������������������������������������������������������������������������� 185
Section 9.5: Map Computations������������������������������������������������������������������������������������������������ 186
Section 9.6: Map Merging��������������������������������������������������������������������������������������������������������� 189
Section 9.7: Functional Interfaces and Sets������������������������������������������������������������������������������ 191
PROJECT 9: Department of Motor Vehicles������������������������������������������������������������������������������� 191
Problem Statement�������������������������������������������������������������������������������������������������������������� 191
Solution������������������������������������������������������������������������������������������������������������������������������� 192
Short Problems������������������������������������������������������������������������������������������������������������������������� 204
Long Problems�������������������������������������������������������������������������������������������������������������������������� 206

Chapter 10: Use in Comparing Objects����������������������������������������������������������������� 209


Section 10.1: The Comparator Interface����������������������������������������������������������������������������������� 209
Section 10.2: Some Useful Comparator Methods��������������������������������������������������������������������� 211
Section 10.3: The Comparator comparing Methods������������������������������������������������������������������ 212
Section 10.4: Specializations of the Comparator comparing Method��������������������������������������� 217
Section 10.5: Building Chains of Comparators�������������������������������������������������������������������������� 218
Section 10.6: Specializing Comparator Chain Components������������������������������������������������������ 221
Section 10.7: Using Comparators to Sort Lists�������������������������������������������������������������������������� 222
Section 10.8: Using Comparators to Sort Java Arrays��������������������������������������������������������������� 224
Section 10.9: Using Comparators to Organize Maps����������������������������������������������������������������� 227
Section 10.10: Using Comparators in BinaryOperator Methods������������������������������������������������ 228
PROJECT 10: Real Estate Broker����������������������������������������������������������������������������������������������� 229

vii
Table of Contents

Problem Statement�������������������������������������������������������������������������������������������������������������� 229


Solution������������������������������������������������������������������������������������������������������������������������������� 230
Short Problems������������������������������������������������������������������������������������������������������������������������� 239
Long Problems�������������������������������������������������������������������������������������������������������������������������� 240

Chapter 11: Use in Optionals�������������������������������������������������������������������������������� 243


Section 11.1: Creating an Optional�������������������������������������������������������������������������������������������� 243
Section 11.2: Determining If an Optional Is Present����������������������������������������������������������������� 244
Section 11.3: Retrieving the Contents of an Optional��������������������������������������������������������������� 245
Section 11.4: Creating Chains of Optionals������������������������������������������������������������������������������� 246
Section 11.5: Printing the Contents of an Optional������������������������������������������������������������������� 248
Section 11.6: Filtering Optionals����������������������������������������������������������������������������������������������� 249
Section 11.7: Optional Chains Involving map and flatmap�������������������������������������������������������� 250
PROJECT 11: Guess a Number�������������������������������������������������������������������������������������������������� 253
Problem Statement�������������������������������������������������������������������������������������������������������������� 253
Solution������������������������������������������������������������������������������������������������������������������������������� 253
Short Problems������������������������������������������������������������������������������������������������������������������������� 256
Long Problems�������������������������������������������������������������������������������������������������������������������������� 257

Chapter 12: Use in Streams���������������������������������������������������������������������������������� 259


Section 12.1: Generating Stream Elements������������������������������������������������������������������������������ 259
Section 12.2: Traversing Streams��������������������������������������������������������������������������������������������� 260
Section 12.3: Filtering Stream Elements����������������������������������������������������������������������������������� 261
Section 12.4: Converting an Object to a Stream����������������������������������������������������������������������� 262
Section 12.5: Sorting Stream Elements������������������������������������������������������������������������������������ 262
Section 12.6: Selecting the Smallest or Largest Element in a Stream�������������������������������������� 263
Section 12.7: flatMap vs. map�������������������������������������������������������������������������������������������������� 264
Section 12.8: Reducing Stream Elements��������������������������������������������������������������������������������� 266
Section 12.9: Collecting Stream Elements into a Mutable Reduction��������������������������������������� 267
Section 12.9.1: Using Prewritten Collectors������������������������������������������������������������������������ 271
Section 12.10: Building Streams Interactively�������������������������������������������������������������������������� 276

viii
Table of Contents

Section 12.11: Displaying Intermediate Results����������������������������������������������������������������������� 277


Section 12.12: Stream Specializations������������������������������������������������������������������������������������� 278
PROJECT 12: Dave’s Part Inventory������������������������������������������������������������������������������������������� 279
Problem Statement�������������������������������������������������������������������������������������������������������������� 279
Solution������������������������������������������������������������������������������������������������������������������������������� 279
Short Problems������������������������������������������������������������������������������������������������������������������������� 290
Long Problems�������������������������������������������������������������������������������������������������������������������������� 290

Chapter 13: Use in Multithreaded Programs�������������������������������������������������������� 293


Section 13.1: Performing Computations Using Runnable and Callable������������������������������������� 293
Section 13.1.1: Using Runnable in Optionals����������������������������������������������������������������������� 297
Section 13.2: Futures and FutureTasks������������������������������������������������������������������������������������� 297
Section 13.3: CompletionStages and CompletableFutures������������������������������������������������������ 298
Section 13.4: Creating CompletableFutures and Retrieving Their Results�������������������������������� 299
Section 13.5: Using the thenApply and thenAccept Methods��������������������������������������������������� 301
Section 13.6: Processing the First Asynchronous Future to Complete������������������������������������� 302
Section 13.7: Making a Future Dependent on Another Future�������������������������������������������������� 303
Section 13.8: Cancelling a Future��������������������������������������������������������������������������������������������� 304
Section 13.9: When Futures Throw Unchecked Exceptions������������������������������������������������������ 306
Section 13.10: Running Futures in Parallel������������������������������������������������������������������������������� 307
PROJECT 13: Sentence Builder������������������������������������������������������������������������������������������������� 313
Problem Statement�������������������������������������������������������������������������������������������������������������� 313
Solution������������������������������������������������������������������������������������������������������������������������������� 313
Short Problems������������������������������������������������������������������������������������������������������������������������� 326
Long Problems�������������������������������������������������������������������������������������������������������������������������� 327

Chapter 14: Use in Atomic Calculations��������������������������������������������������������������� 329


Section 14.1: Atomic Integers��������������������������������������������������������������������������������������������������� 330
Section 14.1.1: Accumulating an Atomic Value������������������������������������������������������������������� 332
Section 14.1.2: Updating an Atomic Value��������������������������������������������������������������������������� 333
Section 14.1.3: Comparing an Atomic Value������������������������������������������������������������������������ 334

ix
Table of Contents

Section 14.2: Atomic Longs������������������������������������������������������������������������������������������������������� 337


Section 14.3: Atomic Booleans������������������������������������������������������������������������������������������������� 338
Section 14.4: Atomic Arrays������������������������������������������������������������������������������������������������������ 338
Section 14.5: Atomic References���������������������������������������������������������������������������������������������� 339
PROJECT 14: Bank Account������������������������������������������������������������������������������������������������������� 341
Problem Statement�������������������������������������������������������������������������������������������������������������� 341
Solution������������������������������������������������������������������������������������������������������������������������������� 341
Short Problems������������������������������������������������������������������������������������������������������������������������� 345
Long Problems�������������������������������������������������������������������������������������������������������������������������� 346

Chapter 15: Use in JavaFX Applications��������������������������������������������������������������� 347


Section 15.1: Handling JavaFX Events�������������������������������������������������������������������������������������� 347
Section 15.2: Building JavaFX UI Components�������������������������������������������������������������������������� 349
Section 15.3: JavaFX Builder Factories������������������������������������������������������������������������������������� 352
Section 15.4: Monitoring Changes in Collections���������������������������������������������������������������������� 356
Section 15.4.1: Monitoring List Changes����������������������������������������������������������������������������� 356
Section 15.4.2: Monitoring Set Changes����������������������������������������������������������������������������� 362
Section 15.4.3: Monitoring Map Changes���������������������������������������������������������������������������� 366
Section 15.5: Invalidating an Observable Object����������������������������������������������������������������������� 370
PROJECT 15: DMV GUI��������������������������������������������������������������������������������������������������������������� 374
Problem Statement�������������������������������������������������������������������������������������������������������������� 374
Solution������������������������������������������������������������������������������������������������������������������������������� 374
Short Problems������������������������������������������������������������������������������������������������������������������������� 388
Long Problems�������������������������������������������������������������������������������������������������������������������������� 389

Appendix: Method References������������������������������������������������������������������������������ 391


Section A.1: Using References to Static Methods to Represent Functional Interfaces������������� 391
Section A.2: References to Instance Methods��������������������������������������������������������������������������� 393
Section A.3: References to Constructors����������������������������������������������������������������������������������� 394
Section A.4: Passing Method References as Arguments����������������������������������������������������������� 395

x
Table of Contents

Section A.5: Representing Functions with Method References������������������������������������������������ 397


Section A.6: Using Method References with Comparators�������������������������������������������������������� 399
Section A.7: Using Method References with Streams��������������������������������������������������������������� 400

Index��������������������������������������������������������������������������������������������������������������������� 405

xi
About the Author
Ralph Lecessi is a software engineer with over 30 years’
professional programming experience in the aerospace,
telecommunications, and payment industries at companies
including Lockheed Martin, Alcatel-Lucent, AT&T, and
Northrop Grumman. He is currently lead embedded
software developer at TranSendIT, Inc in Mount Laurel,
New Jersey.
Ralph is also the author of JAVATM - The Beginnings—a
text on basic Java programming that includes many examples and diagrams.
Ralph is an adjunct professor of programming at Middlesex County College, where
he teaches basic and object-oriented programming in Java. He lives in South Brunswick,
New Jersey, with his wife and two children.

xiii
About the Technical Reviewer
Manuel Jordan Elera is an autodidactic developer and
researcher who enjoys learning new technologies for his
own experiments and creating new integrations. Manuel
won the 2010 Springy Award Community Champion and
Spring Champion 2013. In his little free time, he reads
the Bible and composes music on his guitar. Manuel is
known as dr_pompeii. He has tech-reviewed numerous
books for Apress, including Pro Spring, Fourth Edition
(2014); Practical Spring LDAP (2013); Pro JPA 2, Second
Edition (2013); and Pro Spring Security (2013). Read his
13 detailed tutorials about many Spring technologies, contact him through his blog at
www.manueljordanelera.blogspot.com, and follow him on his Twitter account,
­@dr_pompeii.

xv
Foreword
Functional Interfaces in Java shows how to organize and simplify Java programs through
the use of functional interfaces.
Chapters 1 and 2 introduce functional interfaces and discuss their implementation
using lambda expressions.
Chapters 3 through 7 discuss the functional interfaces present in the java.util.
function package.
Chapters 8 through 15 show how to use functional interfaces to perform various
programming tasks.
The appendix discusses method references and how they can be used in the
implementation of functional interfaces.
The text presents each topic with detailed descriptions and many examples. Each
chapter also contains a project through which I will guide you step by step to the
solution. Each chapter also contains short homework problems which reinforce the
subject matter and longer assignments which utilize the current topic to solve a real-­
world problem.
Knowledge of basic Java programming is needed to understand the examples in this
text. If you need a primer on basic Java, I recommend JAVATM- The Beginnings.
Here are some notes regarding the programming style used in this book:

1) The rules of encapsulation have been relaxed (most class fields


have been given package access) to make the examples easier to
read.

2) Method references are not used in the chapter portion of this


text. Lambda expressions have been used instead to present
a consistent implementation of functional interfaces. Method
references are discussed in the appendix.

All examples have been tested in Java 9, Java 10, and Java 11 unless otherwise noted.
I hope you enjoy reading Functional Interfaces in Java as much as I have enjoyed
writing it.
—Ralph Lecessi

xvii
CHAPTER 1

Functional Interfaces
Section 1.1: Interfaces in Java
In Java, an interface specifies one or more methods. The interface is a contract which
must be honored by all implementing classes. The interface defined in Listing 1-1
specifies methods method1 and method2.

Listing 1-1. I1.java

interface I1
{
    void method1();
    String method2(String x);
}

Any class that implements an interface must provide implementations for all the
methods declared in the interface (or the class must be declared as abstract). Since the
class defined in Listing 1-2 provides implementations for both method1 and method2,
objects of class C1 can be instantiated.

Listing 1-2. C1.java

class C1 implements I1
{
    @Override
    public void method1() {}
    @Override
    public String method2(String x) { return x; }
}

1
© Ralph Lecessi 2019
R. Lecessi, Functional Interfaces in Java, https://doi.org/10.1007/978-1-4842-4278-0_1
Chapter 1 Functional Interfaces

 ection 1.2: Enhancements to Interfaces in Java 8


S
and Java 9
In Java 8, interfaces were allowed to have static and default methods. A static method
has a single instance associated with the interface. A static method can be called without
creation of an object. A default method is an implementation provided by the interface
that does not have to be overridden by an implementing class. Default methods help in
the compilation of legacy code.
The program in Listing 1-3 defines interface I2 that declares field s which contains
the string “I2”. If a constant is common to all classes which implement an interface, it
should be defined as a field in the interface. The interface also defines a static and a
default method.
Since method1 is static, it can be called directly from interface I2, without creating an
object. When called, it prints “I2”, which is the value of field s. Since class C2 overrides
method2, the call to method2 from objC2 executes the C2 implementation, which returns
the string argument and prints “Hello”. Since class C3 does not override method2, the call
to method2 from objC3 executes the default implementation provided in interface I2,
which concatenates the string argument to field s and prints “I2World”.

Listing 1-3. I2.java

interface I2
{
    String s = "I2";
    static void method1()
    {
        System.out.println(s);
    }

    default String method2(String x)


    {
        return s + x;
    }
}

2
Chapter 1 Functional Interfaces

class C2 implements I2
{
    @Override
    public String method2(String x) { return x; }
}

class C3 implements I2 {}

class TestI2
{
    public static void main(String[] args)
    {
        I2.method1();
        I2 objC2 = new C2();
        I2 objC3 = new C3();
        System.out.println(objC2.method2("Hello"));
        System.out.println(objC3.method2("World"));
    }
}

PROGRAM OUTPUT:

I2
Hello
I2World

In Java 9, interfaces were allowed to have private methods. Private methods are
useful to call from default methods. The program in Listing 1-4 concatenates a random
integer between 0 and 99 to the string “Hello”. Interface I3 contains private method
getNumber which generates the integer. This method is called by default method M1.

Listing 1-4. I3.java

import java.util.Random;
interface I3
{
    private int getNumber() { return (new Random()).nextInt(100); }
    default String M1(String s)

3
Chapter 1 Functional Interfaces

    {
        return s + getNumber();
    }
}

class C4 implements I3
{
}

class TestI3
{
    public static void main(String[] args)
    {
        I3 objC4 = new C4();
        System.out.println(objC4.M1("Hello"));
    }
}

PROGRAM OUTPUT:

Hello21

In Java 9, interfaces can also have private static methods. Since the static methods of
an interface can be called without creation of an implementing object, these methods
can only be called by public static methods defined in the interface.
Interface I4 in the following program defines public static method getName which
calls private static method getPrefix to add the prefix “Mr. ” or “Ms. ” based on gender.
The getName method can be called from the main method of class TestI4. The getPrefix
method can be called only from inside method getName.

Listing 1-5. I4.java

interface I4
{
    private static String getPrefix(String p)
    {
        return p.equals("male")? "Mr. " : "Ms. ";
    }
    public static String getName(String n, String p)

4
Chapter 1 Functional Interfaces

    {
        return getPrefix(p) + n;
    }
}

class TestI4
{
    public static void main(String[] args)
    {
        System.out.println(I4.getName("Smith", "female"));
        System.out.println(I4.getName("Jones", "male"));
    }
}

PROGRAM OUTPUT:

Ms. Smith
Mr. Jones

Section 1.3: Functional Interfaces Defined


A functional interface is an interface with a single abstract method, called its functional
method. The code in Listing 1-6 defines functional interface StringProcessor, which
specifies functional method process that takes a string argument and returns a string.
The @FunctionalInterface annotation instructs the compiler to verify that the interface
has only one abstract method.

Listing 1-6. StringProcessor.java


@FunctionalInterface
interface StringProcessor
{
       String process(String x);
}

If StringProcessor contained more than one abstract method, the


@FunctionalInterface annotation would cause a compilation error to be generated.
The code in Listing 1-7 demonstrates.

5
Chapter 1 Functional Interfaces

Listing 1-7. StringProcessor.java (Incorrect)

@FunctionalInterface    ERROR: Not a functional interface.


                               Contains more than one abstract
                               method.
interface StringProcessor
{
       String process(String x);
       String process2(String x);
}

Functional interfaces are ideal for defining a single problem or operation. In Java 8,
the API was enhanced to utilize functional interfaces. Many of the functional interfaces
can contain static and default methods, making them extendable by the user.

 ection 1.4: Implementing Functional Interfaces


S
with Pre-Java 8 Constructs
A functional interface can be implemented by defining a named class that provides
the functional method. The class in Listing 1-8 implements functional interface
StringProcessor by providing an implementation of functional method process.

Listing 1-8. NamedStringProcessor.java

class NamedStringProcessor implements StringProcessor


{
    @Override
    public String process(String s) { return s; }
}

A functional interface can also be implemented by an anonymous class that provides


the functional method. The following code defines an anonymous class that implements
StringProcessor. The anonymous implementation is referenced by variable anonSP.

6
Chapter 1 Functional Interfaces

StringProcessor anonSP = new StringProcessor() {


    @Override
    public String process(String x)
    {
        return x.toUpperCase();
    }
};

The program in Listing 1-9 provides both named class and anonymous class
implementations for functional interface StringProcessor.

Listing 1-9. TestStringProcessor.java

public class TestStringProcessor


{
    public static void main(String[] args)
    {
        NamedStringProcessor namedSP = new NamedStringProcessor();

        StringProcessor anonSP = new StringProcessor() {


            @Override
            public String process(String x)
            {
                return x.toUpperCase();
            }
        };
        System.out.println(namedSP.process("hello"));
        System.out.println(anonSP.process("hello"));
    }
}

PROGRAM OUTPUT:

Hello
HELLO

7
Chapter 1 Functional Interfaces

 ection 1.5: Providing Default Methods in


S
Functional Interfaces
A functional interface can provide default methods. An implementing class can use the
default methods or provide its own versions.
Suppose a program needs to print different receipts based on item, price, discount,
and tax. Listing 1-10 defines the Receipt class that contains the corresponding fields.

Listing 1-10. Receipt.java

class Receipt
{
    String item;
    double price;
    double discount;
    double tax;
    public Receipt(String i, double a, double d, double s)
    {
        item     = i;
        price    = a;
        discount = d;
        tax      = s;
    }
    public Receipt(Receipt r)
    {
        item     = r.item;
        price    = r.price;
        discount = r.discount;
        tax      = r.tax;
    }
}

In order to define a framework for receipt printing, a functional interface with a


functional method that accepts a Receipt argument can be utilized. Listing 1-11 defines
functional interface ReceiptPrinter whose functional method print accepts a Receipt.
ReceiptPrinter also provides a default method that calculates the total amount based

8
Chapter 1 Functional Interfaces

on price, discount, and tax, but allows implementing classes to override the calculation.
The default method calls private method getDiscounterPrice which applies the
discount to the price.

Listing 1-11. ReceiptPrinter.java

@FunctionalInterface
interface ReceiptPrinter
{
    void print(Receipt receipt);

    private double getDiscountedPrice(Receipt receipt)


    {
        return receipt.price
            - (receipt.price * receipt.discount);
    }

    default double computeTotal(Receipt receipt)


    {
        double discountedPrice = getDiscountedPrice(receipt);

        return discountedPrice + (discountedPrice * receipt.tax);


    }
}

The following ReceiptPrinter implementation prints each Receipt field on a


separate line followed by the total. It uses the default total calculation.

ReceiptPrinter simpleReceiptPrinter = new ReceiptPrinter() {


    @Override
    public void print(Receipt receipt)
    {
        System.out.println("\nItem :\t" + receipt.item);
        System.out.println("Price:\t"   + receipt.price);
        System.out.println("Disc:\t"    + receipt.discount);
        System.out.println("Tax:\t"     + receipt.tax);
        System.out.println("Total:\t"   + computeTotal(receipt));
    }
};

9
Chapter 1 Functional Interfaces

The following ReceiptPrinter implementation is for merchants who are exempt from
tax. It provides its own implementation of the total calculation which does not include tax.

ReceiptPrinter exemptReceiptPrinter = new ReceiptPrinter() {


    @Override
    public void print(Receipt receipt)
    {
        System.out.println("\nItem :\t" + receipt.item);
        System.out.println("Price:\t"   + receipt.price);
        System.out.println("Disc:\t"    + receipt.discount);
        System.out.println("Total:\t"   + computeTotal(receipt));
    }
    @Override
    public double computeTotal(Receipt receipt)
    {
        return receipt.price - (receipt.price * receipt.discount);
    }
};

The simpleReceiptPrinter and ExemptReceiptPrinter implementations can be


used to print the same receipt. The following code prints one copy of the receipt whose
total is based on tax and a second copy of the receipt whose total is not based on tax:

Receipt receipt = new Receipt("shirt", 20.00, 0.05, 0.07);


simpleReceiptPrinter.print(receipt);
exemptReceiptPrinter.print(receipt);

OUTPUT:

Item :    shirt
Amount:   20.0
Disc:     0.05
Tax:      0.07
Total:    20.33

Item :    shirt
Amount:   20.0
Disc:     0.05
Total:    19.0
10
Chapter 1 Functional Interfaces

 ection 1.6: Providing Static Methods in Functional


S
Interfaces
A functional interface can also have static methods. Static methods are useful to define
helper methods that are not meant to be overridden by implementing classes.
Listing 1-12 enhances the StringProcessor interface by adding helper methods to
check if a string is uppercase or lowercase.

Listing 1-12. StringProcessor.java (Version 2)

@FunctionalInterface
interface StringProcessor
{
    String process(String s);

    static boolean isLowerCase(String s)


    {
        boolean result = true;
        for (int i = 0; i < s.length() && result; ++i)
            result &= Character.isLowerCase(s.charAt(i));
        return result;
    }

    static boolean isUpperCase(String s)


    {
        boolean result = true;
        for (int i = 0; i < s.length() && result; ++i)
            result &= Character.isUpperCase(s.charAt(i));
        return result;
    }
}

In the following example, one StringProcessor implementation is provided whose


functional method converts a string to lowercase, and another implementation is
provided that converts a string to uppercase:

11
Chapter 1 Functional Interfaces

StringProcessor toLowerCase = new StringProcessor() {


    @Override
    public String process(String s)
    {
        return s.toLowerCase();
    }
};

StringProcessor toUpperCase = new StringProcessor() {


    @Override
    public String process(String s)
    {
        return s.toUpperCase();
    }
};

The static methods can be used to verify the case of the processed string.

String s = toLowerCase.process("FUNCTIONALINTERFACES");
System.out.println(s);
System.out.println("Lower case: " + StringProcessor.isLowerCase(s));
System.out.println("Upper case: " + StringProcessor.isUpperCase(s));

String t = toUpperCase.process(s);
System.out.println("\n" + t);
System.out.println("Lower case: " + StringProcessor.isLowerCase(t));
System.out.println("Upper case: " + StringProcessor.isUpperCase(t));

OUTPUT:

functionalinterfaces
Lower case: true
Upper case: false

FUNCTIONALINTERFACES
Lower case: false
Upper case: true

12
Chapter 1 Functional Interfaces

Section 1.7: Generic Functional Interfaces


Many of the functional interfaces defined in the Java API are generic for one or more
types. Suppose a program needs methods that take two arguments of a type and returns
a result of the same type, but both the type and the operation performed can vary.
A functional interface can be defined that is generic for type X and has a functional
method that accepts two arguments of type X and returns a value of type X.

Listing 1-13. TwoArgsProcessor.java

@FunctionalInterface
interface TwoArgsProcessor<X> {
    X process(X arg1, X arg2);
}

The program simply needs to provide different implementations of


TwoArgsProcessor and call the process method of each. The program in Listing 1-14
declares a TwoArgsProcessor<Integer> implementation that multiplies two Integers.
It also declares a TwoArgsProcessor<Double> implementation that adds two Doubles.
Lastly, it declares a TwoArgsProcessor<String> implementation that compares two
strings and returns the largest.

Listing 1-14. TestTwoArgsProcessor.java

class TestTwoArgsProcessor {
    public static void main(String[] args)
    {
        TwoArgsProcessor<Integer> multiplyInts
            = new TwoArgsProcessor<>() {
                @Override
                public Integer process(Integer arg1, Integer arg2)
                {
                    return arg1 * arg2;
                }
            };

13
Chapter 1 Functional Interfaces

        TwoArgsProcessor<Double> addDoubles
            = new TwoArgsProcessor<>() {
                @Override
                public Double process(Double arg1, Double arg2)
                {
                    return arg1 + arg2;
                }
            };

        TwoArgsProcessor<String> compareStrings
            = new TwoArgsProcessor<>() {
                @Override
                public String process(String arg1, String arg2)
                {
                    return arg1.compareTo(arg2) > 0? arg1: arg2;
                }
            };

        System.out.println(multiplyInts.process(2,3));
        System.out.println(addDoubles.process(4.1,5.2));
        System.out.println(compareStrings.process("ace","age"));
    }
}

PROGRAM OUTPUT:

6
9.3
age

 ection 1.7.1: Generic Functional Interfaces with Type


S
Restrictions
Generic functional interfaces can be restricted to certain types. The class in Listing 1-15
extends the Receipt class to include county tax.

14
Random documents with unrelated
content Scribd suggests to you:
"Now we send one time machine back to 1868 by means of the
other. Then one of us travels to 2068 from 1868 while another goes
to that allegedly same year 2068 from the present, from 1968. See
what I'm getting at?"
Shaheen slowly nodded his head and then closed one eye.
"If there's free will," Blackburn mumbled, "they won't meet."
"Let's look for exclusion principles," Freylinghuysen commented,
putting the balls of his fingers together and staring at the ceiling.
Rowan, ignored, watched them for a few seconds and sat down
again. After a while he began to wonder how many experiments it
would take before Pendelton found the proof he was looking for.
THE END
*** END OF THE PROJECT GUTENBERG EBOOK THE GREAT
IMPLICATION ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying copyright
royalties. Special rules, set forth in the General Terms of Use part of
this license, apply to copying and distributing Project Gutenberg™
electronic works to protect the PROJECT GUTENBERG™ concept
and trademark. Project Gutenberg is a registered trademark, and
may not be used if you charge for an eBook, except by following the
terms of the trademark license, including paying royalties for use of
the Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is very
easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund from
the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law in
the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name
associated with the work. You can easily comply with the terms of
this agreement by keeping this work in the same format with its
attached full Project Gutenberg™ License when you share it without
charge with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears, or
with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning of
this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1 with
active links or immediate access to the full terms of the Project
Gutenberg™ License.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or expense
to the user, provide a copy, a means of exporting a copy, or a means
of obtaining a copy upon request, of the work in its original “Plain
Vanilla ASCII” or other form. Any alternate format must include the
full Project Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt that
s/he does not agree to the terms of the full Project Gutenberg™
License. You must require such a user to return or destroy all
copies of the works possessed in a physical medium and
discontinue all use of and all access to other copies of Project
Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in paragraph
1.F.3, the Project Gutenberg Literary Archive Foundation, the owner
of the Project Gutenberg™ trademark, and any other party
distributing a Project Gutenberg™ electronic work under this
agreement, disclaim all liability to you for damages, costs and
expenses, including legal fees. YOU AGREE THAT YOU HAVE NO
REMEDIES FOR NEGLIGENCE, STRICT LIABILITY, BREACH OF
WARRANTY OR BREACH OF CONTRACT EXCEPT THOSE
PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE THAT THE
FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving it,
you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or entity
that provided you with the defective work may elect to provide a
replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.
1.F.6. INDEMNITY - You agree to indemnify and hold the
Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and distribution
of Project Gutenberg™ electronic works, harmless from all liability,
costs and expenses, including legal fees, that arise directly or
indirectly from any of the following which you do or cause to occur:
(a) distribution of this or any Project Gutenberg™ work, (b)
alteration, modification, or additions or deletions to any Project
Gutenberg™ work, and (c) any Defect you cause.

Section 2. Information about the Mission of


Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many small
donations ($1 to $5,000) are particularly important to maintaining tax
exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About Project


Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.

Project Gutenberg™ eBooks are often created from several printed


editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like