List<String> wordList = . . .; long count = 0;
for (String w : wordList)
{
if (w.length() > 10) { count++; }
}Stream<String> words = . . .; long count = words .filter(w -> w.length() > 10) .count();
stream.filter(w -> w.length() > 10)
filter produce new streamsStream<String> fiveLongWords = words .filter(w -> w.length() > 10) .limit(5);
limit(5) needs an element...filter(....) examines elements until it finds one.Which of the following counts whether the given stream has at least ten strings with three letters?
boolean atLeastTen = stream .filter(w -> w.length() == 3) .limit(10) .count() == 10;
boolean atLeastTen = stream .filter(w -> w.length() == 3) .count() >= 10;
of method
Stream<String> words = Stream.of("Mary", "had", "a", "little", "lamb");
Stream<Integer> digits = Stream.of(3, 1, 4, 1, 5, 9);Integer[] digitArray = { 3, 1, 4, 1, 5, 9 };
Stream<Integer> digitStream = Stream.of(digitArray);
Integer objectsintList<String> wordList = new ArrayList<>(); // Populate wordList Stream<String> words = wordList.stream();
String filename = . . .;
try (Stream<String> lineStream = Files.lines(Paths.get(filename)))
{
...
} // File is closed here
Stream<Integer> integers = Stream.iterate(0, n -> n + 1);
Stream<String> parStream = lineStream.parallel()
filter and count run in parallel, each processor working on chunks of the dataIn this program, complete the methods that yield
s.split("") is an array of them.)*, **, ***, ****, ...filter), want to harvest resultscount, sum) yield a single valueString[] result = stream.toArray(String[]::new);
String[]::new is a constructor reference (to the array constructor)String with another class if the stream doesn't contain stringsList or Set, use collect:
List<String> result = stream.collect(Collectors.toList()); Set<String> result = stream.collect(Collectors.toSet());
collect is a Collector objectCollectors to get oneString result = words.collect(Collectors.joining(", "));
// Stream elements separated with commasComplete the method in this program that collects the first n strings of length ≤ maxLength from the collection c in a Set<String> (which might have fewer than n elements after discarding duplicates). Use streams.
List<String> result = list.stream() // Create the stream .filter(w -> w.length() > 10) // Keep long strings .limit(50) // Keep only the first fifty. .collect(Collectors.toList()); // Turn into a list
List<String> result = list.stream().filter(w -> w.length() > 10).limit(50).collect( Collectors.toList()); // Don’t use this formatting style
mapmap transforms stream by applying function to each element
Stream<String> words = Stream.of("A", "Tale", "of", "Two", "Cities");
Stream<String> lowerCaseWords = words.map(w -> w.toLowerCase());
// "a", "tale", "of", "two", "cities"Stream<String> consonantsOnly = lowerCaseWords.map(
w -> w.replaceAll("[aeiou]", ""));
// "", "tl", "f", "tw", "cts"Stream<Integer> consonantCount = consonantsOnly.map(w -> w.length()); // 0, 2, 1, 2, 3
Which of the following yields a stream of all words that start with a or A, converted to lowercase?
Stream<String> result = words
.filter(w -> w.substring(0, 1).equalsIgnoreCase("a"))
.map(w -> w.toLowerCase());
Stream<String> result = words
.map(String::toLowerCase)
.filter(w -> w.substring(0, 1).equals("a"));