为了账号安全,请及时绑定邮箱和手机立即绑定

在Java中,C+对<L,R>的等价性是什么?

在Java中,C+对<L,R>的等价性是什么?

侃侃尔雅 2019-06-18 10:32:52
在Java中,C+对<L,R>的等价性是什么?有什么很好的理由Pair<L,R>在爪哇?这个C+结构的等价性是什么?我宁愿避免重新实施我自己的。看来1.6正在提供类似的东西(AbstractMap.SimpleEntry<K,V>),但这看起来相当复杂。
查看完整描述

3 回答

?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

这是爪哇。您必须使用描述性类名和字段名来定制您自己的对对类,而不必担心您将通过编写hashCode()/equals()或一次又一次地实现可比较的方式来重新发明轮子。


查看完整回答
反对 回复 2019-06-18
?
炎炎设计

TA贡献1808条经验 获得超4个赞

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


查看完整回答
反对 回复 2019-06-18
  • 3 回答
  • 0 关注
  • 438 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号