Pair<L,R>Javaにない理由はありますか?このC ++構成に相当するものは何でしょうか?自分で実装することは避けたいです。
と思われる1.6は、似たような(提供されるAbstractMap.SimpleEntry<K,V>)が、このルックスはかなり回旋します。
Pair<L,R>Javaにない理由はありますか?このC ++構成に相当するものは何でしょうか?自分で実装することは避けたいです。
と思われる1.6は、似たような(提供されるAbstractMap.SimpleEntry<K,V>)が、このルックスはかなり回旋します。
回答:
で上糸comp.lang.java.help、ハンターGratznerはの存在に対するいくつかの引数与えPairJavaで構造物を。主な議論は、クラスPairが2つの値の関係についてのセマンティクスを伝えないということです(「最初」と「2番目」の意味をどのようにして知るのですか?)。
より良い方法は、Mikeが提案したような非常に単純なクラスを、クラスで作成したアプリケーションごとに作成することですPair。Map.Entry名前に意味があるペアの例です。
要約すると、私の意見では、クラスPosition(x,y)、クラスRange(begin,end)、およびクラスをEntry(key,value)用意する方が、Pair(first,second)何をすべきかについて何も教えてくれないジェネリックよりも優れています。
これはJavaです。説明的なクラス名とフィールド名を使用して、独自の調整済みPairクラスを作成する必要があります。hashCode()/ equals()を記述するか、Comparableを何度も実装することにより、ホイールを再発明することを気にしないでください。
SimpleImmutableEntry
                    HashMap互換ペアクラス:
public class Pair<A, B> {
    private A first;
    private B second;
    public Pair(A first, B second) {
        super();
        this.first = first;
        this.second = second;
    }
    public int hashCode() {
        int hashFirst = first != null ? first.hashCode() : 0;
        int hashSecond = second != null ? second.hashCode() : 0;
        return (hashFirst + hashSecond) * hashSecond + hashFirst;
    }
    public boolean equals(Object other) {
        if (other instanceof Pair) {
            Pair otherPair = (Pair) other;
            return 
            ((  this.first == otherPair.first ||
                ( this.first != null && otherPair.first != null &&
                  this.first.equals(otherPair.first))) &&
             (  this.second == otherPair.second ||
                ( this.second != null && otherPair.second != null &&
                  this.second.equals(otherPair.second))) );
        }
        return false;
    }
    public String toString()
    { 
           return "(" + first + ", " + second + ")"; 
    }
    public A getFirst() {
        return first;
    }
    public void setFirst(A first) {
        this.first = first;
    }
    public B getSecond() {
        return second;
    }
    public void setSecond(B second) {
        this.second = second;
    }
}
              super()取り出した場合の動作はまったく同じです。それがここにあるように、それがオプションであるなら、私は通常それを捨てます。
                    私が思いつくことができる最短のペアは、ロンボクを使用した次のものです:
@Data
@AllArgsConstructor(staticName = "of")
public class Pair<F, S> {
    private F first;
    private S second;
}
これは、のすべての利点がある@arturhから答えを、それが持っている(比較を除く)hashCode、equals、toStringおよび静的な「コンストラクタ」。
Apache Commons Lang 3.0+には、いくつかのペアクラスがあります:http : //commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/tuple/package-summary.html
ペアリングを実装する別の方法。
単純なファクトリなので、型を指定する必要はありません。例Pair.of( "hello"、1);
public class Pair<FIRST, SECOND> implements Comparable<Pair<FIRST, SECOND>> {
    public final FIRST first;
    public final SECOND second;
    private Pair(FIRST first, SECOND second) {
        this.first = first;
        this.second = second;
    }
    public static <FIRST, SECOND> Pair<FIRST, SECOND> of(FIRST first,
            SECOND second) {
        return new Pair<FIRST, SECOND>(first, second);
    }
    @Override
    public int compareTo(Pair<FIRST, SECOND> o) {
        int cmp = compare(first, o.first);
        return cmp == 0 ? compare(second, o.second) : cmp;
    }
    // todo move this to a helper class.
    private static int compare(Object o1, Object o2) {
        return o1 == null ? o2 == null ? 0 : -1 : o2 == null ? +1
                : ((Comparable) o1).compareTo(o2);
    }
    @Override
    public int hashCode() {
        return 31 * hashcode(first) + hashcode(second);
    }
    // todo move this to a helper class.
    private static int hashcode(Object o) {
        return o == null ? 0 : o.hashCode();
    }
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Pair))
            return false;
        if (this == obj)
            return true;
        return equal(first, ((Pair) obj).first)
                && equal(second, ((Pair) obj).second);
    }
    // todo move this to a helper class.
    private boolean equal(Object o1, Object o2) {
        return o1 == null ? o2 == null : (o1 == o2 || o1.equals(o2));
    }
    @Override
    public String toString() {
        return "(" + first + ", " + second + ')';
    }
}ofです。Google Guavaの不変のコレクションを思い出させます。
                    o1しComparableていますが、実際にそのインターフェースを実装することを示すものはありません。それが要件である場合、FIRSTtypeパラメータはにする必要がありますFIRST extends Comparable<?>。
                    http://www.javatuples.org/index.htmlはどうですか私はそれが非常に有用であることが分かってきました。
javatuplesは、1から10要素までのタプルクラスを提供します。
Unit<A> (1 element)
Pair<A,B> (2 elements)
Triplet<A,B,C> (3 elements)
Quartet<A,B,C,D> (4 elements)
Quintet<A,B,C,D,E> (5 elements)
Sextet<A,B,C,D,E,F> (6 elements)
Septet<A,B,C,D,E,F,G> (7 elements)
Octet<A,B,C,D,E,F,G,H> (8 elements)
Ennead<A,B,C,D,E,F,G,H,I> (9 elements)
Decade<A,B,C,D,E,F,G,H,I,J> (10 elements)
              PairおりTriplet、50年に1回程度使用することを想像できました。次に、Lombokを使用して、ペアが必要になるたびに小さな4行クラスを作成します。したがって、「10が多すぎる」は正確です。
                    Bottom (0 element)クラスが必要ですか?:)
                    何に使いたいかによります。これを行う一般的な理由は、マップを反復処理することです。これを行うには、これを行うだけです(Java 5以降)。
Map<String, Object> map = ... ; // just an example
for (Map.Entry<String, Object> entry : map.entrySet()) {
  System.out.printf("%s -> %s\n", entry.getKey(), entry.getValue());
}
              androidはPairクラスを提供します(http://developer.android.com/reference/android/util/Pair.html)、ここでは実装:
public class Pair<F, S> {
    public final F first;
    public final S second;
    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Pair)) {
            return false;
        }
        Pair<?, ?> p = (Pair<?, ?>) o;
        return Objects.equal(p.first, first) && Objects.equal(p.second, second);
    }
    @Override
    public int hashCode() {
        return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
    }
    public static <A, B> Pair <A, B> create(A a, B b) {
        return new Pair<A, B>(a, b);
    }
}
              Objects.equal(..)Guavaライブラリが必要です。
                    Objects.equals(...)2011年(1.7)以降にJavaで使用されているものに変更します。
                    最大の問題はおそらく、AとBの不変性を保証できないことです(「型パラメーターが不変であることを確認する方法」を参照)。たとえば、コレクションに挿入された後hashCode()、同じペアに対して一貫性のない結果が生じる可能性があります(これにより、未定義の動作が発生します) 、可変フィールドの観点から等しいの定義を参照)。特定の(汎用ではない)Pairクラスの場合、プログラマーは、AとBを不変に注意深く選択することにより、不変性を確保できます。
とにかく、@ PeterLawreyの回答(java 1.7)からジェネリックの警告をクリアします。
public class Pair<A extends Comparable<? super A>,
                    B extends Comparable<? super B>>
        implements Comparable<Pair<A, B>> {
    public final A first;
    public final B second;
    private Pair(A first, B second) {
        this.first = first;
        this.second = second;
    }
    public static <A extends Comparable<? super A>,
                    B extends Comparable<? super B>>
            Pair<A, B> of(A first, B second) {
        return new Pair<A, B>(first, second);
    }
    @Override
    public int compareTo(Pair<A, B> o) {
        int cmp = o == null ? 1 : (this.first).compareTo(o.first);
        return cmp == 0 ? (this.second).compareTo(o.second) : cmp;
    }
    @Override
    public int hashCode() {
        return 31 * hashcode(first) + hashcode(second);
    }
    // TODO : move this to a helper class.
    private static int hashcode(Object o) {
        return o == null ? 0 : o.hashCode();
    }
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Pair))
            return false;
        if (this == obj)
            return true;
        return equal(first, ((Pair<?, ?>) obj).first)
                && equal(second, ((Pair<?, ?>) obj).second);
    }
    // TODO : move this to a helper class.
    private boolean equal(Object o1, Object o2) {
        return o1 == o2 || (o1 != null && o1.equals(o2));
    }
    @Override
    public String toString() {
        return "(" + first + ", " + second + ')';
    }
}
追加/修正は大歓迎です:)特に私は私の使用についてはよくわかりません Pair<?, ?>。
この構文の詳細については、オブジェクトがComparableを実装することを確認するおよび詳細な説明については、Javaでジェネリック関数を実装する方法を参照してください。max(Comparable a, Comparable b)
私の意見では、Javaにペアはありません。ペアに直接追加機能を追加したい場合(たとえば、比較可能)、型をバインドする必要があるためです。C ++では気にしません。ペアを構成する型にがない場合はoperator <、pair::operator <もコンパイルされません。
境界のないComparableの例:
public class Pair<F, S> implements Comparable<Pair<? extends F, ? extends S>> {
    public final F first;
    public final S second;
    /* ... */
    public int compareTo(Pair<? extends F, ? extends S> that) {
        int cf = compare(first, that.first);
        return cf == 0 ? compare(second, that.second) : cf;
    }
    //Why null is decided to be less than everything?
    private static int compare(Object l, Object r) {
        if (l == null) {
            return r == null ? 0 : -1;
        } else {
            return r == null ? 1 : ((Comparable) (l)).compareTo(r);
        }
    }
}
/* ... */
Pair<Thread, HashMap<String, Integer>> a = /* ... */;
Pair<Thread, HashMap<String, Integer>> b = /* ... */;
//Runtime error here instead of compile error!
System.out.println(a.compareTo(b));
型引数が比較可能かどうかをコンパイル時にチェックするComparableの例:
public class Pair<
        F extends Comparable<? super F>, 
        S extends Comparable<? super S>
> implements Comparable<Pair<? extends F, ? extends S>> {
    public final F first;
    public final S second;
    /* ... */
    public int compareTo(Pair<? extends F, ? extends S> that) {
        int cf = compare(first, that.first);
        return cf == 0 ? compare(second, that.second) : cf;
    }
    //Why null is decided to be less than everything?
    private static <
            T extends Comparable<? super T>
    > int compare(T l, T r) {
        if (l == null) {
            return r == null ? 0 : -1;
        } else {
            return r == null ? 1 : l.compareTo(r);
        }
    }
}
/* ... */
//Will not compile because Thread is not Comparable<? super Thread>
Pair<Thread, HashMap<String, Integer>> a = /* ... */;
Pair<Thread, HashMap<String, Integer>> b = /* ... */;
System.out.println(a.compareTo(b));
これは良いことですが、今回は比較できない型をペアの型引数として使用することはできません。いくつかのユーティリティクラスでペア用のコンパレータをたくさん使用するかもしれませんが、C ++の人々はそれを取得しないかもしれません。別の方法は、型引数の境界が異なる型階層に多くのクラスを書き込むことですが、境界とそれらの組み合わせが多すぎます...
JavaFX(Java 8にバンドルされている)にはPair <A、B>クラスがあります
hashCode()。Java自体はこのメソッドを呼び出さないことに注意してください。ライブラリを含むユーザーコード用です。
                    他の多くの人がすでに述べたように、Pairクラスが役立つかどうかはユースケースに本当に依存しています。
プライベートヘルパー関数の場合、コードを読みやすくし、すべてのボイラープレートコードでさらに別の値クラスを作成する労力に値しない場合、Pairクラスを使用することは完全に正当だと思います。
一方、抽象化レベルで2つのオブジェクトまたは値を含むクラスのセマンティクスを明確に文書化する必要がある場合は、そのためのクラスを作成する必要があります。通常、データがビジネスオブジェクトの場合です。
いつものように、熟練した判断が必要です。
2番目の質問については、Apache CommonsライブラリのPairクラスをお勧めします。これらは、Javaの拡張標準ライブラリと見なされます。
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/tuple/Pair.html
また、ビジネスオブジェクトの値クラスの記述を簡略化するApache CommonsのEqualsBuilder、HashCodeBuilder、およびToStringBuilderを確認することもできます。
javafxユーティリティクラスを使用できます。Pairこれは、c ++でペア<>と同じ目的を果たします。https://docs.oracle.com/javafx/2/api/javafx/util/Pair.html
良いニュース JavaFXにはキーバリューペアがあります。
依存関係としてjavafxを追加してインポートするだけです javafx.util.Pairです。
と単純に使用しc++ます。
Pair <Key, Value> 
例えば
Pair <Integer, Integer> pr = new Pair<Integer, Integer>()
pr.get(key);// will return corresponding value
              Java言語の性質によると、私は人々が実際にはを必要としないと思いPairますが、インターフェースは通常彼らが必要とするものです。次に例を示します。
interface Pair<L, R> {
    public L getL();
    public R getR();
}
したがって、2つの値を返したい場合は、次のようにできます。
... //Calcuate the return value
final Integer v1 = result1;
final String v2 = result2;
return new Pair<Integer, String>(){
    Integer getL(){ return v1; }
    String getR(){ return v2; }
}
これはかなり軽量なソリューションであり、「aのセマンティクスは何Pair<L,R>ですか?」という質問に答えます。答えは、これは2つの(異なる場合がある)タイプで構築されたインターフェースであり、それぞれを返すメソッドを持っているということです。さらにセマンティックを追加するのはあなた次第です。たとえば、Positionを使用していて、コードでそれを示す必要がある場合は、を定義しPositionX、PositionYそれを含むことでInteger、を構成できますPair<PositionX,PositionY>。JSR 308が利用可能な場合は、Pair<@PositionX Integer, @PositionY Ingeger>を簡略化するためにする。
編集:ここで指摘すべきことの1つは、上記の定義が型パラメーター名とメソッド名を明示的に関連付けることです。これはa Pairが意味情報の欠如であると主張する人々への回答です。実際には、メソッドgetLは「型パラメーターLの型に対応する要素をくれ」という意味であり、それは何かを意味します。
編集:生活を楽にするシンプルなユーティリティクラスを次に示します。
class Pairs {
    static <L,R> Pair<L,R> makePair(final L l, final R r){
        return new Pair<L,R>(){
            public L getL() { return l; }
            public R getR() { return r; }   
        };
    }
}
使用法:
return Pairs.makePair(new Integer(100), "123");
              equals、hashCodeおよびtoString?
                    toStringするには、2つのフィールド間の関係についてさらに知識が必要です。
                    class、単なるa よりも優れているかもしれないというinterfaceことです。
                    構文的には似ていますが、JavaとC ++には非常に異なるパラダイムがあります。JavaのようにC ++を書くのは悪いC ++であり、C ++のようにJavaを書くのは悪いJavaです。
EclipseのようなリフレクションベースのIDEを使用すると、「ペア」クラスの機能を作成することが迅速かつ簡単になります。クラスを作成し、2つのフィールドを定義し、さまざまな「XXを生成」メニューオプションを使用して、ほんの数秒でクラスに入力します。Comparableインターフェースが必要な場合は、「compareTo」をすばやく入力する必要があるかもしれません。
言語の個別の宣言/定義オプションを使用すると、C ++コードジェネレーターはあまり良くないので、小さなユーティリティクラスを手書きで書くのは時間のかかる退屈な作業です。ペアはテンプレートであるため、使用しない関数に料金を支払う必要はありません。typedef機能を使用すると、コードに意味のある型名を割り当てることができるため、「セマンティクスがない」という異論は実際には成り立ちません。
ペアは良いものであり、複雑なジェネリックの基本的な構築単位になるでしょう。たとえば、これは私のコードからです:
WeakHashMap<Pair<String, String>, String> map = ...
Haskellのタプルと同じです
Pairは、特別なセマンティクスがないため、最適です。明確な概念に対して明確な名前を付けることは良いことですが、「1番目」と「2番目」がうまく機能する名前を探すのはよくありません。
                    Javaのようなプログラミング言語の場合、ほとんどのプログラマーがデータ構造のようなペアを表すために使用する代替データ構造は2つの配列であり、データは同じインデックスを介してアクセスされます
例:http : //www-igm.univ-mlv.fr/~lecroq/string/node8.html#SECTION0080
データを結合する必要があるため、これは理想的ではありませんが、かなり安価であることがわかります。また、ユースケースで座標の格納が必要な場合は、独自のデータ構造を構築することをお勧めします。
私のライブラリにはこのようなものがあります
public class Pair<First,Second>{.. }
              以下は、利便性のために複数のタプルを持つライブラリの一部です。
他のライブラリは少なくともPairタプルを含むと述べられています。
特に、公認の型付けではなく(受け入れられた回答で推奨されているように)構造型の多くを利用する関数型プログラミングのコンテキストでは、これらのライブラリーとそのタプルは非常に便利です。
私はすべてのペアの実装がここに散らばっていることに気づきました。これは、2つの値の順序に意味を与えます。ペアを考えるとき、2つのアイテムの組み合わせを考えます。2つのアイテムの順序は重要ではありません。ここで、順序付けられていないペアの私の実装だhashCodeとequalsコレクションに目的の動作を保証するためにオーバーライド。クローンも可能です。
/**
 * The class <code>Pair</code> models a container for two objects wherein the
 * object order is of no consequence for equality and hashing. An example of
 * using Pair would be as the return type for a method that needs to return two
 * related objects. Another good use is as entries in a Set or keys in a Map
 * when only the unordered combination of two objects is of interest.<p>
 * The term "object" as being a one of a Pair can be loosely interpreted. A
 * Pair may have one or two <code>null</code> entries as values. Both values
 * may also be the same object.<p>
 * Mind that the order of the type parameters T and U is of no importance. A
 * Pair<T, U> can still return <code>true</code> for method <code>equals</code>
 * called with a Pair<U, T> argument.<p>
 * Instances of this class are immutable, but the provided values might not be.
 * This means the consistency of equality checks and the hash code is only as
 * strong as that of the value types.<p>
 */
