Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Java Coding Problems - Second Edition

You're reading from  Java Coding Problems - Second Edition

Product type Book
Published in Mar 2024
Publisher Packt
ISBN-13 9781837633944
Pages 798 pages
Edition 2nd Edition
Languages
Author (1):
Anghel Leonard Anghel Leonard
Profile icon Anghel Leonard

Table of Contents (16) Chapters

Preface 1. Text Blocks, Locales, Numbers, and Math 2. Objects, Immutability, Switch Expressions, and Pattern Matching 3. Working with Date and Time 4. Records and Record Patterns 5. Arrays, Collections, and Data Structures 6. Java I/O: Context-Specific Deserialization Filters 7. Foreign (Function) Memory API 8. Sealed and Hidden Classes 9. Functional Style Programming – Extending APIs 10. Concurrency – Virtual Threads and Structured Concurrency 11. Concurrency ‒ Virtual Threads and Structured Concurrency: Diving Deeper 12. Garbage Collectors and Dynamic CDS Archives 13. Socket API and Simple Web Server 14. Other Books You May Enjoy
15. Index

207. Implementing a Consumer that takes five (or any other arbitrary number of) arguments

Before continuing with this problem, I strongly recommend that you read Problem 206.

Writing a custom Consumer that takes five arguments can be done as follows:

@FunctionalInterface
public interface FiveConsumer <T1, T2, T3, T4, T5> {
    
  void accept (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5);
}

This is the five-arity specialization of the Java Consumer, just as the built-in BiConsumer is the two-arity specialization of the Java Consumer.

We can use FiveConsumer in conjunction with the PL4 formula, as follows (here, we compute y for x = 40.3):

FiveConsumer<Double, Double, Double, Double, Double> 
  pl4c = (a, b, c, d, x) -> Logistics.pl4(a, b, c, d, x);
        
pl4c.accept(4.19, -1.10, 12.65, 0.03, 40.3);  

The Logistics.pl4() is the method that contains the formula and displays the result:

public static void pl4(Double a, Double b, 
                ...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}