mapIndexedNotNullTo
val dw = mutableListOf<String>()
val data = arrayOf("one", null, "three", "four", null)
// 将非空字符串转换为其长度,并添加索引信息, 带索引的非空字符串长度: [0_3, 2_5, 3_4]
data.mapIndexedNotNullTo(dw) { index, word ->
if (word != null) {
"$index" + "_" + word.length
} else {
null
}
}.forEach {
_console.log(it)
}
def dw = $objectWrappers.wrap($iterables.mutableListOf())
def data = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.String"), "one", null, "three", "four", null))
// 将非空字符串转换为其长度,并添加索引信息, 带索引的非空字符串长度: [0_3, 2_5, 3_4]
data.mapIndexedNotNullTo(dw) { index, word ->
if (word != null) {
index + "_" + $objectWrappers.wrap(word).count()
}
}.forEach {
$console.log(it)
}
let dw = $objectWrappers.wrap($iterables.mutableListOf())
let data = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.String"), "one", null, "three", "four", null))
// 将非空字符串转换为其长度,并添加索引信息, 带索引的非空字符串长度: [0_3, 2_5, 3_4]
data.mapIndexedNotNullTo(dw, (index, word) => {
if (word != null) {
return index + "_" + $objectWrappers.wrap(word).count()
} else {
return null
}
}).forEach((it) => {
$console.log(it)
})
local dw = _objectWrappers:wrap(_iterables:mutableListOf())
local data = _objectWrappers:wrap(_arrays:arrayOf(_plugins:loadClass("java.lang.String"), "one", nil, "three", "four",
nil))
-- 将非空字符串转换为其长度,并添加索引信息, 带索引的非空字符串长度: [0_3, 2_5, 3_4]
data:mapIndexedNotNullTo(dw, function(index, word)
if (word ~= nil) then
return index .. "_" .. _objectWrappers:wrap(word):count()
else
return nil
end
end):forEach(function(it)
_console:log(it)
end)
<?php
$dw = $objectWrappers->wrap($iterables->mutableListOf());
$data = $objectWrappers->wrap($arrays->arrayOf($plugins->loadClass("java.lang.String"), "one", null, "three", "four", null));
// 将非空字符串转换为其长度,并添加索引信息, 带索引的非空字符串长度: [0_3, 2_5, 3_4]
$data->mapIndexedNotNullTo($dw, function ($index, $word) use ($objectWrappers) {
if ($word != null) return $index . "_" . $objectWrappers->wrap($word)->count(); else return null;
})->forEach(function ($it) use ($console) {
$console->log($it);
});
dw = _objectWrappers.wrap(_iterables.mutableListOf())
data = _objectWrappers.wrap(_arrays.arrayOf(_plugins.loadClass("java.lang.String"), "one", None, "three", "four", None))
# 将非空字符串转换为其长度,并添加索引信息, 带索引的非空字符串长度: [0_3, 2_5, 3_4]
data.mapIndexedNotNullTo(dw, lambda index, word: str(index) + "_" + str(
_objectWrappers.wrap(word).count()) if word != None else None).forEach(
lambda it: _console.log(it))
# encoding: utf-8
dw = $objectWrappers.wrap($iterables.mutableListOf())
data = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.String"), "one", nil, "three", "four", nil))
# 将非空字符串转换为其长度,并添加索引信息, 带索引的非空字符串长度: [0_3, 2_5, 3_4]
data.mapIndexedNotNullTo(dw) { |index, word|
if word != nil then
index.to_s + "_" + $objectWrappers.wrap(word).count().to_s
else
nil
end
}.forEach { |it|
$console.log(it)
}
Last modified: 09 September 2025