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

[Ch10c] New code for the new terminal operations section.

parent 92a9fc6d
Loading
Loading
Loading
Loading
+12 −1
Changes for src/main/java/ch10c/HelloLambda.java: 12 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ import java.util.stream.Stream;

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

  static void helloLambda() {
@@ -48,6 +48,17 @@ public class HelloLambda {
                                 .collect(Collectors.toList());
  }

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

    List<String> result = strings.stream()
                                 .sorted((s1, s2) -> s1.compareToIgnoreCase(s2))
                                 .skip(2)
                                 .limit(4)
                                 .collect(Collectors.toList());
    System.out.println("result = " + result);
  }

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

+54 −0
Changes for src/test/java/ch10c/JukeboxQueriesTest.java: 54 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -135,6 +135,39 @@ class JukeboxQueriesTest {
    System.out.println("result = " + genreCount);
  }

  @Test
  void shouldSeeIfThereAreAnyRAndBSongs() {
    List<Song> allSongs = songs.getSongs();

    boolean result = allSongs.stream()
                             .anyMatch(song -> song.getGenre().equals("R&B"));

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

  @Test
  void shouldFindASong() {
    List<Song> allSongs = songs.getSongs();

    Optional<Song> result = allSongs.stream()
                                     .filter(song -> song.getYear() == 1995)
                                     .findFirst();

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

  @Test
  void shouldFindDistinctArtists() {
    List<Song> allSongs = songs.getSongs();

    long result = allSongs.stream()
                          .map(Song::getArtist)
                          .distinct()
                          .count();

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

  // CANDIDATE: exercise 1
  // STRAIGHTFORWARD
  // filter
@@ -162,6 +195,27 @@ class JukeboxQueriesTest {
    System.out.println("result = " + result);
  }

  // GROUPING
  @Test
  void shouldFindNumberOfTimesSongTitleAppears2() {
    List<Song> allSongs = songs.getSongs();

    Map<String, Long> result = allSongs.stream()
                                       .collect(Collectors.groupingBy(Song::getTitle, Collectors.counting()));

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

  // GROUPING
  @Test
  void shouldOutputCSVOfArtists() {
    List<Song> songs = this.songs.getSongs();

    System.out.println(songs.stream()
                            .map(Song::getArtist)
                            .collect(joining(",")));
  }

  // CANDIDATE
  // STRAIGHTFORWARD
  // min terminal