๐ ์ ํฌ์คํ ์ ์ด์ด์ง๋ ์๋ฆฌ์ฆ(?)
1๏ธโฃ first() / last()
- first() : ์ปฌ๋ ์ ์ ์ฒซ ๋ฒ์งธ ์๋ฃ๋ฅผ ๋ฐํ, null ๋ฐ์ ์ ์ค๋ฅ
- last() : ์ปฌ๋ ์ ์ ๋ง์ง๋ง ์๋ฃ๋ฅผ ๋ฐํ, null ๋ฐ์ ์ ์ค๋ฅ
- firstOrNull() : ์ปฌ๋ ์ ์ ์ฒซ ๋ฒ์งธ ์๋ฃ๋ฅผ ๋ฐํ, null ๋ฐ์ ์ null ๋ฐํ
- lastOrNull() : ์ปฌ๋ ์ ์ ๋ง์ง๋ง ์๋ฃ๋ฅผ ๋ฐํ, null ๋ฐ์ ์ null ๋ฐํ
val days = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
fun firstLastExample() {
println("< first() >")
println("์ฒซ ๋ฒ์งธ ์ธ์๋ " + days.first() + "\n")
println("< last() >")
println("๋ง์ง๋ง ์ธ์๋ " + days.last() + "\n")
println("< ์กฐ๊ฑด์ด ์๋ first() >")
println("์ฒซ ๋ฒ์งธ ์ธ์๋ " + days.first {day -> day.startsWith("W")} + "\n")
println("< ์กฐ๊ฑด์ด ์๋ last() >")
println("๋ง์ง๋ง ์ธ์๋ " + days.last {day -> day.startsWith("W")} + "\n")
println("< firstOrNull() >")
// println(days.first() {day -> day.startsWith("N")}) -> NoSuchElementException
println("N์ผ๋ก ์์ ํ๋ ์์ผ : " + days.firstOrNull {day -> day.startsWith("N")} + "\n")
println("< lastOrNull() >")
// println(days.last() {day -> day.startsWith("N")}) -> NoSuchElementException
println("N์ผ๋ก ์์ ํ๋ ์์ผ : " + days.lastOrNull {day -> day.startsWith("N")})
}
/*
< first() >
์ฒซ ๋ฒ์งธ ์ธ์๋ Monday
< last() >
๋ง์ง๋ง ์ธ์๋ Sunday
< ์กฐ๊ฑด์ด ์๋ first() >
์ฒซ ๋ฒ์งธ ์ธ์๋ Wednesday
< ์กฐ๊ฑด์ด ์๋ last() >
๋ง์ง๋ง ์ธ์๋ Wednesday
< firstOrNull() >
N์ผ๋ก ์์ ํ๋ ์์ผ : null
< lastOrNull() >
N์ผ๋ก ์์ ํ๋ ์์ผ : null
*/
2๏ธโฃ filter()
- ์ปฌ๋ ์ ๋ด์ ์กฐ๊ฑด์ ์ผ์นํ๋ ์๋ฃ ๋ฐํ
val days = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
fun filterExample() {
days.filter { day -> day.contains("S") }.forEach { print("$it ") }
}
/*
Saturday Sunday
*/
3๏ธโฃ take() / takeLast()
- take() : ์์์๋ถํฐ ์ธ์๋ก ๋ฐ์ ๊ฐ์ ๋งํผ ์ธ์๋ก ๊ฐ๋ List ๋ฐํ
- takeLast() : ๋ค์์๋ถํฐ ์ธ์๋ก ๋ฐ์ ๊ฐ์ ๋งํผ ์ธ์๋ก ๊ฐ๋ List ๋ฐํ
- takeWhile() : ์์์๋ถํฐ ์กฐ๊ฑด์ด false๊ฐ ๋ ๋๊น์ง ์กฐ๊ฑด์ ๋ถํฉํ๋ ์ธ์๋ค์ ๊ฐ๋ List ๋ฐํ
- takeLastWhile() : ๋ค์์๋ถํฐ ์กฐ๊ฑด์ด false๊ฐ ๋ ๋๊น์ง ์กฐ๊ฑด์ ๋ถํฉํ๋ ์ธ์๋ค์ ๊ฐ๋ List ๋ฐํ
val days = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
fun takeExample() {
println("< take() >")
days.take(2).forEach { print("$it ") }
println("\n")
println("< takeLast() >")
days.takeLast(2).forEach { print("$it ") }
println("\n")
println("< takeWhile() >")
days.takeWhile { day -> day.length == 6 }.forEach { print("$it ") }
println("\n")
println("< takeLastWhile() >")
days.takeLastWhile { day -> day.startsWith("S") }.forEach { print("$it ") }
}
/*
< take() >
Monday Tuesday
< takeLast() >
Saturday Sunday
< takeWhile() >
Monday
< takeLastWhile() >
Saturday Sunday
*/
4๏ธโฃ drop() / dropLast()
- take ํจ์์ ๋ฐ๋
- drop() : ์์์๋ถํฐ ์ธ์๋ก ๋ฐ์ ๊ฐ์ ๋งํผ ์ ์ธํ List ๋ฐํ
- dropLast() : ๋ค์์๋ถํฐ ์ธ์๋ก ๋ฐ์ ๊ฐ์ ๋งํผ ์ ์ธํ List ๋ฐํ
- dropWhile() : ์์์๋ถํฐ ์กฐ๊ฑด์ด false๊ฐ ๋ ๋๊น์ง ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์ธ์๋ฅผ ์ ์ธํ List ๋ฐํ
- dropLastWhile() : ๋ค์์๋ถํฐ ์กฐ๊ฑด์ด false๊ฐ ๋ ๋๊น์ง ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์ธ์๋ฅผ ์ ์ธํ List ๋ฐํ
val days = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
fun dropExample() {
println("< drop() >")
days.drop(2).forEach { print("$it ") }
println("\n")
println("< dropLast() >")
days.dropLast(2).forEach { print("$it ") }
println("\n")
println("< dropWhile() >")
days.dropWhile { day -> day.length < 9 }.forEach { print("$it ") }
println("\n")
println("< dropLastWhile() >")
days.dropLastWhile { day -> day.length < 9 }.forEach { print("$it ") }
}
/*
< drop() >
Wednesday Thursday Friday Saturday Sunday
< dropLast() >
Monday Tuesday Wednesday Thursday Friday
< dropWhile() >
Wednesday Thursday Friday Saturday Sunday
< dropLastWhile() >
Monday Tuesday Wednesday
*/
5๏ธโฃ distinct()
- distinct() : ์ค๋ณต๋ ํญ๋ชฉ ์ ์ธ ํ ๋ฐํ
- distinctBy() : ํน์ ์กฐ๊ฑด์ ์ํํ์ ๋, ์ค๋ณต๋๋ ๊ฐ ์ ์ธ ํ ๋ฐํ
fun distinctExample(){
val numbers = arrayListOf(1, 1, -1, 0, -3, 1, 2, -2, 2, 0)
println("< distinct() >")
println(numbers.distinct())
println()
println("< distinctBy() >")
println(numbers.distinctBy { number -> number.absoluteValue }) // ์ ๋๊ฐ์ด ๊ฐ์ผ๋ฉด ์ค๋ณต์ผ๋ก ์ ์ธ
}
/*
< distinct() >
[1, -1, 0, -3, 2, -2]
< distinctBy() >
[1, 0, -3, 2]
*/
โJAVA์์ ๋น์ทํ๊ฒ ๊ตฌํํด๋ณด๊ธฐ
๋๋ณด๊ธฐ
์์ ํ ๋๊ฐ์ด ๊ตฌํ์ด ์๋์์..
ํนํ ๋ค์์๋ถํฐ ๋น๊ตํ๋ ๊ฒ๋ค์ ๋๊ฐ์ด ํด๋ด์ง ๋ชปํ์..
๋ ๊ณต๋ถํด์ ์์ ๋๊ฐ์ด ํด๋ณผ ์ ์๊ธฐ๋ฅผ
static String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
static int[] numbers = {1, 1, -1, 0, -3, 1, 2, -2, 2, 0};
private static void filtering() {
List<String> reverse = new ArrayList<>(List.of(days));
Collections.reverse(reverse);
System.out.println("< ํผ์คํธ, ๋ผ์คํธ >");
System.out.println("์ฒซ ๋ฒ์งธ ์ธ์๋ " + Arrays.stream(days).findFirst().orElse(null));
// System.out.println("N์ผ๋ก ์์ ํ๋ ์์ผ : " + Arrays.stream(days).filter(day -> day.startsWith("N")).findFirst().orElseThrow()); -> NoSuchElementException
System.out.println("N์ผ๋ก ์์ ํ๋ ์์ผ : " + Arrays.stream(days).filter(day -> day.startsWith("N")).findFirst().orElse(null));
System.out.println("๋ง์ง๋ง ์ธ์๋ " + reverse.stream().findFirst().orElse(null));
// System.out.println( "N์ผ๋ก ์์ ํ๋ ์์ผ : " + reverse.stream().findFirst().orElseThrow());
System.out.println("N์ผ๋ก ์์ ํ๋ ์์ผ : " + reverse.stream().findFirst().orElse(null));
System.out.println();
System.out.println("< ํํฐ >");
Arrays.stream(days).filter(day -> day.contains("S")).forEach(s -> System.out.print(s + " "));
System.out.println();
System.out.println();
System.out.println("< ํ
์ดํฌ >");
Arrays.stream(days).takeWhile(day -> day.length() == 6).forEach(s -> System.out.print(s + " "));
System.out.println();
reverse.stream().takeWhile(day -> day.startsWith("S")).forEach(s -> System.out.print(s + " ")); // ๋ค์ง์ด์ง ์ฑ๋ก ์ถ๋ ฅ๋จ
System.out.println();
System.out.println();
System.out.println("< ๋๋กญ >");
Arrays.stream(days).dropWhile(day -> day.length() < 9).forEach(s -> System.out.print(s + " "));
System.out.println();
reverse.stream().dropWhile(day -> day.length() < 9).forEach(s -> System.out.print(s + " ")); // ๋ค์ง์ด์ง ์ฑ๋ก ์ถ๋ ฅ๋จ
System.out.println();
System.out.println();
System.out.println("< ์ค๋ณต ์ ๊ฑฐ >");
System.out.println(Arrays.toString(IntStream.of(numbers).distinct().toArray()));
}
/*
< ํผ์คํธ, ๋ผ์คํธ >
์ฒซ ๋ฒ์งธ ์ธ์๋ Monday
N์ผ๋ก ์์ ํ๋ ์์ผ : null
๋ง์ง๋ง ์ธ์๋ Sunday
N์ผ๋ก ์์ ํ๋ ์์ผ : Sunday
< ํํฐ >
Saturday Sunday
< ํ
์ดํฌ >
Monday
Sunday Saturday
< ๋๋กญ >
Wednesday Thursday Friday Saturday Sunday
Wednesday Tuesday Monday
< ์ค๋ณต ์ ๊ฑฐ >
[1, -1, 0, -3, 2, -2]
*/
'Kotlin' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Kotlin] Stream ํจ์ #3. ์กฐํฉ ๋ฐ ๊ธฐํ ํจ์ (2) | 2023.12.07 |
---|---|
[Kotlin] Stream ํจ์ #1.Map์ด๋ผ๋ ๋จ์ด๊ฐ ๋ค์ด๊ฐ๋ ํจ์ (1) | 2023.12.04 |
[Kotlin] Label์ ์ด์ฉํ break, continue (1) | 2023.11.14 |
[Kotlin] ๋ฐ๋ณต๋ฌธ(while, do-while, for) (0) | 2023.11.06 |
[Kotlin] ์กฐ๊ฑด๋ฌธ(if-else, when) (0) | 2023.11.01 |
๋๊ธ