본문 바로가기
Kotlin

[Kotlin] Stream 함수 #3. 조합 및 기타 함수

by Bhinney 2023. 12. 7.

앞 포스팅과 이어지는 시리즈(?)


1️⃣ zip()

  • 두 컬렉션의 자료를 조합하여 새로운 컬렉션의 자료를 만듦
val koreanDays = arrayListOf("월요일", "화요일", "수요일", "목요일", "금요일", "토요일", "일요일")
val englishDays = arrayListOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

fun zipExample() {
   englishDays.zip(koreanDays) {eng, kor -> "$eng : $kor "}.forEach { println(it) }
}

/*
Monday : 월요일 
Tuesday : 화요일 
Wednesday : 수요일 
Thursday : 목요일 
Friday : 금요일 
Saturday : 토요일 
Sunday : 일요일 
*/

2️⃣ joinToString()

  • 컬렉션을 문자열로 변환하여 한 문자열로 반환
val koreanDays = arrayListOf("월요일", "화요일", "수요일", "목요일", "금요일", "토요일", "일요일")

fun joinToStringExample() {
   println(koreanDays.joinToString(", "))
}

/*
월요일, 화요일, 수요일, 목요일, 금요일, 토요일, 일요일
*/

3️⃣ reduce() / fold()

  • 컬렉션의 자료를 다 합침
val koreanDays = arrayListOf("월요일", "화요일", "수요일", "목요일", "금요일", "토요일", "일요일")
val englishDays = arrayListOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
val numbers = arrayListOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

fun reduceFoldExample() {

   println("< reduce() >")
   koreanDays.reduce { now, next ->
      println("오늘은 $now, 내일은 $next")
      next
   }
   numbers.reduce{ next, total ->
      println("next $next total $total")
      next + total }

   println()
   println("< fold() >")
   englishDays.fold("") { now, next ->
      println("Today is $now, tomorrow is $next")
      next
   }
   numbers.fold(numbers.first()) {next, total ->
      println("next $next total $total")
      next + total}
}

/*
< reduce() >
오늘은 월요일, 내일은 화요일
오늘은 화요일, 내일은 수요일
오늘은 수요일, 내일은 목요일
오늘은 목요일, 내일은 금요일
오늘은 금요일, 내일은 토요일
오늘은 토요일, 내일은 일요일
next 1 total 2
next 3 total 3
next 6 total 4
next 10 total 5
next 15 total 6
next 21 total 7
next 28 total 8
next 36 total 9
next 45 total 10

< fold() >
Today is , tomorrow is Monday
Today is Monday, tomorrow is Tuesday
Today is Tuesday, tomorrow is Wednesday
Today is Wednesday, tomorrow is Thursday
Today is Thursday, tomorrow is Friday
Today is Friday, tomorrow is Saturday
Today is Saturday, tomorrow is Sunday
next 1 total 1
next 2 total 2
next 4 total 3
next 7 total 4
next 11 total 5
next 16 total 6
next 22 total 7
next 29 total 8
next 37 total 9
next 46 total 10
*/

4️⃣ any() / none()

  • 컬렉션의 자료의 존재 여부를 반환
  • any() : 자료가 있으면 true, 없으면 false
  • none() : 자료가 있으면 false, 없으면 true
val koreanDays = arrayListOf("월요일", "화요일", "수요일", "목요일", "금요일", "토요일", "일요일")
val englishDays = arrayListOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

fun anyNoneExample() {
   println("< any() >")
   println(koreanDays.any())
   println(englishDays.any { day -> day.startsWith("N")})
   println()

   println("< none() >")
   println(koreanDays.none())
   println(englishDays.none { day -> day.startsWith("N") })
}

/*
< any() >
true
false

< none() >
false
true
*/

5️⃣ min() / max() / sum() / average()

  • min() : 가장 작은 값 반환
  • max() : 가장 큰 값 반환
  • sum() : 컬렉션 내의 자료의 합 반환
  • average() : 컬렉션 내의 평균 값 반환
val numbers = arrayListOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

fun numExample() {
   println(numbers.max())
   println(numbers.min())
   println(numbers.sum())
   println(numbers.average())
}

/*
10
1
55
5.5
*/

➕JAVA로 비슷하게 해보기?

더보기

전부 다 하지는 못했음..

 

private static void stream() {
   String[] koreanDays = {"월요일", "화요일", "수요일", "목요일", "금요일", "토요일", "일요일"};
   String[] englishDays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
   int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

   System.out.println("< join? >");
   System.out.println(String.join(", ", koreanDays));
   System.out.println(Arrays.stream(koreanDays).collect(Collectors.joining(", ")));
   
   System.out.println("\n< reduce() >");
      Arrays.stream(koreanDays).reduce((now, next) -> {
         System.out.println("오늘은 " + now + ", 내일은 " + next);
         return next;
      });
      Arrays.stream(englishDays).reduce((now, next) -> {
         System.out.println(String.format("Today is %s, tomorrow is %s", now, next));
         return next;
      });
      IntStream.of(numbers).reduce((next, total) -> {
         System.out.println("next " + next + " total " + total);
         return total + next;
      });

   System.out.println("\n< any() && none()? >");
   System.out.println(Arrays.stream(koreanDays).findAny().isPresent());
   System.out.println(Arrays.stream(englishDays).anyMatch(day -> day.startsWith("N")));
   System.out.println(Arrays.stream(koreanDays).findAny().isEmpty());
   System.out.println(Arrays.stream(englishDays).noneMatch(day -> day.startsWith("N")));

   System.out.println("\n< number 관련 >");
   System.out.println(IntStream.of(numbers).max().orElse(0));
   System.out.println(IntStream.of(numbers).min().orElse(0));
   System.out.println(IntStream.of(numbers).sum());
   System.out.println(IntStream.of(numbers).average().orElse(0.0));
}

/*
< join? >
월요일, 화요일, 수요일, 목요일, 금요일, 토요일, 일요일
월요일, 화요일, 수요일, 목요일, 금요일, 토요일, 일요일

< reduce() >
오늘은 월요일, 내일은 화요일
오늘은 화요일, 내일은 수요일
오늘은 수요일, 내일은 목요일
오늘은 목요일, 내일은 금요일
오늘은 금요일, 내일은 토요일
오늘은 토요일, 내일은 일요일
Today is Monday, tomorrow is Tuesday
Today is Tuesday, tomorrow is Wednesday
Today is Wednesday, tomorrow is Thursday
Today is Thursday, tomorrow is Friday
Today is Friday, tomorrow is Saturday
Today is Saturday, tomorrow is Sunday
next 1 total 2
next 3 total 3
next 6 total 4
next 10 total 5
next 15 total 6
next 21 total 7
next 28 total 8
next 36 total 9
next 45 total 10

< any() && none()? >
true
false
false
true

< number 관련 >
10
1
55
5.5
*/

댓글