๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Kotlin

[Kotlin] Stream ํ•จ์ˆ˜ #2.ํ•„ํ„ฐ๋ง ํ•จ์ˆ˜

by Bhinney 2023. 12. 5.

๐Ÿ“Ž ์•ž ํฌ์ŠคํŒ…์— ์ด์–ด์ง€๋Š” ์‹œ๋ฆฌ์ฆˆ(?)


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]
*/

๋Œ“๊ธ€