associateTo
val m = mutableMapOf<Int, Int>()
val numbers = arrayOf(1, 2, 3, 4, 5)
// 将数字与其平方关联起来, {1=1, 2=4, 3=9, 4=16, 5=25}
numbers.associateTo(m) { it to it * it }.forEach {
_console.log(it)
}
def m = $objectWrappers.wrap($maps.mutableMapOf())
def numbers = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.Integer"), 1, 2, 3, 4, 5))
// 将数字与其平方关联起来, {1=1, 2=4, 3=9, 4=16, 5=25}
numbers.associateTo(m) { $maps.pairOf(it, it * it) }.forEach {
$console.log(it)
}
let m = $objectWrappers.wrap($maps.mutableMapOf())
let numbers = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.Integer"), 1, 2, 3, 4, 5))
// 将数字与其平方关联起来, {1=1, 2=4, 3=9, 4=16, 5=25}
numbers.associateTo(m)(((it) => $maps.pairOf(it, it * it))).forEach((it) => {
$console.log(it)
})
local m = _objectWrappers:wrap(_maps:mutableMapOf())
local numbers = _objectWrappers:wrap(_arrays:arrayOf(_plugins:loadClass("java.lang.Integer"), 1, 2, 3, 4, 5))
-- 将数字与其平方关联起来, {1=1, 2=4, 3=9, 4=16, 5=25end)
numbers:associateTo(m, function(it)
return _maps:pairOf(it, it * it)
end) :forEach(function(it)
_console:log(it)
end)
<?php
$m = $objectWrappers->wrap($maps->mutableMapOf());
$numbers = $objectWrappers->wrap($arrays->arrayOf($plugins->loadClass("java.lang.Long"), 1, 2, 3, 4, 5));
// 将数字与其平方关联起来, {1=1, 2=4, 3=9, 4=16, 5=25})
$numbers->associateTo($m, function ($it) use ($maps) {
return $maps->pairOf($it, $it * $it);
})->forEach(function ($it) use ($console) {
$console->log($it);
});
m = _objectWrappers.wrap(_maps.mutableMapOf())
numbers = _objectWrappers.wrap(_arrays.arrayOf(_plugins.loadClass("java.lang.Long"), 1, 2, 3, 4, 5))
# 将数字与其平方关联起来, {1=1, 2=4, 3=9, 4=16, 5=25}
numbers.associateTo(m, lambda it: _maps.pairOf(it, it * it)).forEach(lambda it: _console.log(it))
# encoding: utf-8
m = $objectWrappers.wrap($maps.mutableMapOf())
numbers = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.Long"), 1, 2, 3, 4, 5))
# 将数字与其平方关联起来, {1=1, 2=4, 3=9, 4=16, 5=25}
numbers.associateTo(m) { |it| $maps.pairOf(it, it * it) }.forEach { |it|
$console.log(it)
}
Last modified: 08 August 2025