黑马程序员技术交流社区

标题: Map集合的两种取出方式 [打印本页]

作者: 菜鸟哥    时间: 2015-7-25 22:24
标题: Map集合的两种取出方式
Map集合的两种取出方式
        Map集合的取出原理:将Map集合转成Set集合。再通过迭代器取出。
1、Set<K> keySet():将Map中所以的键存入到Set集合。因为Set具备迭代器。所以可以通过迭代方式取出所以键的值,再通过get方法。获取每一个键对应的值。
示例:
[java] view plaincopy
1./* 
2.每一个学生都有对应的归属地。 
3.学生Student,地址String。 
4.学生属性:姓名,年龄。 
5.注意:姓名和年龄相同的视为同一个学生。 
6.保证学生的唯一性。 
7. 
8.思路:1、描述学生类 
9.      2、定义一个Map集合,存储学生对象和地址值 
10.      3、获取Map中的元素 
11.*/  
12.  
13.import java.util.*;  
14.  
15.//描述学生类  
16.class Student implements Comparable<Student>  
17.{  
18.    private String name;  
19.    private int age;  
20.    Student(String name,int age)  
21.    {  
22.        this.name=name;  
23.        this.age=age;  
24.    }  
25.    public String getName()  
26.    {  
27.        return name;  
28.    }  
29.  
30.    public int getAge()  
31.    {  
32.        return age;  
33.    }  
34.  
35.    //复写hashCode  
36.    public int hashCode()  
37.    {  
38.        return name.hashCode()+age*33;   
39.    }  
40.    //复写equals,比较对象内容  
41.    public boolean equals(Object obj)  
42.    {  
43.        if(!(obj instanceof Student))  
44.            throw new ClassCastException("类型不匹配");  
45.        Student s=(Student)obj;  
46.        return this.name.equals(s.name)&&this.age==s.age;  
47.    }  
48.  
49.    //复写compareTo,以年龄为主  
50.    public int compareTo(Student c)  
51.    {  
52.        int num=new Integer(this.age).compareTo(new Integer(c.age));  
53.        if(num==0)  
54.        {  
55.            return this.name.compareTo(c.name);  
56.        }  
57.        return num;  
58.    }  
59.  
60.    //复写toString,自定义输出内容  
61.    public String toString()  
62.    {  
63.        return name+"..."+age;  
64.    }  
65.  
66.}  
67.class  HashMapTest  
68.{  
69.    public static void main(String[] args)   
70.    {  
71.        HashMap<Student,String > hm=new HashMap<Student,String >();  
72.        hm.put(new Student("zhangsan",12),"beijing");  
73.        hm.put(new Student("zhangsan",32),"sahnghai");  
74.        hm.put(new Student("zhangsan",22),"changsha");  
75.        hm.put(new Student("zhangsan",62),"USA");  
76.        hm.put(new Student("zhangsan",12),"tianjing");  
77.  
78.        keyset(hm);  
79.    }  
80.  
81.    //keySet取出方式  
82.    public static void keyset(HashMap<Student,String> hm)  
83.    {  
84.        Iterator<Student> it=hm.keySet().iterator();  
85.  
86.        while(it.hasNext())  
87.        {  
88.            Student s=it.next();  
89.            String addr=hm.get(s);  
90.            System.out.println(s+":"+addr);  
91.        }  
92.    }  
93.}  
2、Set<Map.Entry<K,V>>   entrySet():将Map集合中的映射关系存入到Set集合中,而这个关系的数据类型就是:Map.Entry
       其实,Entry也是一个接口,它是Map接口中的一个内部接口。   
[java] view plaincopy
1.interface Map  
2.    {  
3.         public static interface Entry  
4.         {  
5.              public abstract Object getKey();  
6.              public abstract Object getValue();  
7.         }  
8. }  
示例:
       同上面示例的学生类----
[java] view plaincopy
1.class  HashMapTest  
2.{  
3.    public static void main(String[] args)   
4.    {  
5.        HashMap<Student,String > hm=new HashMap<Student,String >();  
6.        hm.put(new Student("zhangsan",12),"beijing");  
7.        hm.put(new Student("zhangsan",32),"sahnghai");  
8.        hm.put(new Student("zhangsan",22),"changsha");  
9.        hm.put(new Student("zhangsan",62),"USA");  
10.        hm.put(new Student("zhangsan",12),"tianjing");  
11.  
12.        entryset(hm);  
13.    }  
14.//entrySet取出方式  
15.    public static void entryset(HashMap<Student,String> hm)  
16.    {  
17.        Iterator<Map.Entry<Student,String>> it=hm.entrySet().iterator();  
18.  
19.        while(it.hasNext())  
20.        {  
21.            Map.Entry<Student,String> me=it.next();  
22.            Student s=me.getKey();  
23.            String addr=me.getValue();  
24.            System.out.println(s+":::"+addr);  
25.        }  
26.    }  
27.}  
关于Map.Entry:
        Map是一个接口,其实,Entry也是一个接口,它是Map的子接口中的一个内部接口,就相当于是类中有内部类一样。为何要定义在其内部呢?
        原因:a、Map集合中村的是映射关系这样的两个数据,是先有Map这个集合,才可有映射关系的存在,而且此类关系是集合的内部事务。
                     b、并且这个映射关系可以直接访问Map集合中的内部成员,所以定义在内部。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2