Java 8 :: Streams and Lambda Expressions
/*
* Problem No: #7
* Problem Definition: How to use Aggregate Operations with Arrays.stream() Using SummaryStatatistics[Part 2]?
*
* Answer: By Using,
* * Answer: By Using,
* > java.util.Arrays: This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.
*
* > IntStream java.util.Arrays.stream(int[] array): Returns a sequential IntStream with the specified array as its source. **array - the array, assumed to be unmodified during use.
*
**Same Operations can be applied with LongStream and DoubleStream
*
* Program By: Mr.DIpak SOnar
* Date : 25th Oct 2016
*/
import java.util.Arrays;
public class StreamWithArrayAFEx {
public static void main(String[] args) {
int intArr[] = new int[7];
intArr[0] = 11;
intArr[1] = 21;
intArr[2] = 31;
intArr[3] = 41;
intArr[4] = 51;
intArr[5] = 61;
intArr[6] = 71;
// Arrays.stream(longArr).forEach(System.out::println);
// To Print Integer Array using Stream
System.out.println("Arrays.stream(intArr).summaryStatistics().getMax(): \t"+
Arrays.stream(intArr).summaryStatistics().getMax());
System.out.println("Arrays.stream(intArr).summaryStatistics().getMin(): \t"+ Arrays.stream(intArr).summaryStatistics().getMin());
System.out.println("Arrays.stream(intArr).summaryStatistics().getAverage(): \t"+
Arrays.stream(intArr).summaryStatistics().getAverage());
System.out.println("Arrays.stream(intArr).summaryStatistics().getCount(): \t"+
Arrays.stream(intArr).summaryStatistics().getCount());
System.out.println("Arrays.stream(intArr).summaryStatistics().getSum(): \t"+
Arrays.stream(intArr).summaryStatistics().getSum());
}
}
/*
* Problem No: #7
* Problem Definition: How to use Aggregate Operations with Arrays.stream() Using SummaryStatatistics[Part 2]?
*
* Answer: By Using,
* * Answer: By Using,
* > java.util.Arrays: This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.
*
* > IntStream java.util.Arrays.stream(int[] array): Returns a sequential IntStream with the specified array as its source. **array - the array, assumed to be unmodified during use.
> IntSummaryStatistics java.util.stream.IntStream.summaryStatistics()
IntSummaryStatistics summaryStatistics():
Returns an IntSummaryStatistics describing various summary data about the elements of this stream.
This is a special case of a reduction.
This is a terminal operation.
Returns:
an IntSummaryStatistics describing various summary data about the elements of this stream
> int java.util.IntSummaryStatistics.getMax()
public final int getMax():
Returns the maximum value recorded, or Integer.MIN_VALUE if no values have been recorded.
Returns:
the maximum value, or Integer.MIN_VALUE if none
> int java.util.IntSummaryStatistics.getMin()
public final int getMin():
Returns the minimum value recorded, or Integer.MAX_VALUE if no values have been recorded.
Returns:
the minimum value, or Integer.MAX_VALUE if none
> double java.util.IntSummaryStatistics.getAverage()
public final double getAverage():
Returns the arithmetic mean of values recorded, or zero if no values have been recorded.
Returns:
the arithmetic mean of values, or zero if none
> long java.util.IntSummaryStatistics.getCount()
public final long getCount():
Returns the count of values recorded.
Returns:
the count of values
> long java.util.IntSummaryStatistics.getSum()
public final long getSum():
Returns the sum of values recorded, or zero if no values have been recorded.
Returns:
the sum of values, or zero if none
*
**Same Operations can be applied with LongStream and DoubleStream
*
* Program By: Mr.DIpak SOnar
* Date : 25th Oct 2016
*/
import java.util.Arrays;
public class StreamWithArrayAFEx {
public static void main(String[] args) {
int intArr[] = new int[7];
intArr[0] = 11;
intArr[1] = 21;
intArr[2] = 31;
intArr[3] = 41;
intArr[4] = 51;
intArr[5] = 61;
intArr[6] = 71;
// Arrays.stream(longArr).forEach(System.out::println);
// To Print Integer Array using Stream
System.out.println("Arrays.stream(intArr).summaryStatistics().getMax(): \t"+
Arrays.stream(intArr).summaryStatistics().getMax());
System.out.println("Arrays.stream(intArr).summaryStatistics().getMin(): \t"+ Arrays.stream(intArr).summaryStatistics().getMin());
System.out.println("Arrays.stream(intArr).summaryStatistics().getAverage(): \t"+
Arrays.stream(intArr).summaryStatistics().getAverage());
System.out.println("Arrays.stream(intArr).summaryStatistics().getCount(): \t"+
Arrays.stream(intArr).summaryStatistics().getCount());
System.out.println("Arrays.stream(intArr).summaryStatistics().getSum(): \t"+
Arrays.stream(intArr).summaryStatistics().getSum());
}
}
No comments:
Post a Comment