Commit f7c2859a authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch10c] Five min mystery; and some playing around for method references

parent 49d97946
Loading
Loading
Loading
Loading
+49 −0
Changes for src/main/java/ch10c/FiveMinMystery.java: 49 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10c;

import java.util.List;
import java.util.Optional;

public class FiveMinMystery {
  public static void main(String[] args) {
    List<Coffee> coffees = List.of(new Coffee("Cappuccino", 3),
            new Coffee("Americano", 2), new Coffee("Espresso", 5),
            new Coffee("Cortado", 4), new Coffee("Mocha", 3),
            new Coffee("Flat White", 4), new Coffee("Latte", 3),
            new Coffee("Decaf", 1));

    Optional<String> afternoonCoffee = coffees.stream()
                                              .map(Coffee::getName)
                                              .sorted()
                                              .findFirst();
    System.out.println(afternoonCoffee);
    Optional<String> weakCoffee = coffees.stream()
                                         .sorted()
                                         .map(coffee -> coffee.name)
                                         .findFirst();
    System.out.println(weakCoffee);

  }

  private static class Coffee implements Comparable<Coffee>{
    private final String name;
    private final int strength;

    public Coffee(String name, int strength) {
      this.name = name;
      this.strength = strength;
    }

    public String getName() {
      return name;
    }

    public int getStrength() {
      return strength;
    }

    @Override
    public int compareTo(Coffee other) {
      return this.strength - other.strength;
    }
  }
}
+11 −1
Changes for src/main/java/ch10c/HelloLambda.java: 11 added lines, 1 removed line.
Original line number Diff line number Diff line
package ch10c;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -11,7 +12,7 @@ import java.util.stream.Stream;

public class HelloLambda {
  public static void main(String[] args) {
    mandatoryTypes();
    sortingNaturalOrder();
  }

  static void helloLambda() {
@@ -26,6 +27,15 @@ public class HelloLambda {
    System.out.println("result = " + result);
  }

  static void sortingNaturalOrder() {
    List<String> strings = List.of("I", "am", "a", "list", "of", "Strings");

    List<String> result = strings.stream()
                                 .sorted(Comparator.naturalOrder())
                                 .collect(Collectors.toList());
    System.out.println("result = " + result);
  }

  static void lambdaAsBehaviour() {
    List<String> strings = new ArrayList<>(List.of("I", "am", "a", "list", "of", "Strings"));
    String str = strings.get(2);
+11 −1
Changes for src/test/java/ch10c/JukeboxQueriesTest.java: 11 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -144,7 +144,7 @@ class JukeboxQueriesTest {
    List<Song> allSongs = songs.getSongs();

    List<Song> result = allSongs.stream()
                                .filter(song -> song.getGenre().contains("Rock"))
                                .filter(song -> song.getGenre().equals("Rock"))
                                .collect(toList());

    System.out.println(result);
@@ -175,6 +175,16 @@ class JukeboxQueriesTest {
    System.out.println("result = " + result.get());
  }

  @Test
  void shouldOrderBySongAge() {
    List<Song> allSongs = songs.getSongs();
    List<Song> result = allSongs.stream()
                                .sorted(Comparator.comparingInt(Song::getYear))
                                .collect(toList());

    System.out.println("result = " + result);
  }

  // STRAIGHTFORWARD
  // filter (with param)
  // toList