์คํธ๋ฆผ(Stream)?
- ํจ์ํ ํ๋ก๊ทธ๋จ์ ์ด์ฉํด ๋ค์ํ ๋ฐ์ดํฐ ์์ค(๋ฐฐ์ด, ํ๋ ์ ์ํฌ ๋ฑ)๋ฅผ ํ์คํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ค๋ฃจ๊ธฐ ์ํ ๊ฒ
- Stream์ Collection ์์ ์์.
์คํธ๋ฆผ์ ์ฐ๋ ์ด์ ?
- ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ง๊ณ ์ผ๊ด์ฑ ์ ์ง๊ฐ ๊ฐ๋ฅํด์ง.
โก๏ธ ๋งฅ๋๋ ๋ ๊ฐ๋จ ์ง์ ์ด๋ ๋ถ์ฐ ์ง์ ์ด๋ ํ๋ฒ๊ฑฐ ๋ง์ด ๋๊ฐ์ ๊ฒ ์ฒ๋ผ ๋๊ฐ ์ฝ๋๋ฅผ ์์ฑํ๋ ๋์ผํ ํ์์ด ๋์ฌ ์ ๋ฐ์ ์๊ธฐ ๋๋ฌธ์ ์ฝ๋์ ์ ๋ขฐ๋๊ฐ ๋์์ง๊ณ ๋ฒ๊ทธ๊ฐ ์๊ธฐ์ง ์์.
์คํธ๋ฆผ ํน์ง
- ์๋ฃ์ ๋์๊ณผ ๊ด๊ณ์์ด ๋์ผํ ์ฐ์ฐ ์ํ
- ์ธ๋ถ ๋ฐ๋ณต์ ํตํด ์์ ํ๋ ์ปฌ๋ ์ ๊ณผ ๋ฌ๋ฆฌ ๋ด๋ถ ๋ฐ๋ณต์ ํตํด ์์ ์ํ ( ๋ฐ๋ณต๋ฌธ ๋ฐฐ์ )
- ์คํธ๋ฆผ์ ํ๋ฒ ์์ฑ ๋ ํ ์ต์ข ์ฐ์ฐ์ ํตํด ์๋ชจ๋๋ฉด ์ฌ์ฌ์ฉ ๋ถ๊ฐ (์ต์ข ์ฐ์ฐ์ ํ ๋ฒ๋ง ๊ฐ๋ฅ!!)
- ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ์ง ์์
- ์ง์ฐ ์ฐ์ฐ ์ง์
- ๋ณ๋ ฌ ์ฒ๋ฆฌ ์ง์
parallelStream()
: ๋๋์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ฌ์
๋ณ๋ ฌ์ฒ๋ฆฌ๋ก ๋ฐ๊พธ๋ ค๋ฉด ์คํธ๋ฆผ ์์ฑ ํ ๋ stream() ๋์ parallelStream()์ผ๋ก ๋ฐ๊พธ๋ฉด ๋จ.
์คํธ๋ฆผ ๋์ ํ๋ฆ
์คํธ๋ฆผ ์์ฑ โก๏ธ ์คํธ๋ฆผ ์ค๊ฐ ์ฐ์ฐ(์คํธ๋ฆผ์ ๋ณํ) โก๏ธ ์คํธ๋ฆผ ์ต์ข ์ฐ์ฐ
Integer[] arr = {1,2,3,4,5,6,7,8,9,10};
List<Integer> list = new ArrayList(Arrays.asList(arr));
list.stream().filter(s -> s.intValue() >= 5).forEach(e -> System.out.print(e + " ");
list.stream()
์คํธ๋ฆผ ์์ฑ
.filter(s -> s.intValue() >= 5)
์ค๊ฐ ์ฐ์ฐ
.forEach(e -> System.out.print(e + " ");
์ต์ข
์ฐ์ฐ
list.stream().filter(s -> s.intValue() >= 5).forEach(System.out::println);
๋ก ๋ฐ๊ฟ ์๋ ์์!
๐ถsum(), count()
Integer[] arr = {1,2,3,4,5,6,7,8,9,10};
List<Integer> list = new ArrayList<>(Arrays.asList(arr));
int sum = list.stream().mapToInt(e -> e).sum();
System.out.println("ํฉ๊ณ : " + sum);
- mapToInt(e -> e) : e(์์)๋ฅผ ๋๋ฉด์ e๋ฅผ intํ์ผ๋ก ๋ณํํ์ฌ ์๋ก์ด ์คํธ๋ฆผ์ผ๋ก ๋ฐํ
Integer[] arr = {1,2,3,4,5,6,7,8,9,10};
List<Integer> list = new ArrayList<>(Arrays.asList(arr));
int count = (int)list.stream().count();
System.out.println("ํ์ : " + count);
- count()๋ return ๊ฐ์ด long ์ด๊ธฐ ๋๋ฌธ์ ํ๋ณํ ํ์
๐ถ๋ด๋ถ ๋ฐ๋ณต์ ์ฌ์ฉ
class Student {
String name;
int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add(new Student("ํ๊ธธ๋", 87));
list.add(new Student("์ด์์ ", 65));
list.add(new Student("ํ์๋ด", 98));
list.stream().forEach(s -> {
String name = s.getName();
int score = s.getScore();
System.out.println(name + "-" + score);
});
double avg = list.stream().mapToInt(s -> s.getScore()).average().getAsDouble();
System.out.prinf("ํ๊ท ์ ์ : %.2f", avg);
๐ถ๋ฐฐ์ด๋ก ๋ถํฐ ์คํธ๋ฆผ ๋ง๋ค๊ธฐ
- stream์ Collection์ ์์ด์ ๋ฐฐ์ด์ stream ์ธ ์ ์์.
Stream<E> ์ฐธ์กฐ๋ณ์๋ช = Arrays.stream(๋ฐฐ์ด๋ช );
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
String[] arr = {"ํ๊ธธ๋", "ํ์๋ด", "์ด์์ ", "์ ์ฌ์๋น", "์์ค๊ทผ", "๊ฐ๊ฐ์ฐฌ"};
Stream<String> stream1 = Arrays.stream(arr);
stream1.forEach(e - > System.out.print(e + " "));
Stream<String> stream2 = Arrays.stream(arr, 1, 3);
stream2.forEach(e -> System.out.print(e + " ");
}
}
ํ๊ธธ๋ ํ์๋ด ์ด์์ ์ ์ฌ์๋น ์์ค๊ทผ ๊ฐ๊ฐ์ฐฌ
ํ์๋ด ์ด์์
๐ถ์ซ์ ๋ฒ์๋ก ๋ถํฐ ์คํธ๋ฆผ ๋ง๋ค๊ธฐ
IntStream stream = IntStream.rangeClosed(1,100);
int sum = stream.sum();
System.out.println("ํฉ๊ณ : " + sum);
๐ถํ์ผ๋ก ๋ถํฐ ์คํธ๋ฆผ ์ป๊ธฐ(path)
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws IOException {
Path path = Paths.get("src/์คํธ๋ฆผ์์ 4_0208/chicken.txt");
Stream<String> stream = Files.lines(path, Charset.defaultCharset());
stream.forEach(System.out::println);
System.out.println();
}
}
๐ถ์ค๊ฐ ์ฐ์ฐ
- ์์ฑ๋ ์คํธ๋ฆผ์ ์ค๊ฐ ์ฐ์ฐ์ ํตํด ๋ ๋ค๋ฅธ ์คํธ๋ฆผ์ผ๋ก ๋ณํ
- ์ค๊ฐ ์ฐ์ฐ์ ์ฐ์์ผ๋ก ์ฐ๊ฒฐํด์ ์ฌ์ฉ ๊ฐ๋ฅ
์คํธ๋ฆผ ํํฐ๋ง
filter()
- ์กฐ๊ฑด์ ๋ง๋ ์์๋ง์ผ๋ก ๊ตฌ์ฑ๋ ์๋ก์ด ์คํธ๋ฆผ ๋ฐํ
IntStream stream = IntStream.of(7,5,5,2,1,3,5,4,5,6);
stream.filter(n -> n % 2 != 0).forEach(e -> System.out.print(e + " "));
7 5 5 1 3 5 5
distinct()
- ์คํธ๋ฆผ์์ ์ค๋ณต๋ ์์๋ฅผ ์ ๊ฑฐํ๊ณ ์๋ก์ด ์คํธ๋ฆผ ๋ฐํ
IntStream stream = IntStream.of(7,5,5,2,1,3,5,4,5,6);
stream.distinct().forEach(s -> System.out.print(s + " ");
7 5 2 1 3 4 6
์คํธ๋ฆผ ๋ณํ
map()
- ์ ๋ ฅํ ์์ ๊ทธ๋๋ก ์คํธ๋ฆผ์ผ๋ก ๋ฐํ
Stream<String> stream = Stream.of("HTML", "CSS", "JAVA", "JAVASCRIPT");
stream.map(e -> e.length()).forEach(e -> System.out.print(e + " "));
4 3 4 10
flatMap()
- ์
๋ ฅํ ์์๋ฅผ ๊ฐ์ฅ ์์ ๋จ์๋ก ๋ฐํ
(์์ ์ฒ๋ผ split()ํ๋ฉด split ์กฐ๊ฑด๋๋ก ๋ฐํ๋จ) - map()์ split() ํด๋ ์์ชผ๊ฐ์ง
String[] arr = {"I study hard", "You study JAVA", "I am hungry"};
Stream<String> stream4 = Arrays.stream(arr);
stream4.flatMap(s -> Stream.of(s.split(" "))).forEach(System.out::println);
I
study
hard
You
study
JAVA
I
am
hungry
map()์ผ๋ก ์คํ์ ๊ฒฐ๊ณผ
java.util.stream.ReferencePipeline$Head@3567135c
java.util.stream.ReferencePipeline$Head@327471b5
java.util.stream.ReferencePipeline$Head@4157f54e
์คํธ๋ฆผ ์ ํ
limit()
- ํด๋น ์คํธ๋ฆผ์ ์ฒซ ๋ฒ์งธ ์์๋ถํฐ ์ ๋ฌ๋ ๊ฐ์ ๋งํผ์ ์์๋ก๋ง ์ด๋ฃจ์ด์ง ์๋ก์ด ์คํธ๋ฆผ ๋ฐํ
IntStream stream5 = IntStream.range(0, 10);
stream5.limit(5).forEach(n -> System.out.print(n + " "));
0 1 2 3 4
skip()
- ํด๋น ์คํธ๋ฆผ์ ์ฒซ ๋ฒ์งธ ์์๋ถํฐ ์ ๋ฌ๋ ๊ฐ์ ๋งํผ์ ์์๋ฅผ ์ ์ธํ๊ณ ์ถ๋ ฅ
IntStream stream6 = IntStream.range(0, 10);
stream6.skip(5).forEach(n -> System.out.print(n + " "));
stream7.skip(3).limit(5).forEach(n -> System.out.print(n + " "));
5 6 7 8 9
3 4 5 6 7
์คํธ๋ฆผ ์ ๋ ฌ
sorted()
- ํด๋น ์คํธ๋ฆผ์ ์ฃผ์ด์ง ๋น๊ต์(comparator)๋ฅผ ์ด์ฉํ์ฌ ์ ๋ ฌ
Stream<String> stream8 = Stream.of("HTML", "CSS", "JAVA", "JAVASCRIPT");
Stream<String> stream9 = Stream.of("HTML", "CSS", "JAVA", "JAVASCRIPT");
stream8.sorted().forEach(e -> System.out.print(e + " "));
stream9.sorted(Comparator.reverseOrder()).forEach(e -> System.out.print(e + " "));
CSS HTML JAVA JAVASCRIPT
JAVASCRIPT JAVA HTML CSS
์คํธ๋ฆผ ์ต์ข ์ฐ์ฐ
Stream.of() : ์์์ ์ถ๋ ฅ
Stream<String> stream = Stream.of("ํ๋", "๋", "์
", "๋ท");
stream.forEach(System.out::println);
ํ๋
๋
์
๋ท
์์์ ์๋ชจ
- ์คํธ๋ฆผ์ ์์๋ฅผ ์๋ชจํ์ฌ ์ฐ์ฐ์ ์ํ
โญ reduce() : ์ฒซ๋ฒ์งธ ์์์ ๋๋ฒ์งธ ์์๋ฅผ ๊ฐ์ง๊ณ ์ฐ์ฐ์ ์ํ, ๊ทธ ๊ฒฐ๊ณผ์ ์ธ๋ฒ์งธ ์์๋ฅผ ๊ฐ์ง๊ณ ๋ค์ ์ฐ์ฐ
Stream<String> stream1 = Stream.of("ํ๋", "๋", "์
", "๋ท");
Stream<String> stream2 = Stream.of("ํ๋", "๋", "์
", "๋ท");
IntStream stream3 = IntStream.of(7,5,5,2,1,3,5,4,6);
OptionalInt sum = stream3.reduce((s1,s2) -> s1 + s2);
System.out.println(sum.getAsInt());
Optional<String> rst1 = stream1.reduce((s1, s2) -> s1 + "+" + s2); // Optional์ NullpointerException ๋ฐฉ์ง
rst1.ifPresent(System.out::println); // ์์ผ๋ฉด null ๋ฐํ
38
ํ๋+๋+์
+๋ท
์์์ ๊ฒ์ฌ
anyMatch() : ํด๋น ์คํธ๋ฆผ์ ์ผ๋ถ ์์๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด true
allMatch() : ํด๋น ์คํธ๋ฆผ์ ๋ชจ๋ ์์๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด true
noneMatch() : ํด๋น ์คํธ๋ฆผ์ ๋ชจ๋ ์์๊ฐ ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ์์ ๊ฒฝ์ฐ true
IntStream stream6 = IntStream.of(30,90,70,10);
IntStream stream7 = IntStream.of(30,90,70,10);
IntStream stream8 = IntStream.of(30,90,70,10);
System.out.println(stream6.anyMatch(n -> n > 80)); // true
System.out.println(stream7.allMatch(n -> n > 9)); // true
System.out.println(stream8.noneMatch(n -> n > 90)); // trse
์์์ ํต๊ณ
count()
IntStream stream9 = IntStream.of(30,90,70,10);
System.out.println(stream9.count());
4
max()
IntStream stream10 = IntStream.of(30,90,70,10);
System.out.println(stream10.max().getAsInt());
90
min()
IntStream stream11 = IntStream.of(30,90,70,10);
System.out.println(stream11.min().getAsInt());
10
System.out.println(stream10.max();
์ถ๋ ฅํ๋ฉด OptionalInt๊ฐ ๋ถ์ด์ ๋์ค๋ฏ๋ก
System.out.println(stream10.max().getAsInt() ๋ก ์ถ๋ ฅํ ๊ฒ
์์์ ์ฐ์ฐ
IntStream stream12 = IntStream.of(30,90,70,10);
DoubleStream stream13 = DoubleStream.of(30.3,90.9,72.3,10.1);
System.out.println(stream12.sum());
System.out.println(stream13.average().getAsDouble());
200
50.9
๐ธ ์ค์ต ์์
๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ ๋ฐ์ดํฐ ์ถ์ถ
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<TravelCustomer> customerList = new ArrayList<>();
customerList.add(new TravelCustomer("ํ๊ธธ๋", 30, 3000));
customerList.add(new TravelCustomer("๊น๋ธ๊ธฐ", 13, 2000));
customerList.add(new TravelCustomer("์ด์์ ", 25, 2500));
customerList.add(new TravelCustomer("๊ฐ๊ฐ์ฐฌ", 19, 4500));
System.out.println("๊ณ ๊ฐ ๋ช
๋จ ์ถ๋ ฅ");
customerList.stream().map(c -> c.getName()).forEach(System.out::println);
int total = customerList.stream().mapToInt(c -> c.getPrice()).sum();
System.out.println("์ด ์ฌํ ๋น์ฉ : " + total + "์");
System.out.println("== 20์ธ ์ด์ ์ฑ์ธ ์ถ๋ ฅ ==");
customerList.stream().filter(e -> e.age >= 20)
.map(c -> c.getName())
.sorted()
.forEach(System.out::println);
}
}
class TravelCustomer {
String name;
int age;
int price;
public TravelCustomer(String name, int age, int price) {
this.name = name;
this.age = age;
this.price = price;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getPrice() {
return price;
}
}
'๐๏ธ Backend > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฐ(Java) - ์ ์ถ๋ ฅ ์คํธ๋ฆผ ์ค์ต ๋ฌธ์ (0) | 2023.02.10 |
---|---|
์๋ฐ(Java) - ์ ์ถ๋ ฅ ์คํธ๋ฆผ (0) | 2023.02.09 |
์๋ฐ(Java) - ๋๋ค(Lambda) (0) | 2023.02.08 |
์๋ฐ(Java) - ์์ธ์ฒ๋ฆฌ (1) | 2023.02.07 |
์๋ฐ(Java) - Thread(๋ฉํฐ ์ค๋ ๋) 2 - RPG ๊ฒ์ ๋ง๋ค๊ธฐ (1) | 2023.02.07 |