C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
IntStream: This is a special Stream of ints. It has powerful methods that act on ints in an optimized way.
DoubleStream: This is a Stream of doubles. The Arrays.stream method helpfully returns a DoubleStream.
Stream: Some types do not have specialized Streams. We use a Stream of Strings but must specify the type in brackets.
Here: We invoke the anyMatch method on the three streams. We test for a condition in each case. All calls here return true.
LambdaJava program that uses Arrays.stream, IntStream
import java.util.Arrays;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Program {
public static void main(String[] args) {
// Use Arrays.stream to create an IntStream.
int[] array = { 10, 20, 30 };
IntStream stream = Arrays.stream(array);
// ... Call anyMatch on the IntStream.
boolean result = stream.anyMatch(number -> number >= 25);
System.out.println(result);
// Create a DoubleStream.
double[] array2 = { 1.2, 1.3, 1.4 };
DoubleStream stream2 = Arrays.stream(array2);
// ... Test the DoubleStream.
boolean result2 = stream2.anyMatch(number -> number >= 1.35);
System.out.println(result2);
// Create a Stream of Strings.
String[] array3 = { "cat", "dog", "bird" };
Stream<String> stream3 = Arrays.stream(array3);
// ... Test the strings.
boolean result3 = stream3.anyMatch(value -> value.length() >= 4);
System.out.println(result3);
}
}
Output
true
true
true
However: Stream() does not return an IntStream or DoubleStream, even when those types are present.
So: We must use the stream() directly when we get it from an ArrayList. We "cannot cast from a Stream(int) to an IntStream."
Java program that uses ArrayList, Stream
import java.util.ArrayList;
import java.util.stream.Stream;
public class Program {
public static void main(String[] args) {
// Create an Integer ArrayList and add three numbers to it.
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(10);
list.add(200);
list.add(3000);
// Get a Stream from the ArrayList.
Stream<Integer> stream = list.stream();
// Test the Stream.
boolean result = stream.allMatch(value -> value >= 5);
System.out.println(result);
}
}
Output
true
Here: We sort a String Stream (of bird names) and then with forEach display each element.
Java program that uses sorted, forEach
import java.util.ArrayList;
import java.util.stream.Stream;
public class Program {
static void display(String value) {
// Called on each string.
System.out.println("Value: " + value);
}
public static void main(String[] args) {
// Create list of birds.
ArrayList<String> list = new ArrayList<>();
list.add("parrot");
list.add("sparrow");
list.add("finch");
list.add("canary");
// Get the stream and call sorted on it.
Stream<String> sorted = list.stream().sorted();
// ... Call display method on each element in sorted order.
sorted.forEach(x -> display(x));
}
}
Output
Value: canary
Value: finch
Value: parrot
Value: sparrow