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()) ;
            }
        }
    };
  • 相关阅读:
    EF架构~系列目录
    不谈技术~做一个好人,一个简单的人
    不谈技术~谈人生,干吧,年轻人!
    c++ 用lambda删除vector中元素
    转:我们为什么这么容易受骗?
    windows 数据类型
    《暗时间》书摘 “学习与思考”
    转:斯托克代尔悖论与底线思考法
    Android之HelloWorld
    win7搭建android开发环境
  • 原文地址:https://www.cnblogs.com/tszr/p/12152752.html
Copyright © 2011-2022 走看看