简体   繁体   English

如何在Java中使用Treemap计算对对象

[英]How to count pair objects using treemap in java

I was trying to have a simple String Pair objects to be counted using a TreeMap. 我试图使用TreeMap对一个简单的String Pair对象进行计数。

What I've tried is using this comparator in a treemap, but it still gives me the wrong output as if it compares the pair object to itself: 我尝试过的是在树形图中使用此比较器,但是它仍然给我错误的输出,就好像它将对对象与其自身进行比较一样:

static class Comp implements Comparator<Pair>
{   
    public int compare(Pair s1, Pair s2)
    {   
        boolean c1 = s1.word1.equals(s2.word1),
               c2 = s1.word2.equals(s2.word2),
               c3 = s1.word1.equals(s2.word2),
               c4 = s1.word2.equals(s2.word1);

        if(c1 && c2 || c3 && c4)
            return 0;   
        else if((!c1 && !c2) || (!c3 && !c4))
            return s1.word1.compareTo(s2.word1);
        if(!c1)
            return s1.word1.compareTo(s2.word1);
        else if(!c2)
            return s1.word2.compareTo(s2.word2);
        else if(!c3)
            return s1.word1.compareTo(s2.word2);
        else if(!c4)
            return s1.word2.compareTo(s2.word1);    
        else
            return s1.word1.compareTo(s2.word1);
    }
}

Example input would be: 输入示例为:

<"red", "blue"> , <"blue", "red">, <"red", "red">, <"gray","purple">, <"purple","gray">  

should output: 应该输出:

<"gray", "purple", 2>, <"red", "blue", 2> <"red", "red", 1>

instead i get: 相反,我得到:

<"gray", "purple", 1><"red", "blue", 2> <"red", "red", 1><"purple", "gray", 1>

I'm not sure how you are coding your counting, but the sorting works as expected with what you've shown of your code. 我不确定您如何编码计数,但是排序与您显示的代码一样可以正常工作。 The only difference I've seen between the code below and your code is that the Comp class is not static in mine. 我在下面的代码和您的代码之间看到的唯一区别是Comp类在我自己中不是静态的。

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

public class ComparatorTest {

    public ComparatorTest() {
        Map<Pair, String> x = new TreeMap<>(new Comp());
        x.put(new Pair("red", "blue"), "one");
        x.put(new Pair("blue", "red"), "two");
        x.put(new Pair("red", "red"), "three");
        x.put(new Pair("gray", "purple"), "four");
        x.put(new Pair("purple", "gray"), "five");

        System.out.println(String.format("Print out Map - %s", x.toString()));

        for (Pair pair : x.keySet()) {
            System.out.println(String.format("Key value <%s, %s, %s>", pair.word1, pair.word2, x.get(pair)));
        }
    }

    public static void main(String[] args) {
        new ComparatorTest();
    }
}

class Pair {
    String word1 = null;
    String word2 = null;

    public Pair(String word1, String word2) {
        this.word1 = word1;
        this.word2 = word2;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Pair)) { return false; }

        Pair that = (Pair)obj;

        return ((this.word1.equals(that.word1) && this.word2.equals(that.word2)) || (this.word1.equals(that.word2) && this.word2.equals(that.word1)));
    }

    @Override
    public String toString() {
        return String.format("<%s, %s>", word1, word2);
    }
}

class Comp implements Comparator<Pair> {   
    public int compare(Pair s1, Pair s2) {   
        boolean c1 = s1.word1.equals(s2.word1),
               c2 = s1.word2.equals(s2.word2),
               c3 = s1.word1.equals(s2.word2),
               c4 = s1.word2.equals(s2.word1);

        if(c1 && c2 || c3 && c4)
            return 0;   
        else if((!c1 && !c2) || (!c3 && !c4))
            return s1.word1.compareTo(s2.word1);
        if(!c1)
            return s1.word1.compareTo(s2.word1);
        else if(!c2)
            return s1.word2.compareTo(s2.word2);
        else if(!c3)
            return s1.word1.compareTo(s2.word2);
        else if(!c4)
            return s1.word2.compareTo(s2.word1);    
        else
            return s1.word1.compareTo(s2.word1);
    }
}

Output 输出量

Print out Map - {<gray, purple>=five, <red, blue>=two, <red, red>=three}
Key value <gray, purple, five>
Key value <red, blue, two>
Key value <red, red, three>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM