zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然java开发常用类库学习笔记:Map接口使用的注意事项

    import java.util.HashMap ;
    import java.util.Map ;
    import java.util.Set ;
    import java.util.Iterator ;
    public class ForeachDemo02{
        public static void main(String args[]){
            Map<String,String> map = null; // 声明Map对象,其中key和value的类型为String
            map = new HashMap<String,String>() ;
            map.put("mldn","www.mldn.cn") ;    // 增加内容
            map.put("zhinangtuan","www.zhinangtuan.net.cn") ;    // 增加内容
            map.put("mldnjava","www.mldnjava.cn") ;    // 增加内容
            for(Map.Entry<String,String> me:map.entrySet()){
                System.out.println(me.getKey() + " --> " + me.getValue()) ;
            }
        }
    };
    import java.util.Map ;
    import java.util.HashMap ;
    class Person{
        private String name ;
        private int age ;
        public Person(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public String toString(){
            return "姓名:" + this.name + ";年龄:" + this.age ;
        }
    };
    public class HashMapDemo05{
        public static void main(String args[]){
            Map<String,Person> map = null ;
            map = new HashMap<String,Person>() ;
            map.put("zhangsan",new Person("张三",30));    // 增加内容
            System.out.println(map.get("zhangsan")) ;
        }
    };
    import java.util.Map ;
    import java.util.HashMap ;
    class Person{
        private String name ;
        private int age ;
        public Person(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public String toString(){
            return "姓名:" + this.name + ";年龄:" + this.age ;
        }
    };
    public class HashMapDemo06{
        public static void main(String args[]){
            Map<Person,String> map = null ;
            map = new HashMap<Person,String>() ;
            map.put(new Person("张三",30),"zhangsan");    // 增加内容
            System.out.println(map.get(new Person("张三",30))) ;
        }
    };
    import java.util.Map ;
    import java.util.HashMap ;
    class Person{
        private String name ;
        private int age ;
        public Person(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public String toString(){
            return "姓名:" + this.name + ";年龄:" + this.age ;
        }
    };
    public class HashMapDemo07{
        public static void main(String args[]){
            Map<Person,String> map = null ;
            map = new HashMap<Person,String>() ;
            Person per = new Person("张三",30) ;
            map.put(per,"zhangsan");    // 增加内容
            System.out.println(map.get(per)) ;
        }
    };
    import java.util.Map ;
    import java.util.HashMap ;
    class Person{
        private String name ;
        private int age ;
        public Person(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public String toString(){
            return "姓名:" + this.name + ";年龄:" + this.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 class HashMapDemo08{
        public static void main(String args[]){
            Map<Person,String> map = null ;
            map = new HashMap<Person,String>() ;
            map.put(new Person("张三",30),"zhangsan");    // 增加内容
            System.out.println(map.get(new Person("张三",30))) ;
        }
    };
    import java.util.HashMap ;
    import java.util.Map ;
    import java.util.Set ;
    import java.util.Iterator ;
    public class IteratorDemo04{
        public static void main(String args[]){
            Map<String,String> map = null; // 声明Map对象,其中key和value的类型为String
            map = new HashMap<String,String>() ;
            map.put("mldn","www.mldn.cn") ;    // 增加内容
            map.put("zhinangtuan","www.zhinangtuan.net.cn") ;    // 增加内容
            map.put("mldnjava","www.mldnjava.cn") ;    // 增加内容
            Set<Map.Entry<String,String>> allSet = null ;
            allSet = map.entrySet() ;
            Iterator<Map.Entry<String,String>> iter = null ;
            iter = allSet.iterator() ;
            while(iter.hasNext()){
                Map.Entry<String,String> me = iter.next() ;
                System.out.println(me.getKey() + " --> " + me.getValue()) ;
            }
        }
    };
  • 相关阅读:
    从安装、管理到防御_阿里云安骑士全向测评
    云架构师前(钱)景这么好_我们该如何转型?这有两位阿里云云架构总监多年心得
    Infrastructure_as_Code——Kubernetes一键编排实践
    大中华地区(含港澳台)空气质量接口参加阿里云API_as_a_Service_大赛
    E-MapReduce集群启停HDFS/YARN服务
    云服务器ECS还原安全组规则功能介绍_安全组规则的备份与还原
    E-MapReduce集群中HDFS服务集成Kerberos
    FastReport中如何加入自定义函数
    查找算法总结
    八大排序算法总结
  • 原文地址:https://www.cnblogs.com/tszr/p/12152752.html
Copyright © 2011-2022 走看看