zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然java开发常用类库学习笔记:IdentityHashMap类

    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-2.6 逗号操作符
    JavaScript基础知识
    特性选择器
    文本缩进
    如何使图片与导航栏对齐
    如何使用CSS选择器应用于子元素
    图像
    布局
    列表,表格和表单
    盒子
  • 原文地址:https://www.cnblogs.com/tszr/p/12152765.html
Copyright © 2011-2022 走看看