小编Gau*_*lla的帖子

使用map.get(Object)时TreeMap返回null

使用get方法获取值时, TreeMap将值打印为null,而它正常工作HashMap().请在下面找到示例代码并为此提供输入.

它适用于Hashmap,因为它使用equals()/hashcode()方法,而TreeMap是SortedMap,它不使用equals方法来比较两个对象.相反,它使用比较器/可比较来比较对象,但在使用get方法获取对象时,它将null作为响应.请在此提供一些清晰度.

    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.TreeMap;

    class Employees implements Comparable<Employees>, Comparator<Employees> {

        public Employees(String name, int id) {
            super();
        this.name = name;
        this.id = id;
    }

    private String name;
    private int id;

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

java collections treemap

-1
推荐指数
1
解决办法
262
查看次数

标签 统计

collections ×1

java ×1

treemap ×1