在TreeMap中的Key上对自定义数据结构进行排序

问题描述 投票:1回答:3

我想在key上对TreeMap进行排序。 Key是一些具有int,List,String等的自定义DataStructure。我期望排序的成员有一些重复。假设成员是Rank。超过1个对象可以具有相同的排名。

简化版示例:

注意:在CompareTo方法中,0不会故意返回0而不是忽略重复。(如果这不是避免重复的正确方法,请纠正我)

import java.util.TreeMap;


public class TreeTest {

public static void main(String[] args) {
    TreeMap<Custom,String> t = new TreeMap<Custom,String>();
    Custom c1 = new Custom();
    c1.setName("a");
    c1.setRank(0);

    Custom c2 = new Custom();
    c2.setName("b");
    c2.setRank(1);

    Custom c3 = new Custom();
    c3.setName("c");
    c3.setRank(0);

    t.put(c1, "first");
    t.put(c2, "Second");
    t.put(c3, "Third");

    System.out.println(t.keySet());

    for(Custom c:t.keySet()){
        System.out.println(t.get(c));
    }
  }
}

和自定义对象

package com.example.ui;

 public class Custom implements Comparable<Custom>{

int rank;
String name;

public int getRank() {
    return rank;
}

public void setRank(int rank) {
    this.rank = rank;
}

public String getName() {
    return name;
}

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



@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + rank;
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Custom other = (Custom) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    if (rank != other.rank)
        return false;
    return true;
}

    // 0 is not returned intentionally to NOT ignore duplicates.
 public int compareTo(Custom o) {
    if(o.rank>this.rank)
        return 1;
    if(o.rank==this.rank)
        return -1;
    return -1;
 }
 }

输出::

[com.example.ui.Custom@fa0, com.example.ui.Custom@fbe, com.example.ui.Custom@f80]
null
null
null

预期:第一,第二,第三,分别基于等级0,1,0。

我在Google上看了几个例子。其中大多数是使用具有原始数据类型的键或值的TreeMap排序的基本用法,但是在排序成员时没有重复项是自定义键DataStructure的一部分。

请帮忙?

java sorting treemap
3个回答
7
投票

问题是你的compareTo的实现与equals不一致,这是TreeMap所要求的。来自API文档:

请注意,如果此有序映射要正确实现Map接口,则由有序映射维护的排序(无论是否提供显式比较器)必须与equals一致。

一种可能的一致实现方式是首先按等级进行比较,然后按等级进行比较,如果等级值相等。对于具有相同排名和相同名称的两个Custom实例,您不应期望将它们作为键存储在同一个Map中 - 这违反了Map的合同。

public int compareTo(Custom o) {
  int ret = this.rank - o.rank;

  // Equal rank so fall back to comparing by name.
  if (ret == 0) {
    ret = this.name.compareTo(o.name);
  }

  return ret;
}

0
投票

如前所述,你的equals和compareTo的实现彼此不一致。如果我正确地阅读了您的问题,您需要的是保留具有相同密钥的重复项。我建议你研究一下Google Guava系列的TreeMultimap。它为每个值对象创建集合容器,以保留具有相同键的不同值。例如

treeMultimap.put ("rank1", "Joe");
treeMultimap.put ("rank1", Jane");
treeMultimap.get ("rank1"); // Set("Joe","Jane");

该数据结构中的约束是K,V对必须是唯一的。也就是说,你不能在Multimap中插入两次(“rank 1”,“Jee”)两次。

一个重要的注意事项:为什么你看到这么多Map的例子,使用简单的类型,特别是字符串,是因为map中的键必须是不可变的。对象的equals和hashcode值在用作映射中的键时不得更改。翻译为您的示例,当它用作键时,您无法执行customObject.setRank(...)并更新排名值。为此,首先需要删除密钥及其值,更新密钥然后重新插入。


0
投票

您也可以通过将Comparator实现为匿名内部类型并覆盖compare()来返回所需的比较。

public class TreeMaps 
{
    public static void main(String[] args) 
    {
    Custom c1 = new Custom(1,"A");
    Custom c2 = new Custom(3,"C");
    Custom c3 = new Custom(2,"B");

    TreeMap<Custom , Integer > tree = new TreeMap<Custom, Integer>  (new Comparator<Custom>() {

                                            @Override
                                            public int compare(Custom o1, Custom o2) {

                                                return o1.rank - o2.rank;
                                            }
                                        });
    tree.put(c1, 1);
    tree.put(c2, 2);
    tree.put(c3, 3);

    System.out.println(tree);
}
}

class Custom
{
int rank ; 
String name  ; 
public Custom(int rank , String name) {
    this.rank = rank ;
    this.name = name ;

}

@Override
public String toString()
{
    return "Custom[" + this.rank + "-" + this.name + "]" ;
}
}
© www.soinside.com 2019 - 2024. All rights reserved.