如何在Kotlin的.forEach迭代中获取Set中的某个元素

时间:2019-06-07 11:05:42

标签: kotlin foreach iterator set

为了获取满足if条件的特定元素,“ pairedDevices”之后的表达式是什么?

//creating a SET of paired devices
    val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices

    //for each paired device
    pairedDevices?.forEach { device ->
        if (device.name == "HC-05") {
            val hc05 = pairedDevices

        }

    }

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找

hc05 = pairedDevices?.first { it.name == "HC-05" }