Prev:
W4 , Next:
W6
Links:
Zoom ,
TopHat (010339, or
Form ), Calculator:
Eval
Slide:
Go All Prev Next
# Lambda Expressions
📗 Lambda expressions are often used to create anonymous functions without formally defining them.
📗 (args ...) -> {body ...}, for example:
➩ () -> System.out.println("line");
➩ (x) -> System.out.println(x);
➩ (x) -> x * x;
➩ (x, y) -> x * y;
📗
java.util.function interfaces:
Doc
# Void in, Void out Example
📗 public interface Runnable { public void run(); }
➩ Runnable r1 = () -> System.out.println("line");
➩ Runnable r1 = () -> System.out.println("line1"); System.out.println("line2");};
# 1 Value in, Void out Example
📗 public interface Consumer { public void consume(double val1); }
➩ Consumer c1 = (x) -> System.out.println(x);
➩ Consumer c2 = (x) -> {double z = x * x; System.out.println(z);};
# 1 Value in, Value out
📗 public interface Function { public double apply(double val1); }
➩ Function f1 = (y) -> y * y;
➩ Function f2 = (y) -> {double z = y * y; System.out.println(z); return z;};
# 2 Values in, Value out
📗 public interface BiFunction { public double apply(double va1, double val2); }
➩ BiFunction b1 = (x, y) -> x * y;
➩ BiFunction b2 = (x, y) -> {double z = x * y; System.out.println(z); return z;};
# Java Streams [Not on Midterm]
📗 A Java Stream requires:
Doc
➩ Data source.
➩ Chain of intermediate operations: input stream -> operation -> output stream, for example, filter(Predicate), map(Function), limit(int), skip(int).
➩ One terminal operation, for example, findFirst(), forEach(Consumer), count(), min(Comparator), max(Comparator).
# Java Streams Examples [Not on Midterm]
📗 Print all even numbers in a list.
📗 Compute the sum of all odd numbers in a list.
📗 Print the words in upper case in a list.
📗 Print the words in the list that start with "a".
📗 Compute the factorial of a positive integer.
📗 Check if a positive integer is a prime number.
📗 Approximate the sum \(\displaystyle\sum_{i=1}^{\infty} \dfrac{1}{i^{2}} = \dfrac{\pi^{2}}{6}\).
📗 Approximate the continued fraction \(1 + \dfrac{1}{1 + \dfrac{1}{1 + \dfrac{1}{1 + \dfrac{1}{1 + ...}}}} = \dfrac{1 + \sqrt{5}}{2}\).
📗 Approximate the continued fraction \(2 + \dfrac{2}{1 + \dfrac{1}{\dfrac{1}{2} + \dfrac{1}{\dfrac{1}{3} + \dfrac{1}{...}}}} = \pi\).
# Midterm
📗 This is the end of the materials covered on the midterm.
test l
📗 Notes and code adapted from the course taught by Professor Florian Heimerl and Ashley Samuelson
.
📗 Please use Ctrl+F5 or Shift+F5 or Shift+Command+R or Incognito mode or Private Browsing to refresh the cached JavaScript.
📗 You can expand all the examples and demos: Expand , or print the notes: Print .
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google
Form at the end of the lecture.
Prev:
W4 , Next:
W6
Last Updated: November 25, 2025 at 1:46 AM