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()) ;
            }
        }
    };
  • 相关阅读:
    JavaWeb--JDBC
    JavaWeb--过滤器、监听器
    Javaweb-MVC三层架构
    JavaWeb--JSP
    JavaWeb--Cookie、Session
    JavaWeb--Servlet
    实用记录文档
    分库分表Sharding-JDBC + MyBatis-Plus动态表名
    如何不靠运气变得富有 (九) —— 互联网极大地丰富了职业发展的可能性
    如何不靠运气变得富有 (八) —— 给社会提供它不知道如何获得的东西
  • 原文地址:https://www.cnblogs.com/tszr/p/12152765.html
Copyright © 2011-2022 走看看