Input/Output and Streams in Java
Alex Liu
Software Development Engineer
Data transformation with streams
List to Set)ArrayList, LinkedList)HashSet, TreeSet)Keys/Values as Streams) (e.g., HashMap, TreeMap)Arrays.stream())Example ArrayList named namesto use for the rest of the video
names: ["Alice", "Bob", "Charlie", "David"]
Set, Collectors and Streamimport java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream;// Convert the `ArrayList` we created earlier `names` to a `Stream` Stream<String> namesStream = names.stream();
[Alice, Bob, Charlie, David]// Use `.map()` to convert each element to upper case
Stream<String> upperCaseNameStream = namesStream.map(
        name -> name.toUpperCase())
[ALICE, BOB, CHARLIE, DAVID]// use `.collect()` and `Collectors.toSet()` to store the transformed stream data in a `set`
upperCaseNameStream.collect(Collectors.toSet())
{CHARLIE, ALICE, BOB, DAVID}
The .reduce() method
Use .reduce() takes two inputs
0(x, y) -> x + y means:x represents the accumulated totaly represents the current element being processedy to x, continuing until all elements are processedArrayList names to a Stream object.Stream<String> stream = names.stream();
.map() to convert name to its length and use .reduce() to add them up togetherstream
  .map(name -> name.length())
  .reduce(0, (sum, length) -> sum + length);
20Input/Output and Streams in Java