1 / 14

java.util.streams

axel-meyers
Download Presentation

java.util.streams

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Proud (stream)umožňuje na principu roury agregované zpracování dat-objektů či int, long, double. Proud je definován sekvencí metod a je tzv. líný – nepracuje napřed do zásoby, nýbrž až po iniciaci terminální operace.Zdroj lze využít jen jednou. Tok zajišťuje jediné běžné vlákno. java.util.streams source - - - intermediate - - - terminal RESULT SUCTION Collection c.stream() Arrays.stream( ) java.nio.file.File.lines( ) bufferedRdr.lines( ) Stream.of( … ) Stream.iterate( , ) IntStream.range( , ) LongStream.range( , ) Stream.generate( Supplier s ) Stream.empty() “ abc“ .chars() .split() Stream.concat(-> | # , -> | # ) Random.ints( ) BitSet filter(Predicate p ) sorted( [Comparator c]) map( Function f ) flatMap( Function f ) peek( Consumer c ) distinct() limit(long n ) skip (long n ) mapToInt(ToIntFunction f) flatMapToInt(Function f) onClose( ) parallel( ) unordered( ) voidforEach(Consumer c ) voidforEachOrdered( Consumer c ) collect( -> ) booleananyMatch( Predicate p ) boolean{all|none}Match( Predicate p ) long count( ) reduce( -> | # ) toArray( ) {max|min}( Comparator c ) booleanfindAny() booleanfindFirst() iterator() spliterator() Lokesh Gupta: http://howtodoinjava.com/2014/04/13/java-8-tutorial-streams-by-examples http://howtodoinjava.com/2014/05/04/read-file-line-by-line-in-java-8-streams-of-lines-example PJV04

  2. java.util.streams pro int/long/double BaseStream Stream Collector AutoCloseable <T,S..> <T> <T,A,R> Characteristics IntStream Stream Support Collectors + …… asDoubleStream() average() boxed() mapToLong( -> ) mapToObj( -> ) staticrange( , ) staticrangeClosed( , ) sum() summaryStatistics() toArray() static static …… stream( , ) intStream( , ) averagingInt( -> | # ) collectingAndThen( , ) counting() groupingBy( ) groupingByConcurrent( ) joining( ) mapping( , ) {max|min}By( ->| # ) partitioningBy( ) reducing( , ) summarizingInt( -> | # ) summingInt( -> | # ) toCollection(-> | # ) toConcurrentMap(-> | # ) toList() toMap(-> | # ) toSet() Int alter Long Double PJV04

  3. .stream.BaseStream<T,S extendsBaseStream<T,S>> boolean isParallel( ) // static methods only Iterator<T> iterator( ) S onClose( Runnable closeHandler ) S parallel( ) S sequential( ) Spliterator<T> spliterator( ) S unordered( ) PJV25

  4. Interfejs java.util.stream.Stream<T> 1/2 short circuitting je potomkemBaseStream<T, Stream<T>> static <T> Stream<T> concat( Stream<? extends T> a, Stream<? extends T> b ) static <T> Stream<T> empty( ) static <T> Stream<T> generate( Supplier<T> s ) static <T> Stream<T> iterate( T seed, UnaryOperator f ) s, f(s), f(f(s)), … static <T> Stream<T> of( T t ), of( T … values ) – hodnota či pole static <T> Stream.Builder<T> build( ) booleanallMatch( ), noneMatch( ), anyMatch( ) <R,A> R collect( Collector<? super T, R> collector ) long count( ) Stream<T> distinct( ) - propustí jenunikátníhodnoty Stream<T> filter( Predicate< ? super T > predicate ) Optional<T> findAny( ), findFirst( ) flatMap( … ) voidforEach( Consumer< ? super T > action ) PJV25

  5. Interfejs java.util.stream.Stream<T> 2/2 Stream<T> limit( longmaxSize ) <R> Stream<R> map( Function<? super T,? extends R> mapper) IntStream mapToInt( ToIntFunction<? super T> mapper) Optional<T> max( ), min( ) Stream<T> peek( Consumer<? super T> action ) - pro diagnostiku Optional<T>reduce( BinaryOperator accumulator ) - kumulace T reduce( T identity, BinaryOperator accumulator ) Stream<T> skip(long n ) - přeskočení Stream<T> sorted( ), sorted( Comparator<? super T> comparator ) Object[ ] toArray( ) PJV25

  6. java.util.stream. StreamSupport public final class StreamSupport { // static methods only IntStreamintStream( Spliterator.ofIntspliterator, boolean parallel ) IntStreamintStream( Supplier <? extendsSpliterator.ofInt> supplier, intcharacteristics, boolean parallel ) <T> Stream<T> stream( Spliterator<T> spliterator, boolean parallel ) <T> Stream<T> stream( Supplier <? extends Spliterator<T>> supplier, intcharacteristics, boolean parallel ) } PJV25

  7. Finální třída java.util.stream.Collectors její statickémetody vracejí kolektory pro praktické redukční operace: <T> Collector<T, ?, Double> averagingInt( ToIntFunction<? super<T> mpr) <T> Collector<T, ?, Long> counting() collectingAndThen <T,K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T> Collector<CharSeq,?,String> joining( CharSequencedeleimiter ) <T,U,A,R> Collector<T,?,R> mapping(Function< > mpr,Collector<> stream) <T> Collector<T,?,Optional<T>> max/minBy(Comparator<? super T> cmp) <T> Collector<T,?,Map<Boolean,D>>partitioningBy(Predicate<?superT> p. <T> Collector<T,?,Optional<T>> reducing( BinaryOperator op ) <T> Collector<T,?,IntSum~> summarizingInt(ToIntFunction<? super<T> m) <T> Collector<T,?,Integer>summingInt(ToIntFunction< ? super T > mpr) <T,C extends Collection <T>>Collector<T,?,C>toCollection( Supplier<C ) <T> Collector<T,?,List<T>> toList(), toSet() toMap toConcurrentMap PJV25

  8. Příklady Integer[ ] a = { 0,1,2,3,4,5,6,7,8,9 }; Stream<Integer> s1 = Stream.of( a );// proud z pole s1.sort( ).forEach( w -> { System.out.println(w); } ); // sort a výpis longcount = s1.collect( Collectors.counting( ) ); // počet položek Stream<Integer> s2 = Stream.of( a ).limit( 5 ); // jen prvních pět položek doubleavg = s2.collect( Collectors.averagingInt( x -> x ) ); // průměr Stream<Person> s3 = kolekceOsob.stream( ); // proud z kolekce doubleavg = s3.collect( Collectors.averagingDouble( p -> p.getPlat( ) ) ); List b = Arrays.asList( "A","BB","CCC" ); // tvorbakolekce Stream<String> s4 = b.stream( ); // proud z kolekce doubleavg = s4.collect( Collectors.averagingInt( String::length ) ); Optional w = b.stream( ).reduce( ( u, v) -> u=u+"-"+v); PJV04

  9. Příklady vytvoření statistiky IntConsumerst = newIntSummaryStatistics( ); Integer[ ]a = { 0,1,2,3,4,5,6,7,8,9 }; for (intk : a) st.accept(k); Collection<Integer> b = Arrays.asList( 0,1,2,3,4,5,6,7,8,9 ); Spliterator<Integer> sp = b.spliterator( ); sp.forEachRemaining( st::accept ); Collector<Integer, ?, IntSummaryStatistics> coSt = Collectors.summarizingInt( x -> x ); IntSummaryStatisticsiss = b.stream( ).collect(coSt); Tisk vytvořené statistiky: System.out.println( b.stream( ).collect(Collectors.summarizingInt( x -> x ) ) ); PJV04

  10. Příklad: statistika znaků List<String> Stream<String> IntStream String[] p={ "AAA", "BB", "C" }; int[] stat=newint[128]; Arrays.asList(p) // tvorba statistiky .stream().forEach( s -> s.chars().forEach(n-> stat[n]++)); for (int i = 0; i < stat.length; i++) // tisk statistiky if(stat[i]>0) System.out.println( (char)i+" "+stat[i]); PJV04

  11. Příklad grouping a partitioning Budiž Collection col = Arrays.asList("A","BB","CCC","DDD","EE","F"); Stream<String> stream = col.stream(); Map<Integer, List<String>> map = stream .collect( Collectors.groupingBy( x->x.length( ) ) ); System.out.println(map ); // ~{ 1=[A, F], 2=[BB, EE], 3=[CCC, DDD] } Stream<String> stream = col.stream(); Map<Boolean, List<String>> map= stream .collect( Collectors.partitioningBy(t-> t.length( )%2==0)); System.out.println(map ); // ~{ false=[A, CCC, DDD, F], true=[BB, EE] } PJV04

  12. Příklady redukce Budiž Collection col = Arrays.asList("A","BB","CCC","DDD","EE","F"); Stream<String> stream = col.stream(); Optional<Integer> opt = stream .map(t-> t.length()) .reduce( ( a, e ) -> a + e ); System.out.println( opt ); // ~ Optional[12] • OptionalInt opt = IntStream.iterate( 111, i -> --i ).limit( 5 ) .reduce( ( acu, elem ) -> Math.min( acu, elem ) ); System.out.println( opt ); // ~ Optional[107] System.out.println( IntStream.iterate( 1, i -> i+1 ).limit( 5 ) .reduce( ( a, e ) -> a * e ).getAsInt( ) ); // = 120 = 5 ! PJV04

  13. Příklad konverze proudu a redukce ekvivalentní možnosti např. možnosti Stream<Integer> s=list.stream(); double result = s.filter( x -> true) // čirá propust .map(R -> ++R) // inkrementace hodnot .map( r -> new Double(r) ) .map( r -> Double.valueOf(r) ) // konverze do Double .map( r -> Double.valueOf ) .map(Double::new ) .collect( Collectors.summingDouble( Double::doubleValue ) ) ; .collect( Collectors.averagingDouble( r -> r.doubleValue() ) ); .collect( Collectors.averagingDouble( r -> r ) ); System.out.println( result ); PJV04

  14. Příkladspliteratoru s externími zdroji ekvivalentní možnosti Stream<String> s= java.nio.file.File.lines(Paths.get(".", "data.txt")); newBufferedReader( newFileReader( "data.txt" ) ).lines( ); Spliterator<String> sp = s.limit( 42 ) // jentrochudat .spliterator( ); Cons cons = new Cons( ); // konzumující objekt while ( sp.tryAdvance( cons::accept ) ) ; class Cons implements Consumer<String> { public void accept( String t ) { System.out.print( t ); } } PJV04

More Related