kotlin 警告「 warning: ‘sumBy((T) -> Int): Int’ is deprecated. Use sumOf instead.」の解決方法

kotlin 警告「 warning: ‘sumBy((T) -> Int): Int’ is deprecated. Use sumOf instead.」の解決方法

kotlinで、警告「warning: ‘sumBy((T) -> Int): Int’ is deprecated. Use sumOf instead.」の解決方法を記述してます。

環境

  • OS windows11 home
  • java 17.0.2
  • kotlin 1.6.10-release-923

警告全文

以下のコードで発生。

fun main() {

    val list = mutableListOf(10, 20, 30, 40, 50) 

    println(list.sumBy { it * 5 })
    // 750

}

警告メッセージ

hello.kt:5:18: warning: 'sumBy((T) -> Int): Int' is deprecated. Use sumOf instead.
    println(list.sumBy { it * 5 })

原因

kotlin1.5から「sumBy」は非推奨なため

対処法

「uppercase」を使用する

fun main() {

    val list = mutableListOf(10, 20, 30, 40, 50) 

    println(list.sumOf { it * 5 })
    // 750

}