Saturday, January 15, 2022

Java Tutorial: DoubleStream, IntStream and LongStream

Chapters

DoubleStream, IntStream and LongStream

DoubleStream, IntStream and LongStream are variants of Stream class. They specialize in operating on primitive wrapper classes. In this tutorial, I'm gonna demonstrate some methods that are distinct from Stream class.

Lots of the methods of these classes have closely equivalent versions to Stream class methods like filter(), map(), flatMap() and many more. I won't explain these method because I already explained them in this article. You may wanna read that article first before reading this tutorial.

average() Method

This method returns the arithmetic mean of elements of this stream, or an empty optional if this stream is empty. This is a terminal operation. This method is present in DoubleStream, IntStream and LongStream.

This example demonstrate average() in DoubleStream that returns an OptionalDouble.
import java.util.stream.Stream;
import java.util.OptionalDouble;

public class SampleClass{

  public static void main(String[] args){
    Stream<Double> numbers =
    Stream.of(3.5,4.4,1.75,5.55);
    
    OptionalDouble avg = 
    numbers
    .mapToDouble((e) -> e)
    .average();
    
    if(avg.isPresent())
      System.out.println("Average: " + avg.getAsDouble());
    else
      System.out.println("Result is empty!");
    
  }
}

Result
Average: 3.8
boxed() Method

Returns a Stream consisting of the elements of this stream, each boxed to a specified type. This is an intermediate operation. This method is present in DoubleStream, IntStream and LongStream.

This example demonstrates boxed() method in LongStream that returns Stream<Long>.
import java.util.stream.Stream;
import java.util.stream.Collectors;

public class SampleClass{

  public static void main(String[] args){
    Stream<Long> numbers =
    Stream.of(3L,4L,1L,5L);
    
    Long result = 
    numbers
    .mapToLong((v) -> v*v)
    .boxed()
    .collect(
     Collectors
     .summingLong((v) -> v+v));
    
    System.out.println(result);
  }
}

Result
102
mapToObj() Method and its Variants

This method returns an object-valued Stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. mapToObj() is present in DoubleStream, IntStream and LongStream. Other variants like mapToInt(), mapToDouble() and others are present if necessary in DoubleStream, IntStream and LongStream.

For example, mapToLong() is present in DoubleStream and IntStream but not present in LongStream. Moreover, These variants are present in the Stream class.

This example demonstrates mapToObj().
import java.util.stream.Stream;
import java.util.stream.Collectors;

public class SampleClass{

  public static void main(String[] args){
    Stream<Long> numbers =
    Stream.of(3L,4L,1L,5L);
    
    Long result = 
    numbers
    .mapToLong((v) -> v*v)
    .mapToObj((v) -> v+2)
    .collect(
     Collectors
     .summingLong((v) -> v+v));
    
    System.out.println(result);
  }
}

Result
118
range() Method

Returns a sequential ordered IntStream or LongStream starting from the first parameter(inclusive) to second parameter(exclusive) by an incremental step of 1. This method is not present in DoubleStream.

This example demonstrates range() method that returns an IntStream.
import java.util.stream.IntStream;

public class SampleClass{

  public static void main(String[] args){
    
    IntStream.range(3,9)
    .boxed()
    .forEach((v) -> 
    System.out.print(v + " "));
    
  }
}

Result
3 4 5 6 7 8
rangeClosed() Method

Returns a sequential ordered IntStream or LongStream starting from the first parameter(inclusive) to second parameter(inclusive) by an incremental step of 1. This method is not present in DoubleStream.

This example demonstrates rangeClosed() method that returns an IntStream.
import java.util.stream.IntStream;

public class SampleClass{

  public static void main(String[] args){
    
    IntStream.rangeClosed(3,9)
    .boxed()
    .forEach((v) -> 
    System.out.print(v + " "));
    
  }
}

Result
3 4 5 6 7 8 9
sum() Method

Returns the sum of elements in this stream. This method is present in DoubleStream, IntStream and LongStream.

This example demonstrates sum() that returns a double-type value.
import java.util.stream.Stream;

public class SampleClass{

  public static void main(String[] args){
    Stream<Double> numbers = 
    Stream.of(3.5,4.5,5.5,2.5);
    
    double result = 
    numbers
    .mapToDouble(v -> v+v)
    .sum();
    
    System.out.println(result);
  }
}

Result
32.0
summaryStatistics() Method

Returns a statistics describing various summary data about the elements of this stream. This method is present in IntStream, DoubleStream and LongStream and returns IntSummaryStatistics, LongSummaryStatistics and DoubleSummaryStatistics respectively.

This example demonstrates summaryStatistics() that returns an IntSummaryStatistics.
import java.util.stream.Stream;
import java.util.stream.Collectors;
import java.util.IntSummaryStatistics;

public class SampleClass{

  public static void main(String[] args){
    Stream<Integer> numbers = 
    Stream.of(3,4,5,2);
    
    IntSummaryStatistics result = 
    numbers
    .mapToInt(v -> v+v)
    .summaryStatistics();
    
    System.out.println(result);
  }
}

Result
IntSummaryStatistics
{count=4, sum=28, min=4, 
average=7.00000, max=10}

No comments:

Post a Comment