public class Pair<T, U> implements Cloneable {
    /**
     * One of the two values, for the declared type T.
     */
    private final T object1;
    /**
     * One of the two values, for the declared type U.
     */
    private final U object2;
    private final boolean object1Null;
    private final boolean object2Null;
    private final boolean dualNull;
    /**
     * Constructs a new <code>Pair<T, U></code> with T object1 and U object2 as
     * its values. The order of the arguments is of no consequence. One or both of
     * the values may be <code>null</code> and both values may be the same object.
     *
     * @param object1 T to serve as one value.
     * @param object2 U to serve as the other value.
     */
    public Pair(T object1, U object2) {
        this.object1 = object1;
        this.object2 = object2;
        object1Null = object1 == null;
        object2Null = object2 == null;
        dualNull = object1Null && object2Null;
    }
    /**
     * Gets the value of this Pair provided as the first argument in the constructor.
     *
     * @return a value of this Pair.
     */
    public T getObject1() {
        return object1;
    }
    /**
     * Gets the value of this Pair provided as the second argument in the constructor.
     *
     * @return a value of this Pair.
     */
    public U getObject2() {
        return object2;
    }
    /**
     * Returns a shallow copy of this Pair. The returned Pair is a new instance
     * created with the same values as this Pair. The values themselves are not
     * cloned.
     *
     * @return a clone of this Pair.
     */
    @Override
    public Pair<T, U> clone() {
        return new Pair<T, U>(object1, object2);
    }
    /**
     * Indicates whether some other object is "equal" to this one.
     * This Pair is considered equal to the object if and only if
     * <ul>
     * <li>the Object argument is not null,
     * <li>the Object argument has a runtime type Pair or a subclass,
     * </ul>
     * AND
     * <ul>
     * <li>the Object argument refers to this pair
     * <li>OR this pair's values are both null and the other pair's values are both null
     * <li>OR this pair has one null value and the other pair has one null value and
     * the remaining non-null values of both pairs are equal
     * <li>OR both pairs have no null values and have value tuples <v1, v2> of
     * this pair and <o1, o2> of the other pair so that at least one of the
     * following statements is true:
     * <ul>
     * <li>v1 equals o1 and v2 equals o2
     * <li>v1 equals o2 and v2 equals o1
     * </ul>
     * </ul>
     * In any other case (such as when this pair has two null parts but the other
     * only one) this method returns false.<p>
     * The type parameters that were used for the other pair are of no importance.
     * A Pair<T, U> can return <code>true</code> for equality testing with
     * a Pair<T, V> even if V is neither a super- nor subtype of U, should
     * the the value equality checks be positive or the U and V type values
     * are both <code>null</code>. Type erasure for parameter types at compile
     * time means that type checks are delegated to calls of the <code>equals</code>
     * methods on the values themselves.
     *
     * @param obj the reference object with which to compare.
     * @return true if the object is a Pair equal to this one.
     */
    @Override
    public boolean equals(Object obj) {
        if(obj == null)
            return false;
        if(this == obj)
            return true;
        if(!(obj instanceof Pair<?, ?>))
            return false;
        final Pair<?, ?> otherPair = (Pair<?, ?>)obj;
        if(dualNull)
            return otherPair.dualNull;
        //After this we're sure at least one part in this is not null
        if(otherPair.dualNull)
            return false;
        //After this we're sure at least one part in obj is not null
        if(object1Null) {
            if(otherPair.object1Null) //Yes: this and other both have non-null part2
                return object2.equals(otherPair.object2);
            else if(otherPair.object2Null) //Yes: this has non-null part2, other has non-null part1
                return object2.equals(otherPair.object1);
            else //Remaining case: other has no non-null parts
                return false;
        } else if(object2Null) {
            if(otherPair.object2Null) //Yes: this and other both have non-null part1
                return object1.equals(otherPair.object1);
            else if(otherPair.object1Null) //Yes: this has non-null part1, other has non-null part2
                return object1.equals(otherPair.object2);
            else //Remaining case: other has no non-null parts
                return false;
        } else {
            //Transitive and symmetric requirements of equals will make sure
            //checking the following cases are sufficient
            if(object1.equals(otherPair.object1))
                return object2.equals(otherPair.object2);
            else if(object1.equals(otherPair.object2))
                return object2.equals(otherPair.object1);
            else
                return false;
        }
    }
    /**
     * Returns a hash code value for the pair. This is calculated as the sum
     * of the hash codes for the two values, wherein a value that is <code>null</code>
     * contributes 0 to the sum. This implementation adheres to the contract for
     * <code>hashCode()</code> as specified for <code>Object()</code>. The returned
     * value hash code consistently remain the same for multiple invocations
     * during an execution of a Java application, unless at least one of the pair
     * values has its hash code changed. That would imply information used for 
     * equals in the changed value(s) has also changed, which would carry that
     * change onto this class' <code>equals</code> implementation.
     *
     * @return a hash code for this Pair.
     */
    @Override
    public int hashCode() {
        int hashCode = object1Null ? 0 : object1.hashCode();
        hashCode += (object2Null ? 0 : object2.hashCode());
        return hashCode;
    }
}
この実装は適切にユニットテストされており、セットとマップでの使用が試されています。
これをパブリックドメインでリリースすることを主張していないことに注意してください。これは、アプリケーションで使用するために記述したばかりのコードです。そのため、これを使用する場合は、直接コピーを作成しないでください。コメントや名前を少し変更してください。私のドリフトをキャッチ?
非常にシンプルで使いやすいバージョンが必要な場合は、https://github.com/lfac-pt/Java-Pairで入手できるようにしました。また、改善は大歓迎です!
com.sun.tools.javac.util.Pairは、ペアの単純な実装です。jdk1.7.0_51 \ lib \ tools.jarにあります。
org.apache.commons.lang3.tuple.Pair以外は、単なるインターフェースではありません。
public class Pair<K, V> {
    private final K element0;
    private final V element1;
    public static <K, V> Pair<K, V> createPair(K key, V value) {
        return new Pair<K, V>(key, value);
    }
    public Pair(K element0, V element1) {
        this.element0 = element0;
        this.element1 = element1;
    }
    public K getElement0() {
        return element0;
    }
    public V getElement1() {
        return element1;
    }
}
使用法 :
Pair<Integer, String> pair = Pair.createPair(1, "test");
pair.getElement0();
pair.getElement1();
不変、ペアのみ!
Pair<Object, Object> pair = Pair.createPair("abc", "def")ただしPair.createPair((Object)"abc", (Object)"def")、コードで記述する必要があると思いますか?
                    @SuppressWarnings("unchecked") 	public static <K, V, X, Y> Pair<X, Y> createPair(K key, V value) { 		return new Pair<X, Y>((X) key, (Y) value); 	}  しかし、それが良い習慣であるかどうかはわかりません
                    new Pair<Object, Object>("abc", "def")私の実験では、トラディショナルが最も信頼できました。
                    
AbstractMap.SimpleEntry複雑なのですか?