Domanda

C'è una buona ragione per cui non esiste Pair < L, R > in Java? Quale sarebbe l'equivalente di questo costrutto C ++? Preferirei evitare di reimplementare il mio.

Sembra che 1.6 stia fornendo qualcosa di simile ( AbstractMap.SimpleEntry < K, V > ), ma questo sembra abbastanza contorto.

È stato utile?

Soluzione

In una discussione su comp .lang.java.help , Hunter Gratzner fornisce alcuni argomenti contro la presenza di un costrutto Pair in Java. L'argomento principale è che una classe Pair non trasmette alcuna semantica sulla relazione tra i due valori (come fai a sapere cosa significa "primo" e "secondo"?).

Una pratica migliore è scrivere una classe molto semplice, come quella proposta da Mike, per ogni applicazione che avresti fatto della classe Pair . Map.Entry è un esempio di coppia che porta il suo significato nel suo nome.

Per riassumere, secondo me è meglio avere una classe Position (x, y) , una classe Range (inizio, fine) e una classe < code> Entry (chiave, valore) piuttosto che un Pair (primo, secondo) generico che non mi dice nulla su cosa dovrebbe fare.

Altri suggerimenti

Questo è Java. Devi creare la tua classe Pair su misura con classe descrittiva e nomi di campo e non ti preoccupare che reinventerai la ruota scrivendo hashCode () / equals () o implementando Comparable ancora e ancora.

Classe di coppia compatibile con 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;
    }
}

La coppia più breve che ho potuto inventare è la seguente, usando Lombok :

@Data
@AllArgsConstructor(staticName = "of")
public class Pair<F, S> {
    private F first;
    private S second;
}

Ha tutti i vantaggi di la risposta di @arturh (tranne la comparabilità), ha hashCode , è uguale a , toString e un "costruttore" statico.

Un altro modo per implementare Pair with.

  • Campi immutabili pubblici, ovvero struttura di dati semplice.
  • Paragonabile.
  • Hash semplice e uguale.
  • Factory semplice, quindi non è necessario fornire i tipi. per esempio. 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 + ')';
        }
    }
    

Che ne dici di http://www.javatuples.org/index.html ho trovato molto utile.

Il javatuples ti offre classi di tuple da uno a dieci elementi:

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)

Dipende da cosa vuoi usarlo. Il motivo tipico per farlo è iterare sulle mappe, per le quali lo fai semplicemente (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 fornisce la classe Pair ( http://developer.android.com /reference/android/util/Pair.html ), qui l'implementazione:

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);
    }
}

Il problema più grande è probabilmente che non si può garantire l'immutabilità su A e B (vedere Come assicurarsi che i parametri di tipo siano immutabili ) in modo che hashCode () possa dare risultati incoerenti per la stessa coppia dopo in una raccolta per esempio (questo darebbe un comportamento indefinito, vedi Definizione di uguali in termini di campi mutabili ). Per una particolare classe di coppie (non generiche) il programmatore può garantire l'immutabilità scegliendo accuratamente A e B come immutabili.

Ad ogni modo, cancellando gli avvertimenti del generico dalla risposta di @ 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 + ')';
    }
}

Aggiunte / correzioni molto gradite :) In particolare non sono del tutto sicuro del mio uso di Pair <?,? > .

Per ulteriori informazioni sul perché questa sintassi, vedere Assicurarsi che gli oggetti implementino Comparable e per una spiegazione dettagliata Come implementare una funzione generica max (Comparable a, Comparable b) in Java?

A mio avviso, non esiste una coppia in Java perché, se si desidera aggiungere funzionalità extra direttamente sulla coppia (ad es. Comparable), è necessario associare i tipi. In C ++, non ci interessa, e se i tipi che compongono una coppia non hanno operator < , anche pair :: operator < non verrà compilato.

Un esempio di Comparable senza limiti:

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));

Un esempio di Comparable con controllo in fase di compilazione per verificare se gli argomenti di tipo sono comparabili:

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));

Va ??bene, ma questa volta non puoi usare tipi non confrontabili come argomenti di tipo in Pair. Uno può usare molti Comparatori per Pair in alcune classi di utilità, ma le persone C ++ potrebbero non ottenerlo. Un altro modo è scrivere molte classi in una gerarchia di tipi con limiti diversi sugli argomenti di tipo, ma ci sono troppi limiti possibili e le loro combinazioni ...

JavaFX (che viene fornito in bundle con Java 8) ha la coppia < A, B > Classe

Come molti altri hanno già affermato, dipende davvero dal caso d'uso se una classe Pair sia utile o meno.

Penso che per una funzione di aiuto privato sia assolutamente legittimo usare una classe Pair se questo rende il tuo codice più leggibile e non vale la pena di creare un'altra classe di valore con tutto il suo codice della caldaia.

D'altra parte, se il tuo livello di astrazione richiede di documentare chiaramente la semantica della classe che contiene due oggetti o valori, allora dovresti scrivere una classe per esso. Di solito è così se i dati sono un oggetto business.

Come sempre, richiede un giudizio competente.

Per la tua seconda domanda, raccomando la classe Pair dalle librerie di Apache Commons. Quelle potrebbero essere considerate librerie standard estese per Java:

https: // commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/tuple/Pair.html

Potresti anche dare un'occhiata a EqualsBuilder , HashCodeBuilder e ToStringBuilder che semplifica la scrittura di classi di valori per i tuoi oggetti business.

Buone notizie Java hanno aggiunto il valore chiave Pair.

importa solo javafx.util.Pair ;

e usa semplicemente come in c ++ .

Pair < Key , Value > 

es.

Pair < Integer , Integer > pr = new Pair<Integer , Integer>()

pr.get(key); // will return corresponding value

Puoi utilizzare la classe di utilità javafx, Pair che ha lo stesso scopo della coppia < > in c ++. https://docs.oracle.com/javafx/2/api /javafx/util/Pair.html

Map.Entry vieni abbastanza vicino alla coppia c ++. Guarda l'implementazione concreta, come AbstractMap.SimpleEntry e AbstractMap.SimpleImmutableEntry Il primo elemento è getKey () e il secondo è getValue ().

Collections.singletonMap(left, rigth);

Secondo la natura del linguaggio Java, suppongo che le persone in realtà non richiedano un Pair , di solito un'interfaccia è ciò di cui hanno bisogno. Ecco un esempio:

interface Pair<L, R> {
    public L getL();
    public R getR();
}

Quindi, quando le persone vogliono restituire due valori, possono fare quanto segue:

... //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; }
}

Questa è una soluzione piuttosto leggera e risponde alla domanda " Qual è la semantica di una coppia < L, R > ? " ;. La risposta è che questa è un'interfaccia creata con due tipi (che possono essere diversi) e ha metodi per restituire ciascuno di essi. Sta a te aggiungere ulteriore semantico ad esso. Ad esempio, se si utilizza Position e si desidera VERAMENTE indicarlo nel proprio codice, è possibile definire PositionX e PositionY che contiene Integer , per crea un Pair < PositionX, PositionY > . Se JSR 308 è disponibile, puoi anche utilizzare Pair < @PositionX Integer, @PositionY Ingeger > per semplificare ciò.

EDIT: Una cosa che dovrei indicare qui è che la definizione di cui sopra si riferisce esplicitamente al nome del parametro di tipo e al nome del metodo. Questa è una risposta a coloro che sostengono che un Pair è la mancanza di informazioni semantiche. In realtà, il metodo getL significa " dammi l'elemento che corrisponde al tipo di parametro di tipo L " che significa qualcosa.

EDIT: Ecco una semplice classe di utilità che può semplificare la vita:

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; }   
        };
    }
}

utilizzo:

return Pairs.makePair(new Integer(100), "123");

Nonostante siano sintatticamente simili, Java e C ++ hanno paradigmi molto diversi. Scrivere C ++ come Java è un cattivo C ++ e scrivere Java come C ++ è un cattivo Java.

Con un IDE basato sulla riflessione come Eclipse, scrivendo necessariamente la funzionalità di una "coppia" la lezione è semplice e veloce. Crea classe, definisci due campi, usa i vari " Genera XX " opzioni di menu per compilare la classe in pochi secondi. Forse dovresti digitare un " comparePer " molto veloce se si desidera l'interfaccia comparabile.

Con opzioni di dichiarazione / definizione separate nella lingua i generatori di codice C ++ non sono così buoni, quindi scrivere a mano piccole classi di utilità richiede più tempo. Poiché la coppia è un modello, non è necessario pagare per le funzioni che non si utilizzano e la funzione typedef consente di assegnare nomi di tipo significativi al codice, quindi le obiezioni su "nessuna semantica" non reggere davvero.

La coppia sarebbe una buona cosa, essere un'unità di costruzione di base per generici complessi, ad esempio, questo è dal mio codice:

WeakHashMap<Pair<String, String>, String> map = ...

È lo stesso della Tupla di Haskell

Per linguaggi di programmazione come Java, la struttura di dati alternativa utilizzata dalla maggior parte dei programmatori per rappresentare coppie come strutture di dati sono due array e ai dati si accede tramite lo stesso indice

esempio: http: //www-igm.univ -mlv.fr/~lecroq/string/node8.html#SECTION0080

Questo non è l'ideale in quanto i dati dovrebbero essere uniti, ma si rivelano anche piuttosto economici. Inoltre, se il tuo caso d'uso richiede l'archiviazione delle coordinate, è meglio costruire la tua struttura dati.

Ho qualcosa del genere nella mia biblioteca

public class Pair<First,Second>{.. }

Puoi utilizzare la libreria AutoValue di Google - https://github.com/google/auto / albero / master / valore .

Crea una classe astratta molto piccola e la annoti con @AutoValue e il processore di annotazione genera una classe concreta per te che ha un valore semantico.

Ecco alcune librerie che hanno più gradi di tuple per comodità:

  • JavaTuples . Le tuple dal grado 1-10 sono tutto ciò che ha.
  • JavaSlang . Tuple dal grado 0-8 e molte altre chicche funzionali.
  • jOO & # 955; . Tuple di grado 0-16 e alcuni altri gadget funzionali. (Dichiarazione di non responsabilità, lavoro per la società di manutenzione)
  • Java funzionale . Tuple dal grado 0-8 e molte altre chicche funzionali.

Altre librerie sono state menzionate per contenere almeno la tupla Pair .

In particolare, nel contesto della programmazione funzionale che utilizza molta tipizzazione strutturale, piuttosto che tipizzazione nominale ( come sostenuto nella risposta accettata ), quelle biblioteche e le loro tuple sono molto utili.

Brian Goetz, Paul Sandoz e Stuart Marks spiegano perché durante la sessione di QA a Devoxx'14 .

Avere una classe di coppia generica nella libreria standard si trasformerà in debito tecnico una volta introdotto tipi di valore .

Vedi anche: Java SE 8 ha coppie o tuple?

Simple way Object [] - può essere usato come qualsiasi tupla di dimensione

Ho notato che tutte le implementazioni di Pair disseminate qui attribuiscono un significato all'ordine dei due valori. Quando penso a una coppia, penso a una combinazione di due elementi in cui l'ordine dei due non ha importanza. Ecco la mia implementazione di una coppia non ordinata, con hashCode e uguale a sostituisce per garantire il comportamento desiderato nelle raccolte. Clonabile anche.

/**
 * 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&lt;T, U> can still return <code>true</code> for method <code>equals</code>
 * called with a Pair&lt;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&lt;T, U&gt;</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 &lt;v1, v2> of
     * this pair and &lt;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&lt;T, U> can return <code>true</code> for equality testing with
     * a Pair&lt;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;

    }

}

Questa implementazione è stata correttamente testata dall'unità ed è stato provato l'uso in Set e Mappa.

Avviso Non sto dichiarando di rilasciarlo di dominio pubblico. Questo è il codice che ho appena scritto per l'uso in un'applicazione, quindi se hai intenzione di usarlo, ti preghiamo di astenersi dal fare una copia diretta e fare un po 'casino con i commenti e i nomi. Catturare la mia deriva?

Se qualcuno desidera una versione estremamente semplice e facile da usare, la metto a disposizione su https: / /github.com/lfac-pt/Java-Pair . Inoltre, i miglioramenti sono i benvenuti!

com.sun.tools.javac.util.Pair è una semplice implementazione di una coppia. Può essere trovato in jdk1.7.0_51 \ lib \ tools.jar.

Oltre a org.apache.commons.lang3.tuple.Pair, non è solo un'interfaccia.

un'altra implementazione tomb lombok

import lombok.Value;

@Value(staticConstructor = "of")
public class Pair<F, S> {
    private final F first;
    private final S second;
}
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;
    }

}

utilizzo:

Pair<Integer, String> pair = Pair.createPair(1, "test");
pair.getElement0();
pair.getElement1();

Immutabile, solo una coppia!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top