collection vs stream
@@ -2,6 +2,10 @@ package com.baeldung.streams.streamvscollection;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -26,6 +30,14 @@ public class StreamVsCollectionExample {
return userNames().collect(Collectors.toList());
}
+ public static Set<String> userNameSet() {
+ return userNames().collect(Collectors.toSet());
+ }
+
+ public static Map<String, String> userNameMap() {
+ return userNames().collect(Collectors.toMap(u1 -> u1.toString(), u1 -> u1.toString()));
+ }
+
public static Stream<String> filterUserNames() {
return userNames().filter(i -> i.length() >= 4);
}
@@ -46,4 +58,38 @@ public class StreamVsCollectionExample {
stream.forEach(System.out::println);
}
+ public static void modifyList() {
+ userNameSource.remove(2);
+ }
+
+ public static Map<String, String> modifyMap() {
+ Map<String, String> userNameMap = userNameMap();
+ userNameMap.put("bob", "bob");
+ userNameMap.remove("alfred");
+
+ return userNameMap;
+ }
+
+ public static void tryStreamTraversal() {
+ Stream<String> userNameStream = userNames();
+ userNameStream.forEach(System.out::println);
+
+ try {
+ userNameStream.forEach(System.out::println);
+ } catch(IllegalStateException e) {
+ System.out.println("stream has already been operated upon or closed");
+ }
+ }
+
+ public static void main(String[] args) {
+ System.out.println(userNameMap());
+ System.out.println(modifyMap());
+ tryStreamTraversal();
+
+ Set<String> set = userNames().collect(Collectors.toCollection(TreeSet::new));
+ set.forEach(val -> System.out.println(val));
+
+ }
+
+
}
