Java中没有Pair<L,R>,有什么好理由吗?与这个c++构造等价的是什么?我宁愿避免重新实现我自己的。

似乎1.6提供了类似的东西(AbstractMap.SimpleEntry<K,V>),但这看起来相当复杂。


当前回答

您可以使用javafx实用程序类Pair,它的作用与c++中的Pair <>相同。https://docs.oracle.com/javafx/2/api/javafx/util/Pair.html

其他回答

根据Java语言的性质,我认为人们实际上并不需要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; }
}

This is a pretty lightweight solution, and it answers the question "What is the semantic of a Pair<L,R>?". The answer is, this is an interface build with two (may be different) types, and it has methods to return each of them. It is up to you to add further semantic to it. For example, if you are using Position and REALLY want to indicate it in you code, you can define PositionX and PositionY that contains Integer, to make up a Pair<PositionX,PositionY>. If JSR 308 is available, you may also use Pair<@PositionX Integer, @PositionY Ingeger> to simplify that.

编辑: 这里我应该指出的一点是,上面的定义显式地将类型参数名和方法名联系起来。这是对那些认为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");

简单的方法Object[] -可以作为一个几何体元组使用

尽管语法相似,但Java和c++有非常不同的范例。像Java一样写c++是糟糕的c++,像c++一样写Java是糟糕的Java。

使用像Eclipse这样基于反射的IDE,编写“pair”类的必要功能是快速而简单的。创建类,定义两个字段,使用各种“Generate XX”菜单选项在几秒钟内填写类。如果你想要Comparable界面,也许你必须很快地输入一个“compareTo”。

由于c++语言中有单独的声明/定义选项,代码生成器并不是很好,所以手工编写小的实用程序类更加费时乏味。由于pair是一个模板,您不必为不使用的函数付费,并且typedef功能允许为代码分配有意义的类型名,因此关于“无语义”的反对意见并不成立。

pair是pair的一个简单实现。 它可以在jdk1.7.0_51\lib\tools.jar中找到。

除了org.apache.commons.lang3.tuple.Pair之外,它不仅仅是一个接口。

HashMap兼容Pair类:

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