Core Java · Chapter 30 of 48

Lambdas and functional interfaces

By the end of this chapter you can write a small function as a lambda, pass it around like a value, combine functions, and design your own functional interface. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Core Java
  • 5 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Meera has a new offer every week: ₹10 off at happy hour, 20% off in the monsoon, both at Diwali. Ravi wants the counter to hand out token numbers with one call, and Anjali wants every finished order announced on the screen.

With what you know so far, every offer is a new method and a new if in the billing code, and every combination of offers is one more. What the team really wants is to hand the billing code a piece of behaviour, "take ₹10 off", the way you hand it a number.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Rules.java: chain price functions in the right order

    In Rules.java, write three methods that each return a function. withGst() returns a function that adds 5% GST to a price in rupees, rounding down with integer division (the price times 105, divided by 100). priceTag() returns a function that adds GST first and then puts the rupee sign in front, with no space. gstAfter(discount) returns a function that runs the given discount first and then adds GST to what is left.

    For example, withGst gives 42 for 40, 26 for 25 and 0 for 0; priceTag gives ₹42 for 40 and ₹52 for 50; gstAfter with a discount of 10 off gives 94 for 100 (90, then GST), and with a discount of half gives 21 for 40.

  2. Task 2

    Rules.java: combine predicates to spot the right items

    In Rules.java, make isChai() return a predicate that is true for an item name ending with "chai", ignoring upper and lower case. Make chaiButNotCutting() return a predicate that is true for a chai whose name does not start with "cutting", ignoring case.

    For example, isChai is true for Masala chai and KULHAD CHAI and false for Bun maska. chaiButNotCutting is true for Masala chai and Ginger chai, and false for Cutting chai, cutting chai and Bun maska.

  3. Task 3

    Rules.java: a token dispenser that keeps its own count

    In Rules.java, make tokenDispenser(first) return a Supplier that gives first on its first call, then first + 1, then first + 2, one more each call. Every dispenser keeps its own count: calls on one dispenser never change another's.

    For example, a dispenser started at 101 gives 101, then 102; a second one started at 1 gives 1; the first then gives 103, and the second 2.

  4. Task 4

    Rules.java: a consumer, a bi-function and a unary operator

    In Rules.java, make announceTo(board) return a Consumer that adds "Order ready: " followed by the item to the board list each time it accepts an item. Make lineTotal() return a BiFunction that multiplies a price by a quantity. Make tidyName() return a UnaryOperator that removes the spaces at both ends of a name and puts it in capitals.

    For example, accepting Masala chai and then Bun maska leaves the board as Order ready: Masala chai, Order ready: Bun maska. lineTotal gives 120 for 40 and 3, and 0 for 25 and 0. tidyName gives ANJALI for " anjali " and RAVI KUMAR for Ravi Kumar.

  5. Task 5

    PriceRule.java: your own functional interface for offers

    In PriceRule.java, write three methods of the PriceRule interface. flatOff(rupees) returns a rule that takes that many rupees off a price, but never goes below 0. percentOff(percent) returns a rule that takes that percent off, with the discount rounded down (the price times the percent, divided by 100, is the discount). then(next) returns a new rule that applies this rule first and then next to the result; it works on any rule, a lambda too.

    For example, flatOff(10) gives 30 for 40 and 0 for 5; percentOff(20) gives 20 for 25 and 34 for 42 (a discount of 8). flatOff(10) then percentOff(50) gives 15 for 40; percentOff(50) then flatOff(10) gives 10 for 40; a rule that doubles the price, then flatOff(10), gives 70 for 40.

What you will learn

  • Lambdas: a function written in one line, parameters -> body.
  • Functional interfaces: an interface with one abstract method, which a lambda fills in; the standard ones in java.util.function.
  • Combining: andThen, compose, and, or and negate build bigger functions from small ones.
  • Capturing: which variables a lambda may use, and how it keeps state.

More in Core Java

Build "Lambdas and functional interfaces" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.