- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这可能是一个简单的问题,但我想清楚地理解它......
我有这样的代码:
public final class Persona
{
private final int id;
private final String name
public Persona(final int id,final String name)
{
this.id = id;
this.name = name;
}
public int getId(){return id;}
public String getName(){return name;}
@Override
public String toString(){return "Persona{" + "id=" + id + ", name=" + name+'}';}
}
我正在测试这段代码:
import static java.util.Comparator.*;
private void nullsFirstTesting()
{
final Comparator<Persona>comparator = comparing(Persona::getName,nullsFirst(naturalOrder()));
final List<Persona>persons = Arrays.asList(new Persona(1,"Cristian"),new Persona(2,"Guadalupe"),new Persona(3,"Cristina"),new Persona(4,"Chinga"),new Persona(5,null));
persons
.stream()
.sorted(comparator)
.forEach(System.out::println);
}
这显示了以下结果:
Persona{id=5, name=null}
Persona{id=4, name=Chinga}
Persona{id=1, name=Cristian}
Persona{id=3, name=Cristina}
Persona{id=2, name=Guadalupe}
这些结果对我来说没问题,但我在理解上有问题。
当我忽略 new Persona(5,null)
对象并传递比较器时:
final Comparator<Persona>comparator = comparing(Persona::getName);
它就像一个魅力。我的排序是按名称属性的自然顺序
。当我使用 name=null
添加对象时出现问题,我只是认为我需要这样的比较器。
final Comparator<Persona>comparator = comparing(Persona::getName,nullsFirst());
我的想法是错误的:“好吧,当名称为非空时,它们按照名称的自然顺序
排序,就像前面的比较器一样,如果它们是 null
它们将排在第一位 但我的非 null 名称仍将按自然顺序排序”。
但正确的代码是这样的:
final Comparator<Persona>comparator = comparing(Persona::getName,nullsFirst(naturalOrder()));
我不明白nullsFirst
的参数。我只是认为 名称的自然顺序
会显式 [默认] 甚至处理 null
值。
但是文档说:
Returns a null-friendly comparator that considers
null
to be less than non-null. When both arenull
, they are considered equal. If both are non-null, the specifiedComparator
is used to determine the order. If the specified comparator isnull
, then the returned comparator considers all non-null values to be equal.
这一行:“如果两者都不为空,则使用指定的Comparator
来确定顺序。”
我对何时以及如何明确设置自然顺序或何时推断自然顺序感到困惑。
最佳答案
“自然顺序”比较器,即当您使用仅带有一个参数的比较
时得到的结果,不处理空值。 (我不确定你从哪里得到它的想法。)Comparable
类的“自然顺序”由 compareTo()
方法定义,它是像这样使用:
obj1.compareTo(obj2)
显然,如果 obj1
为 null,这将不起作用;对于String
,如果obj2
为null也会抛出异常。
naturalOrder()
方法返回一个比较两个对象的Comparator
。 javadoc明确指出此比较器在比较 null 时抛出 NullPointerException
。
nullsFirst()
方法(和 nullsLast()
类似)基本上将 Comparator
转换为新的 Comparator
。您放入一个比较器,如果它尝试比较 null,它可能会抛出异常,并且它吐出一个新的比较器,它的工作方式相同,只是它允许 null 参数。所以这就是为什么你需要一个参数到 nullsFirst
——因为它在现有比较器之上构建了一个新的比较器,你告诉它现有的比较器是什么。
那么,如果您省略参数,为什么它不给您自然顺序呢?因为他们没有那样定义它。 nullsFirst
在 javadoc 中定义获取参数:
static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
我认为如果设计师愿意,他们可以添加一个不带参数的重载:
static <T> Comparator<T> nullsFirst() // note: not legal
这与使用 nullsFirst(naturalOrder())
相同。但他们没有,所以你不能那样使用它。
关于Java 8 Comparator nullsFirst naturalOrder 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26350996/
我真的很困惑。我已经尝试使用带有 tomcat 的 Jax-rs 并使用所有注释,我能够使用 url 调用我的服务。因此,如果没有 Jax-rs,我可以简单地拥有一个 servlet 并调用我的服务。
是否有任何工具/商业混淆器可以混淆 WPF 控件中的 BAML 资源? 如果没有,就 IP 保护而言,这是一段艰难的时期,因为黑客可以通过使用 BAML 到 XAML 转换器轻松查看 BAML 资源。
嘿大家。我在尝试使用 COBOL 在 zOS 环境中解决的编码项目中遇到了一些麻烦。我需要读入一个文件并将它们放入索引表中(我知道将少于 90 条记录)。 让我感到困扰的是,我们受到项目参数的约束,以
我试图按照这个例子来理解 join() 方法: class PrintDemo { public void printCount() { try { for(int
当我编译我正在编写的代码,然后在 JD Gui 中查看时,方法显示带有如下标题: public void growSurface(Random paramRandom, int paramInt1,
我正在为重新分发准备 Android 库,它的代码必须进行混淆处理。我已经阅读了有关此主题的一些内容,并且决定使用 Android Library Project。它将作为 jar 分发(自动在/bi
两个混淆相关的问题: 1) 是否有任何工具可以将 F# 从 MSIL 目标形式反汇编回其源形式或接近它的形式?这不是通过默默无闻来实现安全性的尝试,但我想保护某些源代码免遭“盗窃”。 2) 我简要地查
谁能向我解释为什么 simulatedCase <- rbinom(100,1,0.5) simDf <- data.frame(CASE = simulatedCase) posterior_m0
我一直无法找到关于使用 AppDomains 时发生的事情的非常清楚的描述,所以希望有人能够启发我。我有一个简单的测试程序(基本上是扯掉了 MSDN example ): using System;
假设我有 2 个分支topic和 master如果我在 topic分支,然后运行 git rebase master它是 rebase master 还是 rebase 主题分支? 做 git r
我有一个类(class): public class LockTest { public void LockThis() { lock (this)
我正在尝试最小化/混淆我的 Angular 代码,但遇到了问题。我在这里阅读“缩小说明”http://docs.angularjs.org/tutorial/step_05但我定义我的 Control
我遇到了一些困惑的操作。 var a = 0.1; var b = 0.2; var c = 0.3; console.log(a); // 0.1 console.log(b); // 0.2 co
感谢您查看我的帖子 - 我正在尝试弄清楚如何在单击链接时关闭此下拉菜单,但我的 JavaScript 技能非常缺乏,而且代码似乎很困惑。这是 HTML:
混淆、哈希和加密之间有什么区别? 这是我的理解: 哈希是一种单向算法;无法逆转 混淆与加密类似,但不需要任何“ secret ”即可理解(ROT13 就是一个例子) 加密是可逆的,但需要“ secre
我有以下代码 my $content = $response->content; $content =~ /username=([\s\S]+?)&/; my $username = $1; prin
我在 .NET 中发现了一些与我预期的有点不同的东西。我粘贴的代码没有意义,但它是我拥有的一个复杂得多的函数的浓缩版。我实际上是在获取匿名类型信息作为参数(尚未创建匿名类型的实例),我需要创建该类型的
我正在努力解决 JavaFX 应用程序的混淆问题。使用此项目作为基础: https://github.com/openjfx/samples/tree/master/IDE/IntelliJ/Non-
是否可以制作一个与此类似的 CSV 阅读器 while((line = reader.readLine()) != null){ String[] values = line.
公共(public)类测试2 { 公共(public)静态无效主(字符串[]参数){ System.out.println("3 + 6"); System.out.println(3
我是一名优秀的程序员,十分优秀!