forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#10551 from SmartyAnsh/BAEL-4790_Collection_…
…vs_Stream BAEL-4790 - Stream vs Collection
- Loading branch information
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
...ms-3/src/main/java/com/baeldung/streams/streamvscollection/StreamVsCollectionExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
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; | ||
|
||
public class StreamVsCollectionExample { | ||
|
||
static ArrayList<String> userNameSource = new ArrayList<>(); | ||
|
||
static { | ||
userNameSource.add("john"); | ||
userNameSource.add("smith"); | ||
userNameSource.add("tom"); | ||
userNameSource.add("rob"); | ||
userNameSource.add("charlie"); | ||
userNameSource.add("alfred"); | ||
} | ||
|
||
public static Stream<String> userNames() { | ||
return userNameSource.stream(); | ||
} | ||
|
||
public static List<String> userNameList() { | ||
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); | ||
} | ||
|
||
public static Stream<String> sortUserNames() { | ||
return userNames().sorted(); | ||
} | ||
|
||
public static Stream<String> limitUserNames() { | ||
return userNames().limit(3); | ||
} | ||
|
||
public static Stream<String> sortFilterLimitUserNames() { | ||
return filterUserNames().sorted().limit(3); | ||
} | ||
|
||
public static void printStream(Stream<String> stream) { | ||
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)); | ||
|
||
} | ||
|
||
|
||
} |