The tricky thing is how to decide the key for a hashmap. Especially when you intend to use self-defined objects as key.

If you want to make two different objects 'equal' to each other, you have to overwrite equals() and hashCode().

The simple rule is you have to overwrite hashcode() and equals() method.

Rule 1: x.equals(y) return true if x indeed is equal to y by your own logic;

Rule 2: if x.equals(y) then x.hashcode() == y.hashcode().

Ideally any two different objects have distinct hashcodes. But that't not possible since hashcode is used to generate real array index. Unfortunately, collisison will happen. Thus in practice it's possible for two different objects to have same hashcode. Then equals() is used to tell one from the other.

Example:How to overwrite hashCode() and equals()

Find a line passing the most number of points

Keys of HashMap in Java的更多相关文章

  1. java.util.HashMap和java.util.HashTable (JDK1.8)

    一.java.util.HashMap 1.1 java.util.HashMap 综述 java.util.HashMap继承结构如下图 HashMap是非线程安全的,key和value都支持nul ...

  2. Hashmap in java

    1. HashMap要点: 1.1 基本数据结构:  采用 数组+链表/平衡二叉查找树 的组合形式,所有键值对都以Entry<K,V>形式存储(每put进一个键值对,就会实例化一个Entr ...

  3. LeetCode算法题-Design HashMap(Java实现)

    这是悦乐书的第299次更新,第318篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第167题(顺位题号是706).在不使用任何内置哈希表库的情况下设计HashMap.具体 ...

  4. C++的hashmap和Java的hashmap

    C++里面是这样的:typedef std::unordered_map<std::string,std::string> stringmap; std::unordered_map< ...

  5. java--HashMap多线程并发问题分析

    并发问题的症状 多线程put后可能导致get死循环 从前我们的Java代码因为一些原因使用了HashMap这个东西,但是当时的程序是单线程的,一切都没有问题.后来,我们的程序性能有问题,所以需要变成多 ...

  6. JAVA hashmap知识整理

    HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是否可以随机应变使用多种思路解决问题.HashMap的工作原理.ArrayList与Vect ...

  7. Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

  8. Java 集合系列14之 Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)

    概要 学完了Map的全部内容,我们再回头开开Map的框架图. 本章内容包括:第1部分 Map概括第2部分 HashMap和Hashtable异同第3部分 HashMap和WeakHashMap异同 转 ...

  9. JAVA基础学习day16--集合三-Map、HashMap,TreeMap与常用API

    一.Map简述 1.1.简述 public interface Map<K,V> 类型参数: K - 此映射所维护的键的类型 key V - 映射值的类型 value 该集合提供键--值的 ...

随机推荐

  1. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(四)

    这一章大象将详细分析web层代码,以及使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能.     之前在使用Struts2实现MVC的注解时,是借助struts2-conventi ...

  2. 破解C#的readonly只读字段

    破解C#的readonly只读字段 目录 请允许我再唠叨几句const和readonly 修改readonly字段: 计策1:反间计 -- 反射修改 计策2:借刀杀人--调节字段偏移位置的结构体来修改 ...

  3. linux网卡混杂模式打开

    有时候为嗅探到网络上的数据,需要将网卡设置到混杂模式.进入该模式将网络上的数据一并抓获,为此在设置nic的混杂模式的时候有诸多方法? 通过shell命令来实现:ifconfig eth1 promis ...

  4. MyBatis自动获取主键,MyBatis使用Oracle返回主键,Oracle获取主键

    MyBatis自动获取主键,MyBatis使用Oracle返回主键,Oracle获取主键 >>>>>>>>>>>>>> ...

  5. 第十篇:web之前端之django一些feature

    前端之django一些feature   前端之django一些feature 本节内容 cookie session 跨站请求保护 分页 序列化 model模块 CBV和FBV 模板渲染对象 1. ...

  6. sublime text 3快捷键设置

    sublime text 3  v-3103默认快捷键设置 [ { "keys": ["ctrl+shift+n"], "command": ...

  7. mysql自增

    主键设置自增,同时主键需要是int类型

  8. sublime text3输入中文的问题.

    1.新建sublime_imfix.c文件 里面输入: /* * sublime-imfix.c * Use LD_PRELOAD to interpose some function to fix ...

  9. ios Using CocoaPods to Modularize a Big iOS App->使用CocoaPods来进行模块化开发

    Selecting the right architecture for your mobile app is a pretty big deal. It will shape your daily ...

  10. xCode如何导入自定义的snippets文件

    xCode代码块snippets导入 目标文件放置位置 ~/Library/Developer/Xcode/UserData/CodeSnippets 操作方法: 解压缩并复制到以下目录即可