吴裕雄--天生自然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()) ;
        }
    }
};
原文地址:https://www.cnblogs.com/tszr/p/12152752.html