| Start |
|---|
Accumulate names in a List |
Collect example #5 - find people of legal age, output formatted string And as a side note, in Kotlin we can create simple data classes (opens new window) and instantiate the test data as follows: |
Collect example #6 - group people by age, print age and names together Ok, a more interest case here for Kotlin. First the wrong answers to explore variations of creating a And now for the correct answer: We just needed to join the matching values to collapse the lists and provide a transformer to |
Different Kinds of Streams #7 - lazily iterate Doubles, map to Int, map to String, print each |
Counting items in a list after filter is applied |
Convert elements to strings and concatenate them, separated by commas |
Compute sum of salaries of employee |
Group employees by department |
Compute sum of salaries by department |
Partition students into passing and failing |
Names of male members |
Group names of members in roster by gender |
Filter a list to another list |
Finding shortest string a list |
Different Kinds of Streams #2 - lazily using first item if exists |
Different Kinds of Streams #3 - iterate a range of Integers |
Different Kinds of Streams #4 - iterate an array, map the values, calculate the average |
Different Kinds of Streams #5 - lazily iterate a list of strings, map the values, convert to Int, find max |
Different Kinds of Streams #6 - lazily iterate a stream of Ints, map the values, print results |
How streams work - filter, upper case, then sort a list |
Different Kinds of Streams #1 - eager using first item if it exists or, create an extension function on String called ifPresent: See also: See also: Extension Functions (opens new window) See also: |
Collect example #7a - Map names, join together with delimiter |
Collect example #7b - Collect with SummarizingInt But it is better to create an extension function, 2 actually to match styles in Kotlin stdlib: Now you have two ways to use the new And all of these produce the same results. We can also create this extension to work on RemarksAbout lazinessIf you want to lazy process a chain, you can convert to a Why are there no Types?!?You will notice the Kotlin examples do not specify the types. This is because Kotlin has full type inference and is completely type safe at compile time. More so than Java because it also has nullable types and can help prevent the dreaded NPE. So this in Kotlin: is the same as: Because Kotlin knows what Now, if Returns a Reusing StreamsIn Kotlin, it depends on the type of collection whether it can be consumed more than once. A And in Java to get the same behavior: Therefore in Kotlin the provider of the data decides if it can reset back and provide a new iterator or not. But if you want to intentionally constrain a See also:
|
Read Article
https://develop.horje.com/learn/1434/reference