Java 8 :: Streams and Lambda Expressions
/*
* Problem No: #6
* Problem Definition: How to use Aggregate Operations with Arrays.stream() [Part 1]?
*
* 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.
*
* > OptionalInt java.util.stream.IntStream.max()
OptionalInt max():
Returns an OptionalInt describing the maximum element of this stream, or an empty optional if this stream is empty. This is a special case of a reduction and is equivalent to:
return reduce(Integer::max);
This is a terminal operation.
Returns:
an OptionalInt containing the maximum element of this stream, or an empty OptionalInt if the stream is empty.
> OptionalInt java.util.stream.IntStream.min()
OptionalInt min()
Returns an OptionalInt describing the minimum element of this stream, or an empty optional if this stream is empty. This is a special case of a reduction and is equivalent to:
return reduce(Integer::min);
This is a terminal operation.
Returns:
an OptionalInt containing the minimum element of this stream, or an empty OptionalInt if the stream is empty
> OptionalDouble java.util.stream.IntStream.average()
OptionalDouble average()
Returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty. This is a special case of a reduction.
This is a terminal operation.
Returns:
an OptionalDouble containing the average element of this stream, or an empty optional if the stream is empty
> long java.util.stream.IntStream.count()
long count()
Returns the count of elements in this stream. This is a special case of a reduction and is equivalent to:
return mapToLong(e -> 1L).sum();
This is a terminal operation.
Returns:
the count of elements in this stream
> int java.util.stream.IntStream.sum()
int sum()
Returns the sum of elements in this stream. This is a special case of a reduction and is equivalent to:
return reduce(0, Integer::sum);
This is a terminal operation.
Returns:
the sum of elements in this stream
> int java.util.OptionalInt.getAsInt()
getAsInt
public int getAsInt()
If a value is present in this OptionalInt, returns the value, otherwise throws NoSuchElementException.
Returns:
the value held by this OptionalInt
Throws:
NoSuchElementException - if there is no value present
> double java.util.OptionalDouble.getAsDouble()
getAsDouble
public double getAsDouble()
If a value is present in this OptionalDouble, returns the value, otherwise throws NoSuchElementException.
Returns:
the value held by this OptionalDouble
Throws:
NoSuchElementException - if there is no value present
*
**Same Operations can be applied with LongStream and DoubleStream
*
* Program By: Mr.DIpak SOnar
* Date : 25th Oct 2016
*/
import java.util.Arrays;
public class StreamWithArrayAggregateOpr {
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).max().getAsInt(): "
+ Arrays.stream(intArr).max().getAsInt() );
System.out.println("Arrays.stream(intArr).min().getAsInt(): "
+ Arrays.stream(intArr).min().getAsInt() );
System.out.println("Arrays.stream(intArr).average().getAsDouble()"
+ Arrays.stream(intArr).average().getAsDouble() );
System.out.println("Arrays.stream(intArr).count(): "
+ Arrays.stream(intArr).count() );
System.out.println("Arrays.stream(intArr).sum(): "
+ Arrays.stream(intArr).sum() );
}
}
/*
* Problem No: #6
* Problem Definition: How to use Aggregate Operations with Arrays.stream() [Part 1]?
*
* 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.
*
* > OptionalInt java.util.stream.IntStream.max()
OptionalInt max():
Returns an OptionalInt describing the maximum element of this stream, or an empty optional if this stream is empty. This is a special case of a reduction and is equivalent to:
return reduce(Integer::max);
This is a terminal operation.
Returns:
an OptionalInt containing the maximum element of this stream, or an empty OptionalInt if the stream is empty.
> OptionalInt java.util.stream.IntStream.min()
OptionalInt min()
Returns an OptionalInt describing the minimum element of this stream, or an empty optional if this stream is empty. This is a special case of a reduction and is equivalent to:
return reduce(Integer::min);
This is a terminal operation.
Returns:
an OptionalInt containing the minimum element of this stream, or an empty OptionalInt if the stream is empty
> OptionalDouble java.util.stream.IntStream.average()
OptionalDouble average()
Returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty. This is a special case of a reduction.
This is a terminal operation.
Returns:
an OptionalDouble containing the average element of this stream, or an empty optional if the stream is empty
> long java.util.stream.IntStream.count()
long count()
Returns the count of elements in this stream. This is a special case of a reduction and is equivalent to:
return mapToLong(e -> 1L).sum();
This is a terminal operation.
Returns:
the count of elements in this stream
> int java.util.stream.IntStream.sum()
int sum()
Returns the sum of elements in this stream. This is a special case of a reduction and is equivalent to:
return reduce(0, Integer::sum);
This is a terminal operation.
Returns:
the sum of elements in this stream
> int java.util.OptionalInt.getAsInt()
getAsInt
public int getAsInt()
If a value is present in this OptionalInt, returns the value, otherwise throws NoSuchElementException.
Returns:
the value held by this OptionalInt
Throws:
NoSuchElementException - if there is no value present
> double java.util.OptionalDouble.getAsDouble()
getAsDouble
public double getAsDouble()
If a value is present in this OptionalDouble, returns the value, otherwise throws NoSuchElementException.
Returns:
the value held by this OptionalDouble
Throws:
NoSuchElementException - if there is no value present
**Same Operations can be applied with LongStream and DoubleStream
*
* Program By: Mr.DIpak SOnar
* Date : 25th Oct 2016
*/
import java.util.Arrays;
public class StreamWithArrayAggregateOpr {
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).max().getAsInt(): "
+ Arrays.stream(intArr).max().getAsInt() );
System.out.println("Arrays.stream(intArr).min().getAsInt(): "
+ Arrays.stream(intArr).min().getAsInt() );
System.out.println("Arrays.stream(intArr).average().getAsDouble()"
+ Arrays.stream(intArr).average().getAsDouble() );
System.out.println("Arrays.stream(intArr).count(): "
+ Arrays.stream(intArr).count() );
System.out.println("Arrays.stream(intArr).sum(): "
+ Arrays.stream(intArr).sum() );
}
}
No comments:
Post a Comment