functional programming - java 8 How to only return sum and count values from SummaryStatistics -


as know,summarystatistics contain count, sum, min, average, max values. possibly more efficient if return count , sum values?if so,how achieve that?

you can edit below code show me:

stream.of(0.55f, 0.45f, 0.5f, 0.65f, 0f).             filter(i -> > 0.5).maptoint(i -> (int) (i * 100 - 50)).summarystatistics() 

thank you

if understood correctly, asking if should traverse source twice or once , use intsummarystatistics. cheaper:

stream.of(0.55f, 0.45f, 0.5f, 0.65f, 0f)             .filter(i -> > 0.5)             .maptoint(i -> (int) (i * 100 - 50))             .count();  stream.of(0.55f, 0.45f, 0.5f, 0.65f, 0f)             .filter(i -> > 0.5)             .maptoint(i -> (int) (i * 100 - 50))             .sum(); 

versus:

  intsummarystatistics summarystatistics = stream.of(0.55f, 0.45f, 0.5f, 0.65f, 0f)             .filter(i -> > 0.5)             .maptoint(i -> (int) (i * 100 - 50))             .summarystatistics();      summarystatistics.getsum();     summarystatistics.getcount(); 

without measuring it's hard say, notice in case of using count if there no operations clear sized flag, operations entirely skipped. example:

long result = stream.of(1, 2, 3, 4, 5)             .maptoint(x -> {                 system.out.println("mapping");                 return x;             })             .count();  system.out.println(result); 

the maptoint not have processed @ here - , in fact in java-9 skipped. same optimization not done intsummarystatistics, count computed always. not case in example, thing know about.

since use count , sum either way, still choose intsummarystatistics on iterating twice. 2 other operations done inside intsummarystatistics math.min , math.max.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -