import java.util.IdentityHashMap ;
import java.util.HashMap ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Map ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ;
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ;
}
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
};
public class IdentityHashMapDemo01{
public static void main(String args[]){
Map<Person,String> map = null ; // 声明Map对象
map = new HashMap<Person,String>() ;
map.put(new Person("张三",30),"zhangsan_1") ; // 加入内容
map.put(new Person("张三",30),"zhangsan_2") ; // 加入内容
map.put(new Person("李四",31),"lisi") ; // 加入内容
Set<Map.Entry<Person,String>> allSet = null ; // 准备使用Set接收全部内容
allSet = map.entrySet() ;
Iterator<Map.Entry<Person,String>> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry<Person,String> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};
import java.util.IdentityHashMap ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Map ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ;
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ;
}
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
};
public class IdentityHashMapDemo02{
public static void main(String args[]){
Map<Person,String> map = null ; // 声明Map对象
map = new IdentityHashMap<Person,String>() ;
map.put(new Person("张三",30),"zhangsan_1") ; // 加入内容
map.put(new Person("张三",30),"zhangsan_2") ; // 加入内容
map.put(new Person("李四",31),"lisi") ; // 加入内容
Set<Map.Entry<Person,String>> allSet = null ; // 准备使用Set接收全部内容
allSet = map.entrySet() ;
Iterator<Map.Entry<Person,String>> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry<Person,String> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};

吴裕雄--天生自然java开发常用类库学习笔记:IdentityHashMap类的更多相关文章

  1. 吴裕雄--天生自然java开发常用类库学习笔记:定时调度

    // 完成具体的任务操作 import java.util.TimerTask ; import java.util.Date ; import java.text.SimpleDateFormat ...

  2. 吴裕雄--天生自然java开发常用类库学习笔记:正则表达式

    public class RegexDemo01{ public static void main(String args[]){ String str = "1234567890" ...

  3. 吴裕雄--天生自然java开发常用类库学习笔记:观察者设计模式

    import java.util.* ; class House extends Observable{ // 表示房子可以被观察 private float price ;// 价钱 public ...

  4. 吴裕雄--天生自然java开发常用类库学习笔记:比较器

    class Student implements Comparable<Student> { // 指定类型为Student private String name ; private i ...

  5. 吴裕雄--天生自然java开发常用类库学习笔记:Arrays

    import java.util.* ; public class ArraysDemo{ public static void main(String arg[]){ int temp[] = {3 ...

  6. 吴裕雄--天生自然java开发常用类库学习笔记:大数操作

    import java.math.* ; class MyMath{ public static double add(double d1,double d2){ // 进行加法计算 BigDecim ...

  7. 吴裕雄--天生自然java开发常用类库学习笔记:NumberFormat

    import java.text.* ; public class NumberFormatDemo01{ public static void main(String args[]){ Number ...

  8. 吴裕雄--天生自然java开发常用类库学习笔记:Math与Random类

    public class MathDemo01{ public static void main(String args[]){ // Math类中的方法都是静态方法,直接使用“类.方法名称()”的形 ...

  9. 吴裕雄--天生自然java开发常用类库学习笔记:取得当前日期

    import java.util.* ; // 导入需要的工具包 class DateTime{ // 以后直接通过此类就可以取得日期时间 private Calendar calendar = nu ...

  10. 吴裕雄--天生自然java开发常用类库学习笔记:日期操作类DataFormat、SimpleDataFormat

    import java.text.DateFormat ; import java.util.Date ; public class DateDemo03{ public static void ma ...

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:让表格更加紧凑

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Maven 项目中使用 logback

    添加依赖 <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logsta ...

  3. LeetCode中等题(二)

    题目一: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  4. mysql 统计索引执行情况

    select distinct b.TABLE_SCHEMA,b.TABLE_NAME , b.INDEX_NAME , a.count_starfrom performance_schema.tab ...

  5. 02.Scala高级特性:第6节 高阶函数;第7节 隐式转换和隐式参数

    Scala高级特性 1.    课程目标 1.1.   目标一:深入理解高阶函数 1.2.   目标二:深入理解隐式转换 2.    高阶函数 2.1.   概念 Scala混合了面向对象和函数式的特 ...

  6. MongoDB基础篇1:安装和服务配置

    一.下载 请前往官网下载community版本MongoDB,我当前可见最新版本是3.6.4 https://www.mongodb.com/download-center#community 如需下 ...

  7. Dynamic Programming(动态规划)

    钢材分段问题 #include<iostream> #include<vector> using namespace std; class Solution { public: ...

  8. git - 节点树理解

    1.如下图 最左边黑色线是当前分支节点 每个点代表一次提交 2.当执行merge prd的时候 会把其他分支的节点都merge到你的分支同时产生右边的每条节点线. 3.每个版本都会与某条版本线上的一个 ...

  9. jetson nano 安装 snowboy 遇到的问题及处理

    Snowboy 是 KITT.AI 开发的一个高度可定制的热词检测引擎,当笔者的 jetson nano 加上话筒后,就立马尝试安装,但在安装过程中却发生了错误,所以把处理方式记录了下来以作备忘. 首 ...

  10. 棋盘 || 状压DP

    题意:有一个n*m的棋盘(n,m≤80,n*m≤80)要在棋盘上放k(k≤20)个棋子,使得任意两个棋子不相邻(每个棋子最多和周围4个棋子相邻).求合法的方案总数. 思路:对于每一行,如果把没有棋子的 ...