gpt4 book ai didi

java - 使用比较器在 TreeMap 中获取 Null

转载 作者:行者123 更新时间:2023-12-01 20:13:05 25 4
gpt4 key购买 nike

我有一个名为 Empl 的类和两个名为 MySalaryCompMyNameComp 的比较器。

当我运行此代码时,我在 MySalaryComp 中得到 null 作为值,如下面的输出所示。

public class Test{

public static void main(String a[]) {
TreeMap<Empl, String> tm = new TreeMap<Empl, String>(new MyNameComp());
tm.put(new Empl("zzz", 3000), "RAM");
tm.put(new Empl("aaa", 6000), "JOHN");
Set<Empl> keys = tm.keySet();
for (Empl key : keys) {
System.out.println(key + " ==> " + tm.get(key));
}

TreeMap<Empl, String> trmap = new TreeMap<Empl, String>(new MySalaryComp());
trmap.put(new Empl("zzz", 3000), "RAM");
trmap.put(new Empl("aaa", 6000), "JOHN");
Set<Empl> ks = trmap.keySet();
for (Empl key : ks) {
System.out.println(key + " ==> " + trmap.get(key));
}
}
}

class MyNameComp implements Comparator<Empl> {

@Override
public int compare(Empl e1, Empl e2) {
return e1.getName().compareTo(e2.getName());
}
}

class MySalaryComp implements Comparator<Empl> {

@Override
public int compare(Empl e1, Empl e2) {
if (e1.getSalary() > e2.getSalary()) {
return 1;
} else {
return -1;
}
}
}

class Empl {

private String name;
private int salary;

public Empl(String n, int s) {
this.name = n;
this.salary = s;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}


}

上述代码的输出是:

Name: aaa-- Salary: 6000 ==> JOHN
Name: zzz-- Salary: 3000 ==> RAM
Name: zzz-- Salary: 3000 ==> null
Name: aaa-- Salary: 6000 ==> null

有人可以帮助我理解为什么显示空值吗?以及如何修复它。

最佳答案

您的比较器未正确实现。阅读 JavaDocs:

https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

试试这个:

@Override
public int compare(Employee e1, Employee e2) {
return e1.getSalary() - e2.getSalary();
}

为什么需要返回0?
如果您查看 TreeMap 的源代码,您会看到以下内容:

final Entry<K,V> getEntry(Object key) {
// Offload comparator-based version for sake of performance
if (comparator != null)
return getEntryUsingComparator(key);
if (key == null)
throw new NullPointerException();
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
Entry<K,V> p = root;
while (p != null) {
int cmp = k.compareTo(p.key);
if (cmp < 0)
p = p.left;
else if (cmp > 0)
p = p.right;
else
return p; // <--Here
}
return null;
}

如果比较器永远不为 0,他将取消引用子分支,该子分支将为空。

旁注:您还可以使比较器发挥作用,如下所示:

Comparator<Employee> salaryComparator = (e1, e2) -> (e1.getSalary() - e2.getSalary());
TreeMap<Employee, String> trmap = new TreeMap<>(salaryComparator);

关于java - 使用比较器在 TreeMap 中获取 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46380071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com