小编Ale*_*lex的帖子

Kotlin排序为空

什么是Kotlin排序对象列表的可空字段与空值最后?

Kotlin对象排序:

@JsonInclude(NON_NULL)
data class SomeObject(
    val nullableField: String?
)
Run Code Online (Sandbox Code Playgroud)

类似于以下Java代码:

@Test
public void name() {
    List<SomeObject> sorted = Stream.of(new SomeObject("bbb"), new SomeObject(null), new SomeObject("aaa"))
            .sorted(Comparator.comparing(SomeObject::getNullableField, Comparator.nullsLast(Comparator.naturalOrder())))
            .collect(toList());

    assertEquals("aaa", sorted.get(0).getNullableField());
    assertNull(sorted.get(2).getNullableField());
}

@Getter
@AllArgsConstructor
private static class SomeObject {
    private String nullableField;
}
Run Code Online (Sandbox Code Playgroud)

sorting kotlin

27
推荐指数
2
解决办法
2561
查看次数

标签 统计

kotlin ×1

sorting ×1