Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '11'
cache: maven

- name: Compile
run: mvn -B compile

- name: Test
run: mvn -B test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/target
/local
dependency-reduced-pom.xml

# Eclipse, Netbeans and IntelliJ files
/.*
!.gitignore
!.github
/nbproject
/*.ipr
/*.iws
Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ The source code for all examples can be found in the directory [src/main/java/la
* Appendix D: Lambdas and JVM bytecode
We will update the repository as we update the book. Stay tuned!

### Make sure to have JDK8 installed
The latest binary can be found here: http://www.oracle.com/technetwork/java/javase/overview/java8-2100321.html
### Make sure to have JDK 11 (or newer) installed
The 2nd-edition examples use APIs introduced after Java 8 (e.g. `Optional.or`, `Optional.stream`,
`Collectors.flatMapping`/`filtering`, `Stream.takeWhile`), so the project targets Java 11
(`maven.compiler.release=11` in `pom.xml`). A JDK 11 or later distribution is required to build.
You can download a JDK from [Eclipse Temurin (Adoptium)](https://adoptium.net/) or
[Oracle Java](https://www.oracle.com/java/technologies/downloads/).

$ java -version

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)


You can download a preview version here: https://jdk8.java.net/
openjdk version "11.0.22" 2024-01-16
OpenJDK Runtime Environment (build 11.0.22+7)
OpenJDK 64-Bit Server VM (build 11.0.22+7, mixed mode)

### Compile/Run the examples
Using maven:
Expand All @@ -55,5 +56,5 @@ Alternatively you can compile the files manually inside the directory src/main/j

You can also import the project in your favorite IDE:
* In IntelliJ use "File->Open" menu and navigate to the folder where the project resides
* In Eclipse use "File->Import->Existing Maven Projects" (also modify "Reduntant super interfaces" to report as Warnings instead of Errors
* In Eclipse use "File->Import->Existing Maven Projects" (also modify "Redundant super interfaces" to report as Warnings instead of Errors
* In Netbeans use "File->Open Project" menu
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<jmh.version>1.37</jmh.version>
</properties>

<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.17.4</version>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.17.4</version>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand All @@ -35,15 +38,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
<version>3.13.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.3</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/lambdasinaction/chap1/FilteringApples.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ public static void main(String ... args){

List<Apple> inventory = Arrays.asList(new Apple(80,"green"),
new Apple(155, "green"),
new Apple(120, "red"));
new Apple(120, "red"));

// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
List<Apple> greenApples = filterApples(inventory, FilteringApples::isGreenApple);
System.out.println(greenApples);

// [Apple{color='green', weight=155}]
List<Apple> heavyApples = filterApples(inventory, FilteringApples::isHeavyApple);
System.out.println(heavyApples);

// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor()));
System.out.println(greenApples2);

// [Apple{color='green', weight=155}]
List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150);
System.out.println(heavyApples2);

// []
List<Apple> weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() < 80 ||
List<Apple> weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() < 80 ||
"brown".equals(a.getColor()));
System.out.println(weirdApples);
}
Expand All @@ -54,7 +54,7 @@ public static List<Apple> filterHeavyApples(List<Apple> inventory){
}

public static boolean isGreenApple(Apple apple) {
return "green".equals(apple.getColor());
return "green".equals(apple.getColor());
}

public static boolean isHeavyApple(Apple apple) {
Expand All @@ -69,7 +69,7 @@ public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p
}
}
return result;
}
}

public static class Apple {
private int weight = 0;
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/lambdasinaction/chap10/ReadPositiveIntParam.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
package lambdasinaction.chap10;

import org.junit.*;

import java.util.*;

import static java.util.Optional.*;
import static org.junit.Assert.assertEquals;

public class ReadPositiveIntParam {

@Test
public void testMap() {
Properties props = new Properties();
props.setProperty("a", "5");
props.setProperty("b", "true");
props.setProperty("c", "-3");

assertEquals(5, readDurationImperative(props, "a"));
assertEquals(0, readDurationImperative(props, "b"));
assertEquals(0, readDurationImperative(props, "c"));
assertEquals(0, readDurationImperative(props, "d"));

assertEquals(5, readDurationWithOptional(props, "a"));
assertEquals(0, readDurationWithOptional(props, "b"));
assertEquals(0, readDurationWithOptional(props, "c"));
assertEquals(0, readDurationWithOptional(props, "d"));
}

public static int readDurationImperative(Properties props, String name) {
String value = props.getProperty(name);
if (value != null) {
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/lambdasinaction/chap11/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,11 @@ public static void delay() {

public static double format(double number) {
synchronized (formatter) {
return new Double(formatter.format(number));
return Double.parseDouble(formatter.format(number));
}
}

public static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> futures) {
/*
CompletableFuture<Void> allDoneFuture =
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
return allDoneFuture.thenApply(v ->
futures.stream().
map(future -> future.join()).
collect(Collectors.<T>toList())
);
*/
return CompletableFuture.supplyAsync(() -> futures.stream().
map(future -> future.join()).
collect(Collectors.<T>toList()));
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/lambdasinaction/chap1/FilteringApplesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lambdasinaction.chap1;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import lambdasinaction.chap1.FilteringApples.Apple;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class FilteringApplesTest {

private final List<Apple> inventory = Arrays.asList(
new Apple(80, "green"),
new Apple(155, "green"),
new Apple(120, "red"));

@Test
public void filterGreenApplesReturnsOnlyGreen() {
List<Apple> green = FilteringApples.filterGreenApples(inventory);
assertEquals(2, green.size());
assertTrue(green.stream().allMatch(a -> "green".equals(a.getColor())));
}

@Test
public void filterHeavyApplesReturnsApplesOver150() {
List<Apple> heavy = FilteringApples.filterHeavyApples(inventory);
assertEquals(1, heavy.size());
assertEquals(Integer.valueOf(155), heavy.get(0).getWeight());
}

@Test
public void filterApplesWithPredicate() {
List<Apple> redApples = FilteringApples.filterApples(inventory, a -> "red".equals(a.getColor()));
assertEquals(1, redApples.size());
assertEquals("red", redApples.get(0).getColor());
}

@Test
public void isGreenAppleAndIsHeavyApple() {
assertTrue(FilteringApples.isGreenApple(new Apple(50, "green")));
assertFalse(FilteringApples.isGreenApple(new Apple(50, "red")));
assertTrue(FilteringApples.isHeavyApple(new Apple(200, "red")));
assertFalse(FilteringApples.isHeavyApple(new Apple(100, "red")));
}
}
30 changes: 30 additions & 0 deletions src/test/java/lambdasinaction/chap10/ReadPositiveIntParamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lambdasinaction.chap10;

