Wednesday, October 26, 2016

Problem No: #13 How to Print Sorted list of Random Numbers using Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #13
 * Problem Definition: How to Print Sorted list of Random Numbers using Stream?
 * 
 * Answer: By Using, 
 * > java.util.Collection.stream(): Returns a sequential Stream with this collection as its source. 
 *  
 *> java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a "terminal operation". 
 * 
 * **Same Operations can be applied with LongStream and DoubleStream
 * Program By: Mr.DIpak SOnar
 * Date : 26th Oct 2016
 */

import java.util.Random;

public class StreamsRandomSortedEx {

public static void main(String[] args) {
Random random = new Random();

random.ints().limit(10).sorted().forEach(System.out::println);
}
}

Problem No: #12 How to Print Limited amount of Random Numbers using Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #12
 * Problem Definition: How to Print Limited amount of Random Numbers using Stream?
 * 
 * Answer: By Using, 
 * > java.util.Collection.stream(): Returns a sequential Stream with this collection as its source. 
 *  
 *> java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a "terminal operation". 
 * 
 * **Same Operations can be applied with LongStream and DoubleStream
 * Program By: Mr.DIpak SOnar
 * Date : 26th Oct 2016
 */

import java.util.Random;

public class StreamsWithRandomEx {

public static void main(String[] args) {
Random random = new Random();

// unlimited random numbers get generated
// random.ints().forEach(System.out::println);

random.ints().limit(10).forEach(System.out::println);
}
}

Problem No: #11 How to Print Stream of Collection type Using Parallel Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #11
 * Problem Definition: How to Print Stream of Collection type Using Parallel Stream?
 * 
 * Answer: By Using, 
 * > java.util.Collection.stream(): Returns a sequential Stream with this collection as its source. 
 *  
 *> java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a "terminal operation". 
 * 
 * **In the case of parallelStream () output differs from stream()
 * Program By: Mr.DIpak SOnar
 * Date : 26th Oct 2016
 */

import java.util.ArrayList;
import java.util.List;

public class ParallelStreamEx {

public static void main(String[] args) {
List<Integer> ListInt = new ArrayList<Integer>();

ListInt.add(21);
ListInt.add(111);
ListInt.add(189);
ListInt.add(3341);
ListInt.add(143);
ListInt.add(131);
ListInt.add(11);
ListInt.add(12);
ListInt.add(121);
ListInt.add(9919);

System.out.println("Printing Stream Using java.util.stream.Stream.forEach() method::");

// parallelStream () output differs from stream()
ListInt.parallelStream().forEach(System.out::println);

List<String> ListStr = new ArrayList<String>();

ListStr.add("Dipak");
ListStr.add("Amar");
ListStr.add("VishwaMitra");
ListStr.add("Naval");
ListStr.add("Alok");
ListStr.add("Virat");
ListStr.add("Pallav");
ListStr.add("Viral");
ListStr.add("Shail");
ListStr.add("Sonar");

System.out.println("Printing Stream Using java.util.stream.Stream.forEach() method::");

// parallelStream () output differs from stream()
ListStr.parallelStream().forEach(System.out::println);

}
}

Problem No: #10 How to Print Only Some Elements in Integer Array using Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #10
 * Problem Definition: How to Print Only Some Elements in Integer Array using Stream?
 * 
 * Answer: By Using, 
 * > java.util.Collection.stream(): Returns a sequential Stream with this collection as its source. 
 *  
 *> java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a "terminal operation". 
 * 
 * **Same Operations can be applied with LongStream and DoubleStream
 * Program By: Mr.DIpak SOnar
 * Date : 26th Oct 2016
 */

import java.util.Arrays;

