//Java8 IntStream Feature
//PrimitiveIterator is just introduced in java8
import java.util.PrimitiveIterator.OfInt;
//IntSupplier is just introduced in java8 and it is a functional interface
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
public class IntStreamExample
{
public static void main(String args[])
{
System.out.println("-------of() Example------");
// of just takes the arbitrary numbers of int
IntStream ofEx = IntStream.of(1, 2, 3);
ofEx.forEach(x -> System.out.println(x));// It prints 1 2 3
System.out.println("-------range() Example------");
// range just prints the number within the range includes the starting number and excludes the ending number
IntStream stream = IntStream.range(1, 3); // It prints 1 2
// OfInt as a more performance replacement for Iterator<Integer>
OfInt intList = stream.iterator();
while (intList.hasNext())
{
System.out.println(intList.nextInt());
}
System.out.println("-------rangeClosed() Example------");
// rangeClosed just prints the number within the range includes both starting and ending number
IntStream.rangeClosed(1, 3).forEach(System.out::println); // It prints 1 2 3
// IntStream.
System.out.println("-------iterator() Example------");
// iterator just iterate elements based on condition(x->x+1) and limit(Just count)
IntStream.iterate(1, x -> x + 1).limit(5).forEach(System.out::println);// It prints 1 2 3 4 5
System.out.println("-------generator() Example------");
// generator simply takes an IntSupplier that will independently calculate the next int.
// IntSupplier represents a supplier of int-valued results
IntSupplier fib = new IntSupplier()
{
private int previous = 0;
private int current = 1;
public int getAsInt()
{
int nextValue = this.previous + this.current;
this.previous = this.current;
this.current = nextValue;
return this.previous;
}
};
IntStream.generate(fib).limit(10).forEach(System.out::println); // It prints fibonacci series
}
}
//PrimitiveIterator is just introduced in java8
import java.util.PrimitiveIterator.OfInt;
//IntSupplier is just introduced in java8 and it is a functional interface
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
public class IntStreamExample
{
public static void main(String args[])
{
System.out.println("-------of() Example------");
// of just takes the arbitrary numbers of int
IntStream ofEx = IntStream.of(1, 2, 3);
ofEx.forEach(x -> System.out.println(x));// It prints 1 2 3
System.out.println("-------range() Example------");
// range just prints the number within the range includes the starting number and excludes the ending number
IntStream stream = IntStream.range(1, 3); // It prints 1 2
// OfInt as a more performance replacement for Iterator<Integer>
OfInt intList = stream.iterator();
while (intList.hasNext())
{
System.out.println(intList.nextInt());
}
System.out.println("-------rangeClosed() Example------");
// rangeClosed just prints the number within the range includes both starting and ending number
IntStream.rangeClosed(1, 3).forEach(System.out::println); // It prints 1 2 3
// IntStream.
System.out.println("-------iterator() Example------");
// iterator just iterate elements based on condition(x->x+1) and limit(Just count)
IntStream.iterate(1, x -> x + 1).limit(5).forEach(System.out::println);// It prints 1 2 3 4 5
System.out.println("-------generator() Example------");
// generator simply takes an IntSupplier that will independently calculate the next int.
// IntSupplier represents a supplier of int-valued results
IntSupplier fib = new IntSupplier()
{
private int previous = 0;
private int current = 1;
public int getAsInt()
{
int nextValue = this.previous + this.current;
this.previous = this.current;
this.current = nextValue;
return this.previous;
}
};
IntStream.generate(fib).limit(10).forEach(System.out::println); // It prints fibonacci series
}
}
No comments:
Post a Comment