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()) ;
            }
        }
    };
  • 相关阅读:
    五十:数据库之Flask-Script详解
    四十九:数据库之Flask-SQLAlchemy下alembic的配置
    四十八:数据库之alembic常用命令和经典错误的解决办法
    四十七:数据库之alembic数据库迁移工具的基本使用
    四十六:数据库之Flask-SQLAlchemy的使用
    四十五:数据库之SQLAlchemy之subquery实现复杂查询
    四十四:数据库之SQLAlchemy之join实现复杂查询
    四十三:数据库之SQLAlchemy之group_by和having子句
    四十二:数据库之SQLAlchemy之数据查询懒加载技术
    四十一:数据库之SQLAlchemy之limlt、、slice、offset及切片
  • 原文地址:https://www.cnblogs.com/tszr/p/12152752.html
Copyright © 2011-2022 走看看