ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Stream ကို စက်ရုံက conveyor belt တစ်ခုလို မြင်ကြည့်ပါ။ Collection (source) ကနေ item တွေ belt ပေါ် တက်လာပြီး station တွေကို ဖြတ်သွားပါတယ် — တစ်ခုက မလိုတာကို ဖယ် (filter)၊ တစ်ခုက ပုံစံပြောင်း (map)၊ နောက်ဆုံး station က box ထဲ ထည့် (collect) ပါတယ်။ Lambda ဆိုတာ station တစ်ခုစီမှာ ဘာလုပ်ရမလဲဆိုတဲ့ ညွှန်ကြားချက် တိုတိုလေးပါ၊ name -> name.length() >= 5 လိုမျိုးပါ။
Belt က box ထုပ်တဲ့ station (terminal operation) ကို တပ်ပြီးမှ စ လည်ပါတယ်။ Station တွေ တပ်ထားရုံနဲ့ ဘာမှ မဖြစ်သေးပါဘူး။ Belt တစ်ခုကို တစ်ကြိမ်ပဲ run လို့ ရပြီး မူရင်း collection ကိုတော့ မပြောင်းလဲပါဘူး။
Problem → Why → Solution
ပြဿနာ: loop တွေထဲမှာ ရည်ရွယ်ချက် ပျောက်နေတာ
"စာလုံး ၅ လုံးနဲ့ အထက် ရှိတဲ့ နာမည်တွေကို အကြီးစာလုံးနဲ့ ယူမယ်" ဆိုတဲ့ စကားတစ်ကြောင်းကို ရိုးရိုး loop နဲ့ ရေးရင် result list ဖန်တီးတာ၊ loop ပတ်တာ၊ if စစ်တာ၊ add လုပ်တာ စတဲ့ "ဘယ်လိုလုပ်မလဲ" အသေးစိတ်တွေ ရောနေပြီး "ဘာလိုချင်လဲ" ဆိုတာ ပျောက်သွားပါတယ်။ Condition နှစ်ခု သုံးခု၊ sort နဲ့ group ထပ်ပါလာရင် nested loop တွေနဲ့ temp variable တွေ များလာပါတယ်။
Stream ကို စသုံးတဲ့သူတွေလည်း ထောင်ချောက် တစ်ခုမှာ ကျတတ်ပါတယ် — stream variable တစ်ခုကို နှစ်ခါ သုံးလိုက်တော့ IllegalStateException ဖြစ်ပါတယ်။
import java.util.List;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Stream<String> names = List.of("ada", "grace", "linus").stream();
long longNames = names.filter(n -> n.length() >= 5).count();
long shortNames = names.filter(n -> n.length() < 5).count(); // IllegalStateException
System.out.println(longNames + " " + shortNames);
}
}ဘာကြောင့် stream ကို ပြန်မသုံးနိုင်တာလဲ
Stream က data ကို သိမ်းထားတဲ့ collection မဟုတ်ဘဲ source ပေါ်ကို တစ်ကြိမ် ဖြတ်သွားတဲ့ processing pipeline ပါ။ count() လို terminal operation က pipeline ကို run ပြီး ပိတ်လိုက်လို့ ဒုတိယအကြိမ် သုံးရင် "stream has already been operated upon or closed" error ပေးပါတယ်။ ဖြေရှင်းချက်က တွက်ချက်မှု တစ်ခုစီအတွက် names.stream() ကို အသစ် ခေါ်တာပါ၊ list ကတော့ ပြောင်းလဲမှု မရှိလို့ အကြိမ်ကြိမ် stream လုပ်လို့ ရပါတယ်။
Core concept: lambda တွေနဲ့ ဆောက်ထားတဲ့ lazy pipeline
Lambda ဆိုတာ နာမည်မရှိတဲ့ function တစ်ခုပါ — (parameters) -> expression ဒါမှမဟုတ် (parameters) -> { statements } ပုံစံ ရေးပါတယ်။ Java မှာ function တွေက သီးခြား type မဟုတ်လို့ lambda တစ်ခုကို abstract method တစ်ခုတည်းရှိတဲ့ interface (functional interface) ရဲ့ implementation အဖြစ် သုံးပါတယ်။ java.util.function package ထဲက Predicate<T> (T → boolean)၊ Function<T, R> (T → R)၊ Consumer<T> (T → ဘာမှမပြန်)၊ Supplier<T> (() → T) တို့က အသုံးအများဆုံးပါ။ Lambda တစ်ခုက ဘယ် interface ဖြစ်မလဲဆိုတာကို compiler က context (ဥပမာ filter() က Predicate လိုချင်တာ) ကနေ သိပါတယ်။
Lambda က ပြင်ပ method တစ်ခုကို ခေါ်ရုံပဲ လုပ်ရင် method reference နဲ့ ပိုတိုအောင် ရေးလို့ ရပါတယ် — s -> s.toUpperCase() အစား String::toUpperCase၊ x -> System.out.println(x) အစား System.out::println ပါ။ Method reference မှာ ပုံစံ လေးမျိုး ရှိပါတယ် — static method (Integer::parseInt)၊ object တစ်ခုပေါ်က method (System.out::println)၊ parameter ပေါ်က instance method (String::length) နဲ့ constructor (ArrayList::new) တို့ပါ။ Lambda ထဲကနေ အပြင်က local variable ကို သုံးလို့ ရပေမယ့် အဲဒီ variable က final ဒါမှမဟုတ် effectively final (တစ်ခါ assign ပြီးရင် ဘယ်တော့မှ မပြောင်း) ဖြစ်ရပါမယ်။
Stream pipeline မှာ အပိုင်း သုံးပိုင်း ရှိပါတယ်။ Source က list.stream() လို collection ကနေ စပါတယ်။ Intermediate operation တွေ (filter, map, sorted, distinct, limit) က stream အသစ်ကို ပြန်ပေးပြီး lazy ဖြစ်ပါတယ် — သူတို့ကို ခေါ်တာက လုပ်ရမယ့် အလုပ်ကို မှတ်ထားရုံပါပဲ။ Terminal operation (toList, collect, count, forEach, findFirst, sum) ကို ခေါ်မှသာ data က pipeline ထဲ စီးဆင်းပါတယ်။
Data က operation တစ်ခုချင်းစီ အလွှာလိုက် မဟုတ်ဘဲ item တစ်ခုချင်း pipeline တစ်လျှောက်လုံး ဖြတ်သွားပါတယ်။ ဒါကြောင့် findFirst() လို short-circuit operation က လိုချင်တာ တွေ့တာနဲ့ ကျန်တဲ့ item တွေကို မစစ်တော့ပါဘူး။ Java 16 ကစပြီး ပါလာတဲ့ toList() က unmodifiable list ကို ပြန်ပေးပြီး Stream က source collection ကို ဘယ်တော့မှ မပြောင်းပါဘူး။
- Source: list.stream()
- Intermediate: filter/map ကို မှတ်ထား
- Terminal: toList/count က run စေ
- Item တစ်ခုချင်း pipeline ကို ဖြတ်
- Stream ပိတ်သွား၊ source မပြောင်း
Terminal operation မပါတဲ့ pipeline က ဘာအလုပ်မှ မလုပ်ပါဘူး။
အထွေထွေ ပုံစံ
List<Result> results = source.stream() // source
.filter(item -> condition(item)) // intermediate (lazy)
.map(Type::convert) // intermediate, method reference
.toList(); // terminal: runs the pipeline
Predicate<T> test = t -> ...; // T -> boolean
Function<T, R> fn = t -> ...; // T -> R
Map<K, List<T>> groups = source.stream()
.collect(Collectors.groupingBy(t -> key(t)));Source ကနေ stream ယူပြီး intermediate operation တွေကို ဆက်တိုက် ချိတ်၊ terminal operation တစ်ခုနဲ့ အဆုံးသတ်ပါတယ်။ Lambda တွေက Predicate, Function လို functional interface type တွေ ဖြစ်ပြီး Collectors.groupingBy က key အလိုက် Map အဖြစ် စုပေးပါတယ်။
Example: နာမည်ရှည်တွေကို အကြီးစာလုံးနဲ့ ယူခြင်း
ဒီ pipeline က နာမည်သုံးခုထဲက စာလုံး ၅ လုံးနဲ့ အထက် ရှိတာတွေကို ရွေးပြီး အကြီးစာလုံး ပြောင်းကာ list အသစ်ထဲ စုပါတယ်။ Code ကို အပေါ်ကနေ အောက် ဖတ်လိုက်တာနဲ့ "filter ပြီး map ပြီး list လုပ်" ဆိုတဲ့ ရည်ရွယ်ချက်ကို တန်းမြင်ရပါတယ်။
import java.util.List;
class Main {
public static void main(String[] args) {
List<String> names = List.of("ada", "grace", "linus");
List<String> result = names.stream()
.filter(name -> name.length() >= 5)
.map(String::toUpperCase)
.toList();
System.out.println(result);
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 5
List<String> names = List.of("ada", "grace", "linus");Source collection ပါ။ List.of() က immutable list ဖြစ်ပေမယ့် stream က source ကို မပြင်လို့ ပြဿနာ မရှိပါဘူး။
- Line 6
List<String> result = names.stream()stream() က list ပေါ်မှာ pipeline အသစ်ကို စတင်ပါတယ်။ ဒီအချိန်မှာ item တစ်ခုမှ မဖတ်ရသေးပါဘူး။
- Line 7
.filter(name -> name.length() >= 5)ဒီ lambda က Predicate<String> ဖြစ်ပြီး true ပြန်တဲ့ item တွေပဲ ဆက်သွားပါတယ်။ "ada" (၃ လုံး) ဒီမှာ ကျန်ခဲ့ပါတယ်။
- Line 8
.map(String::toUpperCase)Method reference က name -> name.toUpperCase() နဲ့ အတူတူပါ။ Function<String, String> အဖြစ် item တစ်ခုစီကို အသစ်တစ်ခုအဖြစ် ပြောင်းပါတယ်။
- Line 9
.toList();Terminal operation ဖြစ်လို့ ဒီမှာမှ pipeline တကယ် run ပြီး [GRACE, LINUS] ကို unmodifiable list အဖြစ် ပြန်ပေးပါတယ်။ names list ကတော့ မပြောင်းဘဲ ရှိနေပါတယ်။
[GRACE, LINUS]Functional interface နဲ့ method reference
Lambda တွေကို variable ထဲ သိမ်းလို့လည်း ရပါတယ်၊ ဒါမှ functional interface type တွေကို တိုက်ရိုက် မြင်ရပါတယ်။ Main::isLong လို static method reference ကလည်း Predicate နဲ့ ကိုက်ပါတယ်။ minLength ကို lambda ထဲမှာ သုံးထားပြီး ဘယ်တော့မှ ပြန် assign မလုပ်လို့ effectively final ဖြစ်ပါတယ် — minLength++ ထည့်လိုက်ရင် compile error တက်ပါလိမ့်မယ်။
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class Main {
static boolean isLong(String s) {
return s.length() >= 5;
}
public static void main(String[] args) {
Predicate<String> longName = name -> name.length() >= 5; // T -> boolean
Function<String, Integer> length = String::length; // T -> R
Supplier<String> greeting = () -> "hello"; // () -> T
Predicate<String> sameRule = Main::isLong; // static method ref
System.out.println(longName.test("grace") + " " + sameRule.test("ada"));
System.out.println(length.apply("linus"));
System.out.println(greeting.get());
int minLength = 4; // effectively final: never reassigned
List<String> kept = List.of("ada", "grace", "bob", "linus").stream()
.filter(n -> n.length() >= minLength)
.toList();
System.out.println(kept);
}
}Laziness ကို မျက်မြင် ကြည့်ခြင်း
အောက်က program မှာ filter နဲ့ map ထဲမှာ print ထည့်ထားပါတယ်။ Pipeline တည်ဆောက်ပြီးချိန်မှာ ဘာမှ print မထွက်ဘဲ findFirst() ခေါ်မှ စ run တာ၊ "grace" တွေ့တာနဲ့ "linus" နဲ့ "barbara" ကို လုံးဝ မစစ်တော့တာကို output မှာ မြင်ရပါတယ်။ နောက်ဆုံးမှာ အဲဒီ stream ကို ပြန်သုံးတော့ IllegalStateException ဖြစ်ပါတယ်။
import java.util.List;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
List<String> names = List.of("ada", "grace", "linus", "barbara");
Stream<String> pipeline = names.stream()
.filter(n -> {
System.out.println("filter " + n);
return n.length() >= 5;
})
.map(n -> {
System.out.println("map " + n);
return n.toUpperCase();
});
System.out.println("pipeline built, nothing ran yet");
String first = pipeline.findFirst().orElse("none");
System.out.println("first = " + first);
try {
pipeline.count(); // a stream can only be consumed once
} catch (IllegalStateException e) {
System.out.println("reuse: " + e.getMessage());
}
}
}Output က pipeline built, nothing ran yet → filter ada → filter grace → map grace → first = GRACE → reuse: stream has already been operated upon or closed အစဉ်အတိုင်း ထွက်ပါတယ်။ Filter အားလုံး အရင်ပြီးမှ map စတာ မဟုတ်ဘဲ "grace" တစ်ခုတည်းက filter ကနေ map ဆီ တန်းသွားတာကို သတိထားကြည့်ပါ။
Collectors: groupingBy, joining နဲ့ primitive stream
collect() က Collectors ထဲက recipe တွေနဲ့ ရလဒ်ကို စုပါတယ်။ groupingBy() က key function အလိုက် Map ထဲ ခွဲပြီး TreeMap::new ပေးရင် key တွေ sort လုပ်ထားတဲ့ map ရပါတယ်။ joining() က string တွေကို separator နဲ့ ဆက်ပေးပြီး mapToInt() က boxing မလုပ်ဘဲ sum() လို ဂဏန်း operation တွေ ပေးတဲ့ IntStream ကို ပြန်ပေးပါတယ်။
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> words = List.of("apple", "avocado", "banana", "blueberry", "cherry");
Map<Character, List<String>> byLetter = words.stream()
.collect(Collectors.groupingBy(
w -> w.charAt(0), TreeMap::new, Collectors.toList()));
System.out.println(byLetter);
int totalLetters = words.stream().mapToInt(String::length).sum();
System.out.println("Total letters: " + totalLetters);
String joined = words.stream()
.sorted()
.limit(3)
.collect(Collectors.joining(", ", "[", "]"));
System.out.println(joined);
}
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Service code တွေမှာ database ကနေ ရလာတဲ့ entity list ကို API response DTO list အဖြစ် ပြောင်းတာ (map)၊ active ဖြစ်တဲ့ user တွေကိုပဲ ရွေးတာ (filter)၊ status အလိုက် order တွေကို အုပ်စုဖွဲ့ပြီး စုစုပေါင်း တွက်တာ (groupingBy + summingInt) တွေကို stream နဲ့ ရေးကြပါတယ်။ Spring ရဲ့ event listener, comparator (Comparator.comparing(User::getName)), Optional ရဲ့ map/orElse, CompletableFuture ရဲ့ thenApply တို့ကလည်း lambda နဲ့ method reference ကို လက်ခံလို့ ဒီ syntax ကို နေရာတိုင်းမှာ တွေ့ရပါတယ်။
Team တွေက stream ကို ဖတ်ရလွယ်ဖို့ သုံးပြီး ရှုပ်ထွေးလာရင် step တွေကို နာမည်ပါတဲ့ method အဖြစ် ခွဲထုတ်ပါတယ် — .filter(this::isEligible) က lambda ထဲမှာ condition ငါးခု စုထားတာထက် ရှင်းပါတယ်။ parallelStream() ကိုတော့ data များတယ်ဆိုတိုင်း မသုံးပါဘူး။ Common ForkJoinPool ကို မျှသုံးရလို့ web server မှာ တခြား request တွေကို နှေးစေနိုင်ပြီး thread overhead ကြောင့် data နည်းရင် ပိုနှေးတတ်လို့ benchmark နဲ့ တိုင်းတာမှသာ ရွေးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Stream variable ကို ပြန်သုံးတာ
Stream ကို variable ထဲ သိမ်းပြီး terminal operation နှစ်ခါ ခေါ်ရင် ဒုတိယအကြိမ်မှာ IllegalStateException ဖြစ်ပါတယ်။ Stream ကို variable ထဲ မသိမ်းဘဲ တွက်ချက်မှု တစ်ခုစီအတွက် list.stream() ကို အသစ် ခေါ်ပါ၊ ဒါမှမဟုတ် Supplier<Stream<T>> ကို သုံးပါ။
2. forEach ထဲကနေ အပြင်က list ကို ဖြည့်တာ
stream().filter(...).forEach(x -> results.add(x)) က အလုပ်ဖြစ်ပေမယ့် shared state ကို ပြင်နေတာပါ။ parallel ပြောင်းလိုက်ရင် ArrayList က thread-safe မဟုတ်လို့ item ပျောက်တာ ဒါမှမဟုတ် exception ဖြစ်တာ ကြုံရပါတယ်။ ရလဒ်ကို .toList() ဒါမှမဟုတ် collect() နဲ့ ပြန်ယူပါ။
3. Lambda ထဲမှာ local variable ကို ပြောင်းဖို့ ကြိုးစားတာ
int count = 0; list.forEach(x -> count++); က "local variables referenced from a lambda expression must be final or effectively final" compile error ပေးပါတယ်။ ရေတွက်ချင်ရင် .count() ကို၊ ပေါင်းချင်ရင် mapToInt(...).sum() ကို သုံးပါ။
4. Terminal operation ကို မေ့တာ
list.stream().map(this::save); လို့ ရေးရင် lazy ဖြစ်လို့ save() ကို တစ်ခါမှ မခေါ်ပါဘူး၊ error လည်း မပေါ်ပါဘူး။ Side effect လိုရင် ရိုးရိုး for loop ကို သုံးပါ။
Mini challenge
Mini challenge: order report
Order record list တစ်ခုကနေ stream တွေ သုံးပြီး customer အလိုက် ငွေချေပြီးသား စုစုပေါင်း၊ မချေရသေးတဲ့ order အရေအတွက်၊ 100 နဲ့ အထက် order တွေနဲ့ အများဆုံး ဝယ်တဲ့ customer ကို report ထုတ်ပါ။ Loop မသုံးဘဲ source list ကိုလည်း မပြင်ရပါဘူး။
- Paid order တွေကို customer အလိုက် groupingBy + summingInt နဲ့ ပေါင်းပြီး TreeMap နဲ့ sort လုပ်ရမယ်
- Unpaid order အရေအတွက်ကို filter + count နဲ့ တွက်ရမယ်
- 100 နဲ့ အထက် order တွေကို "Aye:120" ပုံစံ string list အဖြစ် ပြောင်းရမယ်
- Paid total အများဆုံး customer ကို max(Map.Entry.comparingByValue()) နဲ့ ရှာရမယ်
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class Main {
record Order(String customer, int amount, boolean paid) {}
public static void main(String[] args) {
List<Order> orders = List.of(
new Order("Aye", 120, true),
new Order("Bo", 80, false),
new Order("Aye", 40, true),
new Order("Chit", 200, true),
new Order("Bo", 60, true));
// TODO: Map<String, Integer> paidTotals (sorted by customer)
// TODO: long unpaid
// TODO: List<String> bigOrders like "Aye:120"
// TODO: String top customer by paid total
}
}ရလဒ်: Program က ဒီ output ကို ထုတ်ရပါမယ်: Paid totals: {Aye=160, Bo=60, Chit=200} Unpaid orders: 1 Big orders: [Aye:120, Chit:200] Top customer: Chit Source size unchanged: 5
အရိပ်အမြွက် ကြည့်ရန်
groupingBy ရဲ့ argument သုံးခု version က (key function, map factory, downstream collector) ဖြစ်ပါတယ် — Order::customer, TreeMap::new, Collectors.summingInt(Order::amount)။ Record ရဲ့ accessor တွေက paid() လို method တွေဖြစ်လို့ Order::paid ကို Predicate အဖြစ် တန်းသုံးလို့ ရပါတယ်။
အဖြေ ကြည့်ရန်
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class Main {
record Order(String customer, int amount, boolean paid) {}
public static void main(String[] args) {
List<Order> orders = List.of(
new Order("Aye", 120, true),
new Order("Bo", 80, false),
new Order("Aye", 40, true),
new Order("Chit", 200, true),
new Order("Bo", 60, true));
Map<String, Integer> paidTotals = orders.stream()
.filter(Order::paid)
.collect(Collectors.groupingBy(
Order::customer, TreeMap::new, Collectors.summingInt(Order::amount)));
System.out.println("Paid totals: " + paidTotals);
long unpaid = orders.stream().filter(o -> !o.paid()).count();
System.out.println("Unpaid orders: " + unpaid);
List<String> bigOrders = orders.stream()
.filter(o -> o.amount() >= 100)
.map(o -> o.customer() + ":" + o.amount())
.toList();
System.out.println("Big orders: " + bigOrders);
String top = paidTotals.entrySet().stream()
.max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse("none");
System.out.println("Top customer: " + top);
System.out.println("Source size unchanged: " + orders.size());
}
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Lambda က functional interface (Predicate, Function, Consumer, Supplier) ရဲ့ abstract method တစ်ခုကို implement လုပ်ပြီး method reference က အဲဒါကို ပိုတိုစေပါတယ်။
- Stream pipeline = source → lazy intermediate operation တွေ → terminal operation တစ်ခု၊ terminal ကို ခေါ်မှ run ပါတယ်။
- Stream ကို တစ်ခါပဲ သုံးလို့ရပြီး source collection ကို မပြောင်းပါဘူး၊ toList() က unmodifiable list ပြန်ပေးပါတယ်။
- ရလဒ်ကို collect/toList နဲ့ ယူပါ၊ lambda ထဲကနေ shared state ကို မပြင်ပါနဲ့၊ parallel ကို တိုင်းတာပြီးမှ သုံးပါ။
Java Stream API — Oracle