Java Reference
In-Depth Information
if (maxOdd.isPresent()) {
int value = maxOdd.getAsInt();
System.out.println("Maximum odd integer is " + value);
}
else {
System.out.println("Stream is empty.");
}
// Get the maximum of odd integers from the stream
OptionalInt numbers = IntStream.of(1, 10, 37, 20, 31)
.filter(n -> n % 2 == 1)
.max();
if (numbers.isPresent()) {
int value = numbers.getAsInt();
System.out.println("Maximum odd integer is " + value);
}
else {
System.out.println("Stream is empty.");
}
// Get the longest name
Optional<String> name = Stream.of("Ken", "Ellen", "Li")
.max(Comparator.comparingInt(String::length));
if (name.isPresent()) {
String longestName = name.get();
System.out.println("Longest name is " + longestName);
}
else {
System.out.println("Stream is empty.");
}
}
}
Stream is empty.
Maximum odd integer is 37
Longest name is Ellen
Applying Operations on Streams
Table 13-1 lists some of the commonly used stream operations, their types, and descriptions. You have seen some of
these operations in previous sections. Subsequent sections cover them in detail.
 
 
Search WWH ::




Custom Search