Java 8 stream reduce operation

Java 8 stream reduce method


Reduction operation Stream of the  reduce  method  2 of the parameters received with factors such as the value of the return  (T, T) -> T  is the method . Cheot beonjjae factor to accumulate this value is , str2 is  foreach like in the order the elements coming in the way .

public static void main ( String args [ ] ) {   

List < String > list = Arrays . asList ( new String [ ] { "a" , "b" , "c" } ) ;
Stream < String > stream = list . stream ( ) ;
Optional < String > opt = stream . reduce ( ( str1 , str2 ) - > str1     + str2 ) ;

opt . ifPresent ( System . OUT : : println ) ; }


= > > Return value "abc"


Also other forms of  reduce  Methods 
On the introduction  reduce  methods and forms differently this time to introduce  Reduce method  3 of the arguments given . I want this method to return Used when the type and the type to be calculated are different .   

This  reduce the types  reduce (U,  function 1,  function 2)  and the type ,  C return type cheot beonjjae factor  U of the type is returned . The first argument is used as the starting value . Du beonjjae factor function  1 is  (U, T) -> U  is the form . Factor  2 dogs take the first -th argument to the type it returns . The third argument, function 2, is a function
       
    The operation results of  1 are collected and calculated . Type  (U, U) -> U a . For lift , WedulObject of the object's age both by adding the value of the output you want you use.

List< WedulObject>  objects =  Arrays.asList ( new  WedulObject ( "kim" ,  1 ,  "dev" ),  new  WedulObject ( "jun" ,  2 ,  "insa" ),  new  WedulObject ( "jung" ,  3 ,  "kakao" )));
int  ageSum =  objects.stream (). reduce ( 0 , (result, object) ->  result +  object.getAge (), (result, age) ->  result +  age);
       
System . out . println (ageSum);

In the second argument, the first argument, the start value, and the entered  person 's  age value are added . 0 + 10 + 11 + 12 years of age -th in the argument the two -th at the arguments from the results both adds . 10 + 11 + 12 = 33 

wherein the starting value  1 to change results of  34, 2 to change  35 to be output .   

The reason for using this method   
The input  person 's  age values du beonjjae in factor in parallel calculation and ,  se beonjjae in factor in parallel processing results both to consolidate to return to the type to return to time                 
Is used .

If merely the inputted number of  person of  age values extracted in parallel by addition processing the results and returns want case, the following steps haneunge treatment more effective .

public  static  void  main ( String  args []) {
   
    List< WedulObject>  objects =  Arrays.asList ( new  WedulObject ( "kim" ,  1 ,  "dev" ),  new  WedulObject ( "jun" ,  2 ,  "insa" ),  new  WedulObject ( "jung" ,  3 ,  "kakao" )));
   
    int  ageSum =  objects.stream (). filter (x ->  x.getAge () >  2 ) .mapToInt (WedulObject :: getAge) .sum ();
    System . out . println (ageSum);
}



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations