0% found this document useful (0 votes)
129 views

Streams API: Java SE

The document discusses Java Streams API. It defines a Stream as a sequence of elements supporting sequential and parallel aggregate operations. Streams are lazy collections that allow chaining operations such as filtering, mapping, matching, reducing, and collecting. The key benefits of Streams include internal iteration, support for sequential and parallel processing, and intermediate operations that enable pipelines of transformations. Common stream operations include creation from collections, files and ranges, intermediate operations like filter, map and flatMap, and terminal operations like forEach, count and reduce.

Uploaded by

sissthwae oo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Streams API: Java SE

The document discusses Java Streams API. It defines a Stream as a sequence of elements supporting sequential and parallel aggregate operations. Streams are lazy collections that allow chaining operations such as filtering, mapping, matching, reducing, and collecting. The key benefits of Streams include internal iteration, support for sequential and parallel processing, and intermediate operations that enable pipelines of transformations. Common stream operations include creation from collections, files and ranges, intermediate operations like filter, map and flatMap, and terminal operations like forEach, count and reduce.

Uploaded by

sissthwae oo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Streams API

Java SE
Contents

• Overview

• What is a Streams?

• Stream Operations

• Sequential & Parallel Streams


Overview
• Java SE 8 မတiuင&မ'(က၊ collection တစ&ခuအတ.င:& /i data မ01:အ1:
parallel processing 2ပuလuပ&လiueသ1 အခ8၊ for ဒ8မ'မဟuတ& while
iteration အ1:အသu;:2ပuက1 2ပင&ပမ' Control လuပ&<ကရ၏

• External Iteration လuပ&2ခင&?သည& parallel processing အ1?


eဆ1င&B.က&ရန& ခက&ခDeစပ8သည&

• Stream API သည& Internal Iteration အ1?ပ;Eပiu?eပ?Fပ(?၊


sequential eက1 parallel processing ပ8 လuပ&eဆ1င&eစGiuင&Fပ(?
filtering G'င&E mapping eဆ1င&B.က&ခ0က&တiuကiuပ8 အလ.ယ&တကJ
လuပ&eဆ1င&eစGiuင&ပ8သည&
Before Streams
public class T1BefStream {

public static void main(String[] args) {


List<Integer> list = Arrays.asList(1,2,4,5,6,7,8,9,10);
int sum = sum(list);
System.out.println(sum);
}

static int sum(List<Integer> list) {


int sum = 0;
Iterator<Integer> itr = list.iterator();
while(itr.hasNext()) {
sum += itr.next();
}
return sum;
}
}
Using Streams
public class T2UsingStream {

public static void main(String[] args) {


List<Integer> list = Arrays.asList(1,2,4,5,6,7,8,9,10);
int sum = sum(list);
System.out.println(sum);
}

static int sum(List<Integer> list) {


return list.stream().mapToInt(i -> i).sum();
}
}
What is a Stream?
• Stream သည& အသu;?2ပuရန&လiuအပ&လ1သK&Eအခ8တ.င& ထuပ&လuပ&eပ?Giuင&eသ1 Data Structure တစ&မ0iu?2ဖစ&
ပ8သည&

• Stream မ01?အ1? Data အ1?သiမ&?ဆည&?ရန& အသu;?မ2ပuGiuင&ပD၊ မJလ Data Source မ01?မ' Data မ01?အ1?
Pipe Line ကDEသiu တစ&ခu2ခင&? စ(?ဆင&?eစက1၊ သတ&မ'တ&ထ1?eသ1 လuပ&eဆ1င&ခ0က&မ01အ1? လuပ&eဆ1င&eစ
Giuင&ပ8သည&

• တဖန& Stream ၏ Internal Iteration သည& Stream Operation ၏ Lazy Seeking အ1? ပ;Eပiu?eပ?ပ8သည&

• Stream မ01?သည& သu;?စ.Dနiuင&eသ1 Object မ01?2ဖစ&Fပ(?၊ Stream အတ.င&?/i Data မ01?အ1? တစ&Nကiမ& သu;?စ.DFပ(?
ပ8က မJရင&? Stream Object အ1? 2ပန&လည& အသu;?2ပuGiuင&eတ1Eမည& မဟuတ&ပ8

• Stream တစ&ခuတ.င& creation၊ intermediate process G'င&E terminal process တiuပ8ဝင&<ကFပ(? ပiuက&လiuင&?
တစ&ခuကDEသiu ခ0iတ&ဆက&လPက&/iပ8သည&

• Stream အတ.င&?/i Data မ01?အ1? အသu;?2ပuရ1တ.င& Sequential eက1 Parallel လuပ&eဆ1င&မQEကiuပ8


လuပ&eဆ1င&Giuင&ရန& ပ;Eပiu?ထ1?ပ8သည&
Types of Streams

• Stream<T>

• IntStream

• LongStream

• DoubleStream
Stream Operation
• Stream Creation Operations မ01?သည& Stream မ01?အ1? စတင& 2ဖစ&eပR
eစeသ1 Operation မ01?2ဖစ&<ကFပ(? ၎င&? method မ01?၏ return type သည&
Stream အမ0iu? အစ1?မ01? 2ဖစ&<က၏

• Intermediate Operations မ01?သည& Pipe Line အတ.င&? လက&ဆင&Eကမ&?


eဆ1င&B.က&ရeသ1 Operation မ01? 2ဖစ&<ကFပ(?၊ ၎င&? Method မ01?၏ Return
Type သည& Stream Type တစ&မ0iu?မ0iu? 2ဖစ&Fပ(? eန1က&ဆက&တ.D eဆ1င&B.က&
ခ0က&မ01?ကiuလည&? လuပ&eဆ1င&eစGiuင&မည& 2ဖစ&သည&

• Terminal Operations မ01?သည& Stream မ01?အ1? အဆu;?သတ& eစeသ1


လuပ&eဆ1င& ခ0က&မ01?2ဖစ&<ကသည&။ ၎င&? Method မ01?၏ Return Type
မ01?သည& Stream မဟuတ&eသ1 အ2ခ1?eသ1 Type တစ&မ0iu?မ0iu? ဒ8မ'မဟuတ&
void 2ဖစ&ပ8မည&
Stream Creation

• Get stream from Arrays or collection

• From File

• Giving Range of Numbers and specific elements

• Generate and Iterate


From Arrays or Collections
// from array
int [] array = {1,2,4,5,6,7,8};
Arrays.stream(array).forEach(System.out::println);

// from collection
Collection<String> col = Arrays.asList("Hello", "Java",
"I am a developer");
col.stream().forEach(System.out::println);
From a file

public static void main(String[] args) throws IOException {

Files.lines(Paths.get("T2FromFiles.txt"))
.forEach(System.out::println);

}
Ranging
// IntStream
IntStream.range(1, 10).forEach(System.out::println);
IntStream.rangeClosed(1, 10).forEach(System.out::println);

// LongStream
LongStream.range(1, 10)
.forEach(System.out::println);

// DoubleStream
DoubleStream.of(19.001, 12.201,11,111)
.forEach(System.out::println);
Iterate & Generate
// double stream generate
DoubleStream.generate(Math::random).limit(20)
.forEach(System.out::println);

// int stream iterate


IntStream.iterate(0, a -> a + 1).limit(10)
.forEach(System.out::println);

// Stream iterate
Stream.iterate("", a -> a + "a").limit(10)
.forEach(System.out::println);
Intermediate Operations
Method Description

filter Stream အတ#င%&'i Data မ*+&အ+& စစ%ထuတ%/iuင%သည%

map အ2ခ+&eသ+ Data ပu6စ6တစ%မ*iu&သiue2ပ+င%&လ:/iuင%သည%

flatMap Collection Stream မ*+&အ+& Element Stream အ2ဖစ%e2ပ+င%&eပ&သည%

distinct တ<ည=eသ+ Data မ*+&အ+& တစ%ခuတည%&2ဖစ%eအ+င%2ပuလuပ%/iuင%သည%

sorted Data မ*+&အ+& အစ=အစ>%တက*2ဖစ%eအ+င% စ=စ>%eပ&/iuင%သည%

limit Parameter အထi Data မ*+&အ+& က/%@သတ% eပ&/iuင%သည%

skip Parameter မeရ+က%ခင% Data မ*+&အ+& Skip လuပ%eပ&မည%2ဖစ%သည%


Filter

public static void main(String[] args) {


T0Common.getUser().stream()
.filter(a -> a.getAge() > 20)
.map(a -> a.getName()).forEach(System.out::println);
}
Map
public static void main(String[] args) {
// mapping User to String
T0Common.getUser().stream()
.map(a -> a.getName())
.forEach(System.out::println);

// mapping User to int


int sum = T0Common.getUser().stream()
.mapToInt(a -> a.getAge()).sum();

System.out.println(sum);
}
FlatMap
public static void main(String[] args) {
// flat mapping list stream
T0Common.listStream()
.flatMap(l -> l.stream().map(a -> a.getName()))
.sorted()
.forEach(System.out::println);
}
Others
IntStream
.of(1,2,3,4,5,6,4,3,2,5,6,7,8,9,4,4,3,2,1)
.distinct()
.sorted()
.skip(3)
.limit(5)
.forEach(System.out::println);
Terminal Operation
Method Description

forEach Stream အတ#င%&'i Data မ*+&အ+& တစ%ခuစ=အသu6:2ပu/iuင%ပBသည%

forEachOrdered Parallel Stream အတ#င%&'i Data မ*+&အ+&အစ=အစ>%အလiuက% တစ%ခuစ=


အသu6&2ပu/iuင%ပBသည%

reduce Stream အတ#င:% 'i Data မ*+&အ+& eန+က%ဆu6&တစ%ခu အထi eလE+@ခ* eဆ+င%F#က%
eပ&/iuင%ပBသည%

collect Stream အတ#င%&မG Data မ*+&အ+& Container တစ%ခu2ဖင%@ စuစည%&/iuင%ပBသည%

allMatch Stream အတ#င%&'i Data မ*+&အ+&လu6&သည% Parameter Predicate /Gင%@ကiuက%


ည=မH@'iမ'iကiu စစ%eဆ&eပ&/iuင%ပBသည%

anyMatch Stream အတ#င%&'i Data တစ%ခuခuသည% Parameter Predicate /Gင%@ကiuက%ည=မH@


'i မ'iကiu စစ%eဆ&eပ&/iuင%ပBသည%
Terminal Operation
Method Description

nonMatch Stream အတ#င%&'i Data မ*+&သည% Parameter Predicate /Gင%@ကiuက%ည=


မH@ မ'ieIက+င%&ကiu စစ%eဆ&eပ&/iuင%ပBသည%

findFirst Stream Object မGစ=&ဆင%&လ+eသ+ Data မ*+&၏ ပထမဉ=&ဆu6& Data


အ+& ရယ<eပ&/iuင%ပBသည%

findAny Stream အတ#င%&'i Element တစ%ခuခu Optional Object 2ဖင%@ 2ပန%eပ&


မည% 2ဖစ%သည%

min / max အLက=&ဆu6& သiuမဟuတ% အငယ%ဆu6& Element ကiu '+eဖ#eပ&/iuင%မည% 2ဖစ%သည%

count လက%'i Stream အတ#င%&'i Element အeရအတ#က%အ+& '+eဖ#eပ&/iuင%


ပBသည%

toArray Stream အတ#င%&'i Element မ*+&အ+& Array ပu6စ62ဖင%@ စuစည%&eပ&မည%


2ဖစ%ပBသည%
ForEach Vs ForEachOrdered
IntStream
.range(1, 10000)
.parallel()
.forEachOrdered(System.out::println);

အကယ& Parallel Stream အ1: အသu;:2ပuပ8က forEach 2ဖင&E iterate လuပ&လPင&


အစ(အစU&အတiuင&: eဆ1င&B.က&eပ:Giuင&မ မဟuတ&၊ forEachOrdered ကiuသu;:ပ8မ'
အစ(အစU& အတiuင&: eဆ1င&B.က&eပ:Giuင&
Reduce

Stream<User> stream = T0Common.getUser().stream();


int sum = stream.map(a -> a.getAge())
.reduce(0, (a, b) -> a + b);
System.out.println(sum);
Collect

List<String> names = T0Common.getUser()


.stream()
.map(a -> a.getName())
.collect(Collectors.toList());

System.out.println(names);
AllMatch

boolean result = T0Common.getUser()


.stream()
.allMatch(a -> a.getAge() > 10);

System.out.println(result);

You might also like