import org.junit.Test;

import java.util.Properties;

import static lambdasinaction.chap10.ReadPositiveIntParam.readDurationImperative;
import static lambdasinaction.chap10.ReadPositiveIntParam.readDurationWithOptional;
import static org.junit.Assert.assertEquals;

public class ReadPositiveIntParamTest {

@Test
public void testMap() {
Properties props = new Properties();
props.setProperty("a", "5");
props.setProperty("b", "true");
props.setProperty("c", "-3");

assertEquals(5, readDurationImperative(props, "a"));
assertEquals(0, readDurationImperative(props, "b"));
assertEquals(0, readDurationImperative(props, "c"));
assertEquals(0, readDurationImperative(props, "d"));

assertEquals(5, readDurationWithOptional(props, "a"));
assertEquals(0, readDurationWithOptional(props, "b"));
assertEquals(0, readDurationWithOptional(props, "c"));
assertEquals(0, readDurationWithOptional(props, "d"));
}
}
15 changes: 15 additions & 0 deletions src/test/java/lambdasinaction/chap11/UtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lambdasinaction.chap11;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class UtilTest {

@Test
public void formatRoundsToTwoDecimals() {
assertEquals(3.14, Util.format(3.14159), 0.0001);
assertEquals(5.0, Util.format(5.0), 0.0001);
assertEquals(2.5, Util.format(2.5), 0.0001);
}
}
Loading