Skip to content

Instantly share code, notes, and snippets.

@bltavares
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save bltavares/91d69e1d804eec8daf13 to your computer and use it in GitHub Desktop.

Select an option

Save bltavares/91d69e1d804eec8daf13 to your computer and use it in GitHub Desktop.
[KeySet] I should only show twice because limit will be processed before this filter
[KeySet] I should only show twice because limit will be processed before this filter
A
B
[KeySet] I should only show twice because limit will be processed before this filter
[KeySet] I should only show twice because limit will be processed before this filter
A
[KeySet] I should show 4 times without the previous limit
[KeySet] I should show 4 times without the previous limit
[KeySet] I should show 4 times without the previous limit
[KeySet] I should show 4 times without the previous limit
A
B
[List] I should only show twice because limit will be processed before this filter
[List] I should only show twice because limit will be processed before this filter
A
[List] I should only show twice because limit will be processed before this filter
[List] I should only show twice because limit will be processed before this filter
A
[List] I should show 4 times without the previous limit
[List] I should show 4 times without the previous limit
[List] I should show 4 times without the previous limit
[List] I should show 4 times without the previous limit
A
B
import java.util.HashMap;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> info = new HashMap<>();
info.put("C", 3);
info.put("B", 2);
info.put("D", 4);
info.put("A", 1);
info.keySet()
.stream()
.limit(2)
.filter((x) -> {
System.out.println("[KeySet] I should only show twice because limit will be processed before this filter");
return x.equals("A") || x.equals("B");
})
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(toList())
.forEach(System.out::println);
info.keySet()
.parallelStream() // It should work on parallel streams as well, but you might miss ordering
.limit(2)
.filter((x) -> {
System.out.println("[KeySet] I should only show twice because limit will be processed before this filter");
return x.equals("A") || x.equals("B");
})
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(toList())
.forEach(System.out::println);
info.keySet()
.stream()
.filter((x) -> {
System.out.println("[KeySet] I should show 4 times without the previous limit");
return x.equals("A") || x.equals("B");
})
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(toList())
.forEach(System.out::println);
List<String> listInfo = asList("C", "A", "D", "B");
listInfo.stream()
.limit(2) // you will miss the ordering
.filter((x) -> {
System.out.println("[List] I should only show twice because limit will be processed before this filter");
return x.equals("A") || x.equals("B");
})
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(toList())
.forEach(System.out::println);
listInfo.parallelStream() // It should work on parallel streams as well and you will miss the ordering
.limit(2)
.filter((x) -> {
System.out.println("[List] I should only show twice because limit will be processed before this filter");
return x.equals("A") || x.equals("B");
})
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(toList())
.forEach(System.out::println);
listInfo.stream()
.filter((x) -> {
System.out.println("[List] I should show 4 times without the previous limit");
return x.equals("A") || x.equals("B");
})
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(toList())
.forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment