id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

tmhscs@gmail.com streams


 .oooooo..o ooooooooooooo ooooooooo.   oooooooooooo       .o.       ooo        ooooo  .oooooo..o 
d8P'    `Y8 8'   888   `8 `888   `Y88. `888'     `8      .888.      `88.       .888' d8P'    `Y8 
Y88bo.           888       888   .d88'  888             .8"888.      888b     d'888  Y88bo.      
 `"Y8888o.       888       888ooo88P'   888oooo8       .8' `888.     8 Y88. .P  888   `"Y8888o.  
     `"Y88b      888       888`88b.     888    "      .88ooo8888.    8  `888'   888       `"Y88b 
oo     .d8P      888       888  `88b.   888       o  .8'     `888.   8    Y     888  oo     .d8P 
8""88888P'      o888o     o888o  o888o o888ooooood8 o88o     o8888o o8o        o888o 8""88888P'  
Solve all of the following problems again! This time, use Streams though!

To convert an array to a stream, use Arrays.stream(arr)
To convert a list to a stream, use list.stream()

Once you have a stream, you can use as many intermediates as you want, followed by one terminal operation!

Intermediate Operations:
filter(n -> n>0) - keep only the items that the expression returns true on
map(n -> n*2) - replace each item with the expression
distinct() - keep only distinct items (removes duplicates)
sorted() - sorts the items from least to greatest
Terminal Operations:
forEach(n -> System.out.print(n)) - perform this void method on each item
toArray() - converts the stream into an array
collect(Collectors.toList()) - collect the stream as a list
min() - returns the smallest item in the stream
max() - returns the largest item in the stream
count() - returns the number of items in the stream
anyMatch(n -> n==7) - returns true if any items are a 7
allMatch(n -> n==7) - returns true if all items are a 7
noneMatch(n -> n==7) - returns true if no items are a 7
findFirst() - returns an Optional of the first item in the stream
Note: Use .orElse(0) on an Optional to define what should happen if the stream is empty

54. logic_blackJackN H   logic_blackJackN Hint: filter/max/orElse
01. array_firstValue H   array_firstValue Hint: findFirst/orElse
03. array_creation H   array_creation Hint: IntStream/range/toArray
04. array_creationFrom10by2 H   array_creationFrom10by2 Hint: IntStream/range/map/toArray
08. array_numEven H   array_numEven Hint: filter/count
10. array_contains7 H   array_contains7 Hint: anyMatch
15. array_sumElements H   array_sumElements Hint: sum
19. array_findLargest H   array_findLargest Hint: max/orElse
20. array_smallestValue H   array_smallestValue Hint: min/orElse
24. array_negatives H   array_negatives Hint: filter/count
32. array_fivesDisappear H   array_fivesDisappear Hint: filter/toArray
33. array_fivesOnly H   array_fivesOnly Hint: filter/toArray
34. array_fivesToZeroes H   array_fivesToZeroes Hint: map/toArray
35. array_withoutSevens H   array_withoutSevens Hint: filter/toArray
43. array_sortLowToHigh H   array_sortLowToHigh Hint: sorted/toArray
46. array_unique H   array_unique Hint: distinct/count
56. array_fourRules H   array_fourRules Hint: filter/distinct/sorted
20. list_averageElements H   list_averageElements Hint: mapToDouble/average/orElse
21. list_removeEvens H   list_removeEvens Hint: filter/collect
26. list_containsValue H   list_containsValue Hint: anyMatch
32. list_furtherAway H   list_furtherAway Hint: map/collect
35. list_noWordsWithA H   list_noWordsWithA Hint: filter/collect
36. list_removeAudioOutliers H   list_removeAudioOutliers Hint: filter/collect
47. apcsaListReplaceZeros  apcsaListReplaceZeros Hint: mapToDouble/average/orElse

Authoring docs

Copyright Nick Parlante 2017 - privacy