Java의 C ++ 쌍 의 동등한 것은 무엇입니까?
문제
아니오가있는 이유가 있습니까? Pair<L,R>
자바에서? 이 C ++ 구성과 동등한 것은 무엇입니까? 차라리 내 자신의 상환을 피할 것입니다.
그것은 것 같습니다 1.6 비슷한 것을 제공하고 있습니다 (AbstractMap.SimpleEntry<K,V>
), 그러나 이것은 상당히 복잡해 보입니다.
해결책
~ 안에 스레드 켜짐 comp.lang.java.help
, Hunter Gratzner는 Pair
자바로 구성됩니다. 주요 주장은 그 수업입니다 Pair
두 값 사이의 관계에 대한 의미론을 전달하지는 않습니다 ( "첫 번째"와 "두 번째"가 무엇을 의미하는지 어떻게 아십니까?).
더 나은 관행은 Mike가 제안한 것처럼 매우 간단한 수업을 작성하는 것입니다. Pair
수업. Map.Entry
이름으로 의미를 지니는 쌍의 예입니다.
요약하면, 내 생각에 수업을받는 것이 낫습니다. Position(x,y)
, 수업 Range(begin,end)
그리고 수업 Entry(key,value)
일반적인 것이 아니라 Pair(first,second)
그것은해야 할 일에 대해 아무 말도하지 않습니다.
다른 팁
이것은 Java입니다. 설명 클래스 및 필드 이름으로 자신의 맞춤형 쌍 클래스를 만들어야하며 Hashcode ()/equals ()를 작성하거나 비슷한 반복해서 구현하여 휠을 재발 명한다는 것을 염두에 두어야합니다.
해시 맵 호환 쌍 클래스 :
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;
}
}
내가 생각해 낼 수있는 가장 짧은 쌍은 다음과 같습니다. 롬복:
@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
쌍을 구현하는 또 다른 방법.
- 공개 불변 분야, 즉 간단한 데이터 구조.
- 유사한.
- 간단한 해시와 동등합니다.
간단한 공장이므로 유형을 제공 할 필요가 없습니다. 예를 들어 쌍 ( "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 + ')'; } }
어때 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)
사용하려는 것에 따라 다릅니다. 그렇게해야 할 일반적인 이유는 단순히 이렇게하는지도를 반복하기 때문입니다 (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());
}
안드로이드 제공 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);
}
}
가장 큰 문제는 아마도 A와 B에 불변성을 보장 할 수 없다는 것입니다 ( 유형 매개 변수가 불변을 보장하는 방법) 그래서 hashCode()
같은 쌍에 대해 일관되지 않은 결과를 줄 수 있습니다 ~ 후에 예를 들어 컬렉션에 삽입됩니다 (이것은 정의되지 않은 동작을 제공합니다. 변이 가능한 필드 측면에서 동등한 정의). 특정 (제네릭이 아닌) 쌍 클래스의 경우 프로그래머는 조심스럽게 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<?, ?>
.
이 구문에 대한 자세한 내용은 참조하십시오 객체가 비슷한 구현인지 확인하십시오 그리고 자세한 설명을 위해 제네릭을 구현하는 방법 max(Comparable a, Comparable b)
Java에서 기능?
제 생각에는 Java에는 쌍이 없습니다. 쌍에 직접 추가 기능을 추가하려면 (예 : 비교할 수 있음) 유형을 제한해야하기 때문입니다. C ++에서는 우리는 단지 신경 쓰지 않으며 쌍을 구성하는 유형이 operator <
,, pair::operator <
컴파일하지 않습니다.
경계 없음과 비교할 수있는 예 :
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));
유형 인수가 비슷한 지 여부에 대한 컴파일 타임 점검과 비교할 수있는 예입니다.
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과 함께 묶인)
다른 많은 사람들이 이미 언급했듯이, 쌍 클래스가 유용한 경우 사용 사례에 달려 있습니다.
개인 도우미 기능의 경우 코드를 더 읽기 쉽게 만들고 모든 보일러 플레이트 코드를 사용하여 또 다른 값 클래스를 만들기위한 노력의 가치가없는 경우 쌍 클래스를 사용하는 것이 합법적이라고 생각합니다.
반면에, 추상화 수준에서 두 객체 나 값이 포함 된 클래스의 의미를 명확하게 문서화 해야하는 경우 클래스를 작성해야합니다. 일반적으로 데이터가 비즈니스 객체 인 경우입니다.
항상 그렇듯이 숙련 된 판단이 필요합니다.
두 번째 질문은 Apache Commons 라이브러리에서 쌍 클래스를 추천합니다. 이들은 Java의 확장 된 표준 라이브러리로 간주 될 수 있습니다.
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/tuple/pair.html
Apache Commons를보고 싶을 수도 있습니다. EqualsBuilder, 해시 코드 빌더 그리고 TostringBuilder 비즈니스 객체에 대한 값 클래스 작성을 단순화합니다.
좋은 소식 Java
키 값 쌍이 추가되었습니다.
그냥 가져 오기 javafx.util.Pair
;
그리고 간단히 사용하십시오 c++
.
Pair < Key , Value >
예를 들어
Pair < Integer , Integer > pr = new Pair<Integer , Integer>()
pr.get(key); // will return corresponding value
Javafx 유틸리티 클래스를 사용할 수 있습니다. Pair
C ++에서 Pair <>와 동일한 목적을 제공합니다. https://docs.oracle.com/javafx/2/api/javafx/util/pair.html
Map.entry 인터페이스는 C ++ 쌍에 매우 가깝습니다. 구체적인 구현과 같은 것을보십시오 AbstractMap.simpleatry and acpractMap.simpleimMutableEntry 첫 번째 항목은 getKey ()이고 두 번째는 getValue ()입니다.
Collections.singletonMap(left, rigth);
자바 언어의 특성에 따르면 사람들이 실제로 Pair
, 인터페이스는 일반적으로 필요한 것입니다. 예는 다음과 같습니다.
interface Pair<L, R> {
public L getL();
public R getR();
}
따라서 사람들이 두 가지 값을 반환하려면 다음을 수행 할 수 있습니다.
... //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; }
}
이것은 꽤 가벼운 솔루션이며, "Semantic은 무엇입니까? Pair<L,R>
? ". 대답은, 이것은 두 가지 (다른) 유형의 인터페이스 빌드이며 각각을 반환하는 방법이 있습니다. 예를 들어, 사용중인 경우 더 의미 론적 추가를 추가하는 것은 귀하에게 달려 있습니다. 위치가 있고 실제로 코드에 표시하고 싶습니다. 정의 할 수 있습니다. PositionX
그리고 PositionY
그것은 포함되어 있습니다 Integer
, 구성하기 위해 Pair<PositionX,PositionY>
. JSR 308을 사용할 수있는 경우 사용할 수도 있습니다. Pair<@PositionX Integer, @PositionY Ingeger>
그것을 단순화하기 위해.
편집 : 여기에 표시해야 할 한 가지는 위의 정의가 유형 매개 변수 이름과 메소드 이름을 명시 적으로 관련시키는 것입니다. 이것은 그 주장에 대한 답입니다 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");
구문 적으로 유사하지만 Java와 C ++는 매우 다른 패러다임을 가지고 있습니다. Java와 같은 C ++를 쓰는 것은 C ++가 좋지 않으며 C ++와 같은 Java를 쓰는 것은 나쁜 Java입니다.
Eclipse와 같은 반사 기반의 IDE를 사용하면 "쌍"클래스의 반드시 기능성을 작성하는 것이 빠르고 간단합니다. 클래스 생성, 두 필드 정의, 다양한 "XX 생성"메뉴 옵션을 사용하여 몇 초 만에 클래스를 작성하십시오. 비슷한 인터페이스를 원한다면 "비교"를 빠르게 입력해야 할 수도 있습니다.
언어의 별도의 선언 / 정의 옵션을 사용하면 C ++ 코드 생성기가 그리 좋지 않으므로 유틸리티 클래스를 적게 쓰는 것이 더 많은 시간이 소비됩니다. 쌍은 템플릿이므로 사용하지 않는 기능에 대해 지불 할 필요가 없으며 TypEdef 시설을 통해 의미있는 타이프 이름을 코드에 할당 할 수 있으므로 "의미 없음"에 대한 이의 제기는 실제로 유지되지 않습니다.
예를 들어 복잡한 제네릭의 기본 건축 장치가되기 위해 쌍은 좋은 것입니다. 예를 들어, 이것은 내 코드에서 나온 것입니다.
WeakHashMap<Pair<String, String>, String> map = ...
Haskell의 튜플과 동일합니다
Java와 같은 프로그래밍 언어의 경우 대부분의 프로그래머가 데이터 구조와 같은 쌍을 나타내는 데 사용되는 대체 데이터 구조는 두 개의 배열이며 동일한 인덱스를 통해 데이터에 액세스됩니다.
예시: http://www-igm.univ-mlv.fr/~lecroq/string/node8.html#section0080
이것은 데이터가 함께 묶어야하기 때문에 이상적이지 않지만 꽤 싸게 판명되었습니다. 또한 유스 케이스가 코디네이트를 저장 해야하는 경우 자체 데이터 구조를 구축하는 것이 좋습니다.
나는 내 도서관에서 이런 일이 있습니다
public class Pair<First,Second>{.. }
Google Autovalue 라이브러리를 사용할 수 있습니다. https://github.com/google/auto/tree/master/value.
당신은 매우 작은 추상 클래스를 만들고 @autovalue로 주석을 달고 주석 프로세서는 값 의미가있는 콘크리트 클래스를 생성합니다.
다음은 편의를 위해 여러 정도의 튜플이있는 라이브러리입니다.
- Javatuples. 1-10 도의 튜플 만 있으면됩니다.
- Javaslang. 도 0-8 도의 튜플 및 다른 많은 기능적 케이크.
- 주오 λ. 도 0-16 도의 튜플 및 다른 기능적 케이크. (면책 조항, 나는 관리자 회사에서 일합니다)
- 기능적 자바. 도 0-8 도의 튜플 및 다른 많은 기능적 케이크.
다른 도서관에는 적어도 Pair
튜플.
구체적으로, 공칭 타이핑보다는 많은 구조적 타이핑을 사용하는 기능적 프로그래밍의 맥락에서 (받아 들여진 답변에 옹호 된 바와 같이), 그 라이브러리와 그들의 튜플은 매우 편리합니다.
Brian Goetz, Paul Sandoz 및 Stuart Marks 이유를 설명해라 Devoxx'14에서 QA 세션 중.
표준 라이브러리에 일반 쌍 클래스가 있으면 기술 부채가 한 번 가치 유형 소개.
또한보십시오: Java SE 8에는 쌍이나 튜플이 있습니까?
Simple Way Object [] - 치수 튜플로 사용할 수 있습니다.
여기에 모든 쌍 구현이 여기에 흩어져 있음을 알았습니다. 쌍을 생각할 때, 나는 둘의 순서가 중요하지 않은 두 항목의 조합을 생각합니다. 다음은 언더 쌍의 구현입니다. 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 이외의 인터페이스가 아닙니다.
또 다른 Terse 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;
}
}
용법 :
Pair<Integer, String> pair = Pair.createPair(1, "test");
pair.getElement0();
pair.getElement1();
불변, 쌍 만!