• 吴裕雄--天生自然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()) ;
            }
        }
    };
  • 相关阅读:
    20220428 08:00:02
    20220427 08:00:02
    Belong to you
    今天看到疫情驰援的人发布的视频,感触很多
    用生活治愈生活
    站 又名《standard》
    在mysql中,如果传入的startTime为20201112,20201113这样的字符串,则可以使用find_in_set取代in
    界面控件DevExpress WinForms MVVM入门指南——登录表单(下)
    界面控件DevExpress WPF v21.2 可访问性功能升级
    UI组件Kendo UI for jQuery数据管理教程 TaskBoard/搜索工具辑
  • 原文地址:https://www.cnblogs.com/tszr/p/12152752.html
Copyright © 2020-2023  润新知