摩根斯坦利 - 2016年09月8日 面试题:

给定一个 Map<Person, Object> map = new HashMap<Person, Object>();

放入2个Person,如果id相同,则判定2个Person为相同。

然后修改id,再去判定。

package cn.itcast.test;

import java.util.HashMap;
import java.util.Map;

public class TestHashMap {
    public static void main(String[] args) {
        Map<Person, Object> map = new HashMap<Person, Object>();
        
        Person p1 = new Person(1, "aaa");
        Person p2 = new Person(1, "bbb");
        
        map.put(p1, p1);
        map.put(p2, p2);
        
        System.out.println(map.get(p1)); //Person [id=1, name=bbb]
        System.out.println(map.get(p2)); //Person [id=1, name=bbb]
        
        p2.setId(2);
        System.out.println(map.get(p1)); //Person [id=2, name=bbb]
        System.out.println(map.get(p2)); //null
    }
}

class Person {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public Person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public int hashCode() { //pay attention here
        return id;
    }
    @Override
    public boolean equals(Object obj) { //pay attention here
        if (id == ((Person)obj).getId()) {
            return true;
        }
        return false;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + "]";
    }
}

摩根斯坦利 - 2016年09月8日 面试题 - HashMap的更多相关文章

  1. 2016年3月15日Android实习日记

    1.解决了ScrollView滑动冲突问题. 2.设置好了“查看详解”与“题目编号”的部分. 3.完成了app启动图片的设置,并在启动的过程中开辟新的线程连接服务器并开启监听数据. 别忘了注册启动Ac ...

  2. 2016年12月31日 星期六 --出埃及记 Exodus 21:26

    2016年12月31日 星期六 --出埃及记 Exodus 21:26 "If a man hits a manservant or maidservant in the eye and d ...

  3. 2016年12月30日 星期五 --出埃及记 Exodus 21:25

    2016年12月30日 星期五 --出埃及记 Exodus 21:25 burn for burn, wound for wound, bruise for bruise.以烙还烙,以伤还伤,以打还打 ...

  4. 2016年12月29日 星期四 --出埃及记 Exodus 21:24

    2016年12月29日 星期四 --出埃及记 Exodus 21:24 eye for eye, tooth for tooth, hand for hand, foot for foot,以眼还眼, ...

  5. 2016年12月28日 星期三 --出埃及记 Exodus 21:23

    2016年12月28日 星期三 --出埃及记 Exodus 21:23 But if there is serious injury, you are to take life for life,若有 ...

  6. 2016年12月27日 星期二 --出埃及记 Exodus 21:22

    2016年12月27日 星期二 --出埃及记 Exodus 21:22 "If men who are fighting hit a pregnant woman and she gives ...

  7. 2016年2月16日开始,每天一篇,记录学习心得,【基本技能篇】>>开篇《如何阅读一本书——心得》

    如何阅读一本书——心得 ——2016年2月12日 要达到阅读的所有目的,就必须在阅读不同书籍的时候,运用适当的不同速度.读的太快或太慢,都一无所获. 四个阅读层次:①基础阅读,具有基本阅读的能力,包括 ...

  8. Python array,list,dataframe索引切片操作 2016年07月19日——智浪文档

    array,list,dataframe索引切片操作 2016年07月19日——智浪文档 list,一维,二维array,datafrme,loc.iloc.ix的简单探讨 Numpy数组的索引和切片 ...

  9. c++中变量声明和变量定义的区别。2016年12月6日

    整个流程: 1.程序告诉cpu,程序将要使用一个变量.(暂时不一定用到,先说一下.) 2.程序告诉CPU,程序现在就要使用一个变量.(现在就用) 3.cpu按照这个变量的类型,把内存划分出几个单位(b ...

随机推荐

  1. php算法之快速排序

    /** * 快速排序 * 原理: * 快速排序使用分治法(Divide and conquer)策略来把一个串行(list)分为两个子串行(sub-lists). * 最差时间复杂度 O(n*n) * ...

  2. jQuery源代码学习笔记:jQuery.fn.init(selector,context,rootjQuery)代码具体解释

    3.1 源代码 init: function( selector, context, rootjQuery ) { var match, elem, ret, doc; // Handle $(&qu ...

  3. SQL查询练习题目

    SQL查询练习题目 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示 ...

  4. javascript 手机号抽奖

     案例   ----           手机号抽奖   开始抽奖  停止                                                       *具体的备注在代 ...

  5. Spring-----代码中使用注入的Properties配置属性

    转载自:http://blog.csdn.net/hekewangzi/article/details/49990799

  6. PHP的五种常见设计模式

    工厂模式 最初在设计模式 一书中,许多设计模式都鼓励使用松散耦合.要理解这个概念,让我们最好谈一下许多开发人员从事大型系统的艰苦历程.在更改一个代码片段时,就会发生问题,系统其他部分 —— 您曾认为完 ...

  7. Ubuntu 使用wget 命令下载JDK

    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...

  8. Android_BitmapShader实现圆形、圆角图片

    转:http://blog.csdn.net/lmj623565791/article/details/41967509,本文出自:[张鸿洋的博客] 1.概述 记得初学那会写过一篇博客Android ...

  9. mongo设计(一)

    原文:http://blog.mongodb.org/post/87200945828/6-rules-of-thumb-for-mongodb-schema-design-part-1 By Wil ...

  10. debian install & configure(2)-drivers-nvidia

    ==========================================手动编译卸载受限驱动 :apt-get --purge remove nvidia-*apt-get --purge ...