Giter Site home page Giter Site logo

java9-stream's Introduction

Build Status

java9-stream

preface

New methods:

  • default Stream<T> takeWhile(Predicate<? super T> predicate)
    IntStream.iterate(0, i -> i + 3) // for(int i = 0;; i = i + 3)
    .takeWhile(i -> i <= 20)         //   if (i > 20) break
    .forEach(System.out::println)    //   System.out.println(i)
    
  • default Stream<T> dropWhile(Predicate<? super T> predicate)
  • public static<T> Stream<T> ofNullable(T t)
    • suppose we have function:
      List<String> getX() {...}
      
      that possibly may return null, so:
      getX().stream // could throw NullPointerException
      
      and
      Stream.of(getX())...
      
      could throw NullPointerException also, for example:
      List<String> strings = null;
      
      Stream.of(strings).flatMap(Collection::stream)
              .forEach(System.out::println);        
      
      but with ofNullable it is a lot of easier & null safe
       List<String> strings = null;
      
       String joined = Stream.ofNullable(strings)
               .flatMap(Collection::stream)
               .collect(Collectors.joining());
       
       assertThat(joined, is(""));       
      
  • public static<T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)
    • equivalent of for(seed; Predicate; Function), example: for(int i = 0; i < 10; i = i + 3)
    • old iterate method does not have stop function:
      public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
      
      so in java 8 we have to go through limit functions:
      Stream.iterate(0, x -> ++x).limit(10)...
      
    • in java 9 it is a lot easier, extremely elastic and readable
      Stream.iterate(0, x -> x < 10, x -> ++x)...
      
  • Collectors.filtering(Predicate<? super T> predicate, Collector<? super T,A,R> downstream)
    • applies the predicate to each input element and only accumulating if the predicate returns true
     given:
     List<Expense> expenses = ...
    
     when: 'filter then map'
     Map<Integer, List<Expense>> usingFilter = expenses
             .stream()
             .filter { it.getAmount() > 1_000 }
             .collect(Collectors.groupingBy({ it.getYear() }))
    
     and: 'map then filter'
     Map<Integer, List<Expense>> usingFiltering = expenses
             .stream()
             .collect(Collectors.groupingBy({ it.getYear() },
                     Collectors.filtering({ it.getAmount() > 1_000 }, Collectors.toList())
             ));
    
     then: 'filter then map produces only non empty entries'
     and: 'map then filter produces all entries, and some are empty'
    
  • Collectors.flatMapping​(Function<? super T,? extends Stream<? extends U>> mapper, Collector<? super U,A,R> downstream)
    given:
    List<Expense> expenses = ...
    
    when:
    Map<Integer, Set<Tag>> tagsByYear = expenses
            .stream()
            .collect(Collectors.groupingBy({ it.getYear() },
                    Collectors.flatMapping({ it.getTags().stream() }, Collectors.toSet())
            ));
    
    then: 'tags year by year in a map'
    

project description

Test for above mentioned methods.

java9-stream's People

Contributors

mtumilowicz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.