Esempio n. 1
0
  @Test
  public void _03_filter() {
    Stream<String> longWords = words.stream().filter(w -> w.length() > 12);

    Optional<String> first = longWords.findFirst();
    assertThat(first.isPresent(), is(true));
    assertThat(first.get(), is("conversations"));
  }
Esempio n. 2
0
 @Test
 public void shouldFindFirst() {
   Stream<Fruit> fruits = asList(new Fruit(APPLE), new Fruit(GRAPE)).stream();
   Optional<Fruit> first = fruits.findFirst();
   if (first.isPresent()) {
     Fruit firstFruit = first.get();
     assertThat(firstFruit.getName()).isEqualTo(APPLE);
   } else fail("should've find apple.");
 }
Esempio n. 3
0
  @Test
  public void _04_서브스트림_추출() {
    // limit
    Stream<Double> randoms = Stream.generate(Math::random).limit(100);

    assertThat(randoms.count(), is(100L));

    // skip
    Stream<String> words = Stream.of(contents.split("[\\P{L}]+")).skip(1);
    assertThat(words.findFirst().get(), is("was"));
  }
Esempio n. 4
0
  @Test
  public void _05_상태유지변환_StatefulTransformation() {
    // distinct는 이전 값을 기억하고 있어야 한다.
    Stream<String> uniqueWords =
        Stream.of("merrily", "merrily", "merrily", "gently", "merrily").distinct();

    final List<String> actual = uniqueWords.collect(Collectors.toList());
    assertThat(actual.get(0), is("merrily"));
    assertThat(actual.get(1), is("gently"));

    // sort도 이전 값을 기억하고 있어야한다. (전체 값을 다 알고 있어야..)
    Stream<String> longestFirst =
        words.stream().sorted(Comparator.comparing(String::length).reversed());
    final Optional<String> longestFirstFirst = longestFirst.findFirst();
    assertThat(longestFirstFirst.get(), is("disappointment"));
  }