簡體   English   中英

在Java中將三元組映射到int的最佳方法

[英]Best way to map a triplet to an int in Java

我想將Triplets映射為一個Int,如下所示:

(12,6,6) -> 1
(1,0,6)  -> 1
(2,3,7)  -> 0

我需要能夠訪問Int以及三元組中的每個單獨的值。

在Java中最有效的方法是什么?

謝謝

Java 沒有內置的表示元組的方法。

但是您可以輕松地在您的一個上創建一個。 看看這個簡單的通用Triple類:

public class Triple<A, B, C> {
    private final A mFirst;
    private final B mSecond;
    private final C mThird;

    public Triple(final A first, final B second, final C third) {
        this.mFirst = first;
        this.mSecond = second;
        this.mThird = third;
    }

    public A getFirst() {
        return this.mFirst;
    }

    public B getSecond() {
        return this.mSecond;
    }

    public C getThird() {
        return this.mThird;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + this.mFirst.hashCode();
        result = prime * result + this.mSecond.hashCode();
        result = prime * result + this.mThird.hashCode();
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Triple other = (Triple) obj;
        if (this.mFirst == null) {
            if (other.mFirst != null) {
                return false;
            }
        } else if (!this.mFirst.equals(other.mFirst)) {
            return false;
        }
        if (this.mSecond == null) {
            if (other.mSecond != null) {
                return false;
            }
        } else if (!this.mSecond.equals(other.mSecond)) {
            return false;
        }
        if (this.mThird == null) {
            if (other.mThird != null) {
                return false;
            }
        } else if (!this.mThird.equals(other.mThird)) {
            return false;
        }
        return true;
    }
}

該類僅包含三個值並提供getter 此外,它通過比較所有三個值來覆蓋equalshashCode

不要擔心equalshashCode是如何實現的。 它們是由IDE生成的(大多數IDE都可以做到這一點)。


然后,您可以使用如下所示的Map創建映射:

Map<Triple<Integer, Integer, Integer>, Integer> map = new HashMap<>();

map.put(new Triple<>(12, 6, 6), 1);
map.put(new Triple<>(1, 0, 6), 1);
map.put(new Triple<>(2, 3, 7), 0);

並通過Map#get訪問它們:

Triple<Integer, Integer, Integer> key = ...
int value = map.get(key);

另外,您可以在Triple類中添加第四個值,例如id或類似的東西。 或改為建立一個Quadruple類。


為了方便起見,您還可以創建一個通用的工廠方法,如Triple#of ,並將其添加到Triple類:

public static <A, B, C> Triple<A, B, C> of(final A first,
        final B second, final C third) {
    return new Triple<>(first, second, third);
}

然后,您可以使用它來創建Triple稍微緊湊的實例。 比較兩種方法:

// Using constructor
new Triple<>(12, 6, 6);

// Using factory
Triple.of(12, 6, 6);

您可以使用org.apache.commons.lang3.tuple.Triple

HashMap<Triple<Integer, Integer, Integer>, Integer> tripletMap = new HashMap<>();
tripletMap.put(Triple.of(12, 6, 6), 1);

暫無
暫無

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

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