Java Comparator,Comparable和TreeSet.contains

时间:2016-02-02 13:58:36

标签: java comparator comparable treeset

编辑 - 下面稍微简化的示例(任何更简单,我不确定它是否包含所需行为的所有元素)

下面是一段代码片段,代表了我正在尝试使用Comparator做的一件事。我希望包含在第true次调用时返回doRSM

package comparisonTest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.TreeSet;

public class ComparisonTest {

    private static class ArbitraryItem {
        String node;
        ArbitraryItem(String node) {
            this.node = node;
        }
        public String getNode() {
            return node;
        }
    }

    private static final Map<String, ResultSet> idToArbitraryItems = new HashMap<>();


    private static class ArbitraryItemComparable implements Comparable<ArbitraryItemComparable> {
        ArbitraryItem item;
        Comparator<ArbitraryItemComparable> c;
        ArbitraryItemComparable(ArbitraryItem item, Comparator<ArbitraryItemComparable> c) {
            this.item = item;
            this.c = c;
        }
        @Override
        public boolean equals(Object o) {
            if (!(o instanceof ArbitraryItemComparable)) {
                System.out.println("not a ArbitraryItemComparable="+o);
                return false;
            }
            boolean eq = this.c.compare(this, ((ArbitraryItemComparable)o)) == 0;
            System.out.println("   equality val="+eq);
            return eq;
        }
        @Override
        public int compareTo(ArbitraryItemComparable o) {
            int compVal = this.c.compare(this, ((ArbitraryItemComparable)o));
            System.out.println("   comparison val="+compVal);
            return compVal;
        }

    }

    private static class ResultSet {
        SortedSet<ArbitraryItemComparable> usedResults;
        String node;
        Comparator<ArbitraryItemComparable> comparator;
        ResultSet(String node, Comparator<ArbitraryItemComparable> comparator) {
            this.usedResults = new TreeSet<ArbitraryItemComparable>();
            this.node = node;
            this.comparator = comparator;
        }
    }

    static private void doRSM(Collection <ArbitraryItem> foobar, ResultSet set, int max, boolean close, String id) {
        Iterator<ArbitraryItem> items = foobar.iterator();
        for (;items.hasNext();) {
            ArbitraryItem item = (ArbitraryItem) items.next();
            ArbitraryItemComparable itemComparable = new ArbitraryItemComparable(item, set.comparator);
            System.out.println("*** looking at node "+itemComparable.item.getNode()+"***, sur size="+set.usedResults.size());
            if (!set.usedResults.contains(itemComparable)); {
                System.out.println("*** node "+itemComparable.item.getNode()+" not in usedResults");
            }
            set.usedResults.add(itemComparable);
        }
    }

    public static void main(String [] args)
    {
        Collection<ArbitraryItem> items = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            items.add(new ArbitraryItem(""+i));
        }

        Comparator<ArbitraryItemComparable> comparator = new Comparator<ArbitraryItemComparable>() {
            @Override
            public int compare(ArbitraryItemComparable o1, ArbitraryItemComparable o2) {
                // this is where the magic needs to happen!!
                System.out.println("calling compare: o1 node="+o1.item.getNode()+" "+o1.item.getNode().hashCode()+" o2 node="+o2.item.getNode()+" "+o2.item.getNode().hashCode());
                return o1.item.getNode().hashCode() - o2.item.getNode().hashCode();
            }
            @Override
            public boolean equals(Object o) {
                System.out.println("why is this called?");
                return false;
            }
        };
        ResultSet set = new ResultSet("3", comparator);
        idToArbitraryItems.put("q", set);
        doRSM(items, set, 1000, false, "q");
        doRSM(items, set, 1000, false, "q");
    }
}

但是,日志显示了这个:

*** looking at node 0***, sur size=0
*** node 0 not in usedResults
calling compare: o1 node=0 48 o2 node=0 48
   comparison val=0
*** looking at node 1***, sur size=1
calling compare: o1 node=1 49 o2 node=0 48
   comparison val=1
*** node 1 not in usedResults
calling compare: o1 node=1 49 o2 node=0 48
   comparison val=1
*** looking at node 2***, sur size=2
calling compare: o1 node=2 50 o2 node=0 48
   comparison val=2
calling compare: o1 node=2 50 o2 node=1 49
   comparison val=1
*** node 2 not in usedResults
calling compare: o1 node=2 50 o2 node=0 48
   comparison val=2
calling compare: o1 node=2 50 o2 node=1 49
   comparison val=1
*** looking at node 0***, sur size=3
calling compare: o1 node=0 48 o2 node=1 49
   comparison val=-1
calling compare: o1 node=0 48 o2 node=0 48
   comparison val=0
*** node 0 not in usedResults
calling compare: o1 node=0 48 o2 node=1 49
   comparison val=-1
calling compare: o1 node=0 48 o2 node=0 48
   comparison val=0
*** looking at node 1***, sur size=3
calling compare: o1 node=1 49 o2 node=1 49
   comparison val=0
*** node 1 not in usedResults
calling compare: o1 node=1 49 o2 node=1 49
   comparison val=0
*** looking at node 2***, sur size=3
calling compare: o1 node=2 50 o2 node=1 49
   comparison val=1
calling compare: o1 node=2 50 o2 node=2 50
   comparison val=0
*** node 2 not in usedResults
calling compare: o1 node=2 50 o2 node=1 49
   comparison val=1
calling compare: o1 node=2 50 o2 node=2 50
   comparison val=0

违规行从这里开始:

*** looking at node 0***, sur size=3
calling compare: o1 node=0 48 o2 node=1 49
   comparison val=-1
calling compare: o1 node=0 48 o2 node=0 48
   comparison val=0
*** node 0 not in usedResults

意味着比较值为0(这意味着它们是相等的)但仍然在调用添加代码,这意味着列表并不是说它包含值。为什么?我认为返回compareTo 0意味着平等?此外,为什么永远不会调用equalsTreeSet的{​​{1}}文档说明了:

  

如果此set包含指定的元素,则返回true。更正式地,当且仅当此集合包含元素e时才返回true(o == null?e == null:o.equals(e))。

这会让我相信contains会被召唤,但事实并非如此?关于如何让ArbitraryItemComparable.equals函数返回true的任何想法?

1 个答案:

答案 0 :(得分:3)

你有这个:

return (((vat_part(discount_percent, date, options).to_i + non_vat_part(discount_percent, date, options).to_i)*1.2).round(2)/1.2).round(rounded ? 2 : 1000)

这基本上意味着“忽略这个条件并无条件地执行以下块”。