Java 8 — Stream

List<String> list =
Arrays.asList(“ali”, “veli”, “ahmet”,”mehmet”);
list
.stream()
.filter(s -> s.startsWith(“a”))
.map(String::toUpperCase)
.forEach(System.out::println);
Output:
ALI
AHMET
Output:
ALI
AHMET

Stream.of(“ali”, “veli”, “ahmet”,”mehmet””)
.filter(s -> s.startsWith(“a”))
.map(String::toUpperCase)
.forEach(System.out::println);
Output:
ALI
AHMET
for (int i = 0 ; i < 10 ; i++){
System.out.print(i);
}
Output:
0123456789
IntStream.range(0,10).forEach(System.out::print);Output:
0123456789
System.out.println(IntStream.range(0,10).max().getAsInt());
System.out.println(IntStream.range(0,10).min().getAsInt());
System.out.println(IntStream.range(0,10).sum());
System.out.println(IntStream.range(0,10).average().getAsDouble());
Output:
9 //max
0 //min
45 //sum
4.5 //average
IntStream.range(0,3)
.mapToObj(i -> “ali”+i).
forEach(System.out::println);
Output:
ali0
ali1
ali2
Arrays.asList(“ali”, “veli”, “ahmet”)
.parallelStream()
.filter(s -> {
System.out.format(“Filter: %s [%s]\n”,
s, Thread.currentThread().getName());
return true;
})
.map(s -> {
System.out.format(“Map: %s [%s]\n”,
s, Thread.currentThread().getName());
return s.toUpperCase();
})
.forEach(s -> System.out.format(“ForEach: %s [%s]\n”,
s, Thread.currentThread().getName()));
Output:
Filter: ali [ForkJoinPool.commonPool-worker-1]
Map: ali [ForkJoinPool.commonPool-worker-1]
ForEach: ALI [ForkJoinPool.commonPool-worker-1]
Filter: veli [main]
Map: veli [main]
Filter: ahmet [ForkJoinPool.commonPool-worker-2]
Map: ahmet [ForkJoinPool.commonPool-worker-2]
ForEach: AHMET [ForkJoinPool.commonPool-worker-2]
ForEach: VELI [main]

--

--

Software Engineer www.arifpehlivan.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store