public class StreamThreeEx {
public static void main(String[] args) {
int intArr[] = new int[15];

 intArr[0] = 11;
 intArr[1] = 21;
 intArr[2] = 31;
 intArr[3] = 41;
 intArr[4] = 51;
 intArr[5] = 61;
 intArr[6] = 71;
 intArr[7] = 81;
 intArr[8] = 91;
 intArr[9] = 21;
 intArr[10] = 22;
 intArr[11] = 23;
 intArr[12] = 24;
 intArr[13] = 25;
 intArr[14] = 26;
     
System.out.println("Printing All Elements of Stream: ");
 
Arrays.stream(intArr).forEach(System.out::println);
     
System.out.println("Printing Limited Elements of Stream: ");
 
Arrays.stream(intArr).limit(4).forEach(System.out::println);

}
}

Problem No: #9 How to Print Distinct Element's Count in Integer Array using Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #9
 * Problem Definition: How to Print Distinct Element's Count in Integer Array using Stream?
 * 
 * Answer: By Using, 
 * > java.util.Collection.stream(): Returns a sequential Stream with this collection as its source. 
 *  
 *> java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a "terminal operation". 
 * 
 * **Same Operations can be applied with LongStream and DoubleStream
 * Program By: Mr.DIpak SOnar
 * Date : 26th Oct 2016
 */

import java.util.Arrays;

public class StreamTwoEx {
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(intArr).forEach(System.out::println);
// To Print Integer Array using Stream

System.out.println("Arrays.stream(intArr).distinct().count()");

System.out.print("Printing Count of Distinct Elements found in Stream: ");

System.out.println(Arrays.stream(intArr).distinct().count());

int intArrOne[] = new int[7];

intArrOne[0] = 11; // Simillar
intArrOne[1] = 21;
intArrOne[2] = 11; // Simillar
intArrOne[3] = 41;
intArrOne[4] = 51;
intArrOne[5] = 61;
intArrOne[6] = 11; // Simillar

System.out.print("Printing Count of Distinct Elements found in Stream: ");

System.out.println(Arrays.stream(intArrOne).distinct().count());


}
}

Problem No: #8 How to Print First Element present in Integer Array using Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #8
 * Problem Definition: How to Print First Element present in Integer Array using Stream?
 * 
 * Answer: By Using, 
 * > java.util.Collection.stream(): Returns a sequential Stream with this collection as its source. 
 *  
 *> java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a "terminal operation". 
 * 
 * **Same Operations can be applied with LongStream and DoubleStream
 * Program By: Mr.DIpak SOnar
 * Date : 26th Oct 2016
 */

import java.util.Arrays;

public class StreamOneEx {

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(intArr).forEach(System.out::println);
// To Print Integer Array using Stream
 
 System.out.print("Printing First Element in Stream use findFirst():");
 System.out.println(Arrays.stream(intArr).findFirst().getAsInt());
 
}
}

Tuesday, October 25, 2016

Problem No: #7 How to use Aggregate Operations with Arrays.stream() Using SummaryStatatistics [Part 2]?

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.

> 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());

}
}

Problem No: #6 How to use Aggregate Operations with Arrays.stream() [Part 1]?

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() );

}
}

Sunday, October 23, 2016

Problem No: #5 How to print Double Array Using Arrays.stream()?

Java 8 :: Streams and Lambda Expressions
/*
 * Problem No: #5
 * Problem Definition: How to print Double Array Using Arrays.stream()?
 * 
 * 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. 
 *
 *> DoubleStream java.util.Arrays.stream(double[] array): Returns a sequential DoubleStream with the specified array as its source.**array - the array, assumed to be unmodified during use.
 * 
 * > java.util.stream.Stream.forEach(): Performs an act ion for each element of this stream.
 *  
 * Program By: Mr.DIpak SOnar
 * Date : 23rd Oct 2016
 */

import java.util.Arrays;

public class StreamWithDoubleArrayEx {

 public static void main(String[] args) {
  double doubleArr[] = new double[7];

  doubleArr[0] = 1112.1111;
  doubleArr[1] = 2112.000;
  doubleArr[2] = 3112.6111;
  doubleArr[3] = 411.21161;
  doubleArr[4] = 47.121111;
  doubleArr[5] = 711281111;
  doubleArr[6] = 1117777111;

  System.out.println("Printing double Array Using Arrays.stream(): ");

  Arrays.stream(doubleArr).forEach(System.out::println);
 }

}

Problem No: #4 How to print Long Array Using Arrays.stream()?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #4
 * Problem Definition: How to print Long Array Using Arrays.stream()?
 * 
 * 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. 
 *
 *>  LongStream java.util.Arrays.stream(long[] array): Returns a sequential LongStream with the specified array as its source.
**array - the array, assumed to be unmodified during use.
 * 
 * > java.util.stream.Stream.forEach(): Performs an act ion for each element of this stream.
 *  
 * Program By: Mr.DIpak SOnar
 * Date : 23rd Oct 2016
 */

import java.util.Arrays;

public class StreamWithLongArrayEx {

public static void main(String[] args) {
long longArr[] = new long[7];

longArr[0] = 111111111;
longArr[1] = 211111111;
longArr[2] = 311111111;
longArr[3] = 411111111;
longArr[4] = 511111111;
longArr[5] = 611111111;
longArr[6] = 711111111;

System.out.println("Printing long Array Using Arrays.stream(): ");

Arrays.stream(longArr).forEach(System.out::println);
}
}

Problem No: #3 How to print Integer Array Using Arrays.stream()?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #3
 * Problem Definition: How to print Integer Array Using Arrays.stream()?
 * 
 * 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.
 * 
 *> java.util.stream.Stream.forEach(): Performs an act ion for each element of this stream.
 *  
 * Program By: Mr.DIpak SOnar
 * Date : 23rd Oct 2016
 */

import java.util.Arrays;

public class StreamWithIntArrayEx {

public static void main(String[] args) {
int[] intArray = new int[7];

intArray[0] = 11;
intArray[1] = 22;
intArray[2] = 33;
intArray[3] = 44;
intArray[4] = 55;
intArray[5] = 66;
intArray[6] = 77;

System.out.println("Printing Integer Array Using Arrays.stream(): ");

Arrays.stream(intArray).forEach(System.out::println);
}
}

Problem No: #2 How to Sort and Print List of Collection type Using Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #2
 * Problem Definition: How to Sort and Print List of Collection type Using Stream?
 * 
 * Answer: By Using, 
*>java.util.Collection.stream(): Returns a sequential Stream with this collection as its source. 
 * >java.util.stream.Stream.sorted(): Returns a stream consisting of the elements of this stream, sorted according to natural order.This is a stateful intermediate operation.
 * >java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a terminal operation. 
 * 
 * NOTE : In Case of Using java.util.stream.Stream.sorted() method for ordered streams, the sort is stable. For unordered streams, no stability guarantees are made. 
 *  
 * Program By: Mr.DIpak SOnar
 * Date : 23rd Oct 2016
 */

import java.util.ArrayList;
import java.util.List;

public class StreamCollectionInitEx {

public static void main(String[] args) {
List<Integer> ListInt = new ArrayList<Integer>();

ListInt.add(21);
ListInt.add(111);
ListInt.add(189);
ListInt.add(3341);
ListInt.add(143);
ListInt.add(131);
ListInt.add(11);
ListInt.add(12);
ListInt.add(121);
ListInt.add(9919);

System.out.println("Sorting and Printing Stream Using java.util.stream.Stream.sorted() method::");

ListInt.stream().sorted().forEach(System.out :: println);

List<String> ListStr = new ArrayList<String>();

ListStr.add("Dipak");
ListStr.add("Amar");
ListStr.add("VishwaMitra");
ListStr.add("Naval");
ListStr.add("Alok");
ListStr.add("Virat");
ListStr.add("Pallav");
ListStr.add("Viral");
ListStr.add("Shail");
ListStr.add("Sonar");

System.out.println("Sorting & Printing Stream Using java.util.stream.Stream.sorted().forEach() method::");

ListStr.stream().sorted().forEach(System.out::println);


}
}