【问题标题】:How is the compare() method called, without calling it in main method?compare() 方法是如何调用的,而不是在 main 方法中调用它?
【发布时间】:2016-04-01 08:27:45
【问题描述】:
import java.util.*;

class MyComp implements Comparator<String>{
    public int compare(String aStr, String bStr) {
        return bStr.compareTo(aStr);
    }
}


public class CustomComparatorTest {

    public static void main(String[] args) {

        MyComp my = new MyComp();
        TreeSet<String> ts = new TreeSet<String>(my);
        ts.add("C");
        ts.add("A");
        ts.add("B");
        ts.add("Y");
        ts.add("T");
        ts.add("W");

        for(String element : ts)
        {
            System.out.println(element + " ");
        }
        System.out.println();
    }
}

【问题讨论】:

  • 我不明白这个问题。你可以说得更详细点吗?看来您正在尝试使用字符串比较器来保持元素顺序,但 TreeSet 确实保持了它:)
  • 如果你在方法上放置一个断点并在你的调试器中运行它,你会看到它是如何以及为什么被调用的。

标签: java collections


【解决方案1】:

假设您询问如何执行 compare 方法而不在您的 main 方法中显式调用,则每次向其添加元素时都会由 TreeSet 代码调用它,以便在 @987654322 中定位位置@ 应该存储元素的位置。这样TreeSet 会根据您传递给其构造函数的Comparator 指定的顺序维护元素排序。

【讨论】:

  • 我问的是 compare() 方法是如何调用的,即使我们没有在 main 方法中调用它?
  • @VinayGuru 这就是我的回答。您正在调用add,这会导致调用compare
  • 请注意,单个add() 调用可能会导致compare 被多次调用。
【解决方案2】:
public boolean add(E e) {return m.put(e, PRESENT)==null;}

当你调用 add(x) 函数时,在 TreeSet 它调用 TreeMap.put() 方法,在 TreeMap.put() ,它将调用比较器。像这样的源代码:

public V put(K key, V value) {
        Entry<K,V> t = root;
        if (t == null) {
            compare(key, key); // type (and possibly null) check

            root = new Entry<>(key, value, null);
            size = 1;
            modCount++;
            return null;
        }
        int cmp;
        Entry<K,V> parent;
        // split comparator and comparable paths
        Comparator<? super K> cpr = comparator;
        if (cpr != null) {
            do {
                parent = t;
                cmp = cpr.compare(key, t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        else {
            if (key == null)
                throw new NullPointerException();
            Comparable<? super K> k = (Comparable<? super K>) key;
            do {
                parent = t;
                cmp = k.compareTo(t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        Entry<K,V> e = new Entry<>(key, value, parent);
        if (cmp < 0)
            parent.left = e;
        else
            parent.right = e;
        fixAfterInsertion(e);
        size++;
        modCount++;
        return null;
    }

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2012-09-28
    • 2019-03-24
    相关资源
    最近更新 更多