簡體   English   中英

為下一個參考添加一個額外的字段

[英]Adding a Extra Field for the next reference

我正在嘗試修改這個類以包含一個額外的字段以供下一個參考。 有任何想法嗎?

 package project2;

 public interface Entry<K,V> {
 K getKey();
 V getValue();
}

我正在嘗試修改 chainhashmap 以執行與哈希映射相同的行為。

不確定您到底要問什么,但這可能會有所幫助。

public final class Pair<L, R> {

    private L left;
    private R right;

    public Pair() {
        this(null, null);
    }

    public Pair(L initLeft, R initRight) {
        left = initLeft;
        right = initRight;
    }

    public synchronized L getLeft() {
        return left;
    }

    public synchronized R getRight() {
        return right;
    }

    public synchronized void setLeft(L newLeft) {
        left = newLeft;
    }

    public synchronized void setRight(R newRight) {
        right = newRight;
    }

    @Override
    public synchronized boolean equals(Object other) {
        if (other == null)
            return false;
        if (other instanceof Pair) {
            Pair<?, ?> otherPair = (Pair<?, ?>) other;
            return ((this.left == otherPair.left
                    || (this.left != null && otherPair.left != null && this.left.equals(otherPair.left)))
                    && (this.right == otherPair.right
                            || (this.right != null && otherPair.right != null && this.right.equals(otherPair.right))));
        }
        return false;
    }

    @Override
    public synchronized String toString() {
        return "(" + left.toString() + "," + right.toString() + ")";
    }

}

您可以使用Pair作為地圖條目中的值, left作為實際值, right作為您想要的next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM