黑马程序员--java基础之类集框架(三)

Map练习

     练习一;1、每个学生对应一个归宿地;

                   2、学生有姓名和年龄两个属性;

3、如果姓名年龄相同则为同一人;

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//实现Comparable接口是为了防止以后有可能存入TreeSet中;
//因为二叉树结构需要有排序,不实现的话是按默认排序,复写其中的CompareTo方法可以自定义排序 
class Student implements Comparable<Student>{
    private String name;                       
    private int age;
    Student(String name, int age){
        this.name = name;
        this.age = age;
    }
    public int compareTo(Student stu){     
        int num = new Integer(this.age).compareTo(new Integer(stu.age));
        if(num == 0)
               
            return this.name.compareTo(stu.name);
        return num;    
    }
    public int hashCode(){//只要底层数据结构式哈希表的就必须复写其中的hasCode方法和equals方法,要保证元素的唯一性
        return name.hashCode() + age*34;
    }
    public boolean equals(Object obj){
        if(!(obj instanceof Student))
            throw new ClassCastException("类型转换异常");
        Student stu = (Student)obj;
        return this.name.equals(stu.name) && this.age == stu.age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public void setName(){
        this.name = name;
    }
    public void setAge(){
        this.age = age;
    }
    public String toString(){
        return name+"-"+age;
    }
}

    练习二:将练习一的自定义对象排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class myComparator implements Comparator<Student>{
    public int compare(Student stu1,Student stu2){
        int num = stu1.getName().compareTo(stu2.getName());
        if(num == 0)
            return new Integer(stu1.getAge()).compareTo(new Integer(stu2.getAge()));
        return num;
    }
}
class MapTest{
    public static void main(String [] args){
        //HashMap<Student,String> hm = new HashMap<Student, String>();
        TreeMap<Student,String> tm = new TreeMap<Student,String>(new myComparator());
        // hm.put(new Student("zhangsan01",21), "beijing");
        // hm.put(new Student("zhangsan02",22), "tianjin");
        // hm.put(new Student("zhangsan02",25), "wuhan");
        // hm.put(new Student("zhangsan03",23), "chongqing");
        // hm.put(new Student("zhangsan04",24), "shanghai");
        tm.put(new Student("zhangsan01",21), "beijing");
        tm.put(new Student("zhangsan02",22), "tianjin");
        tm.put(new Student("zhangsan02",25), "wuhan");
        tm.put(new Student("zhangsan03",23), "chongqing");
        tm.put(new Student("zhangsan04",24), "shanghai");
        
        注意:HashMap有两种读取方法
            1、用keySet方法,返回Map中的键的引用,放在Set集合中,然后用Set集合的迭代器调用,迭代器返回的对象是自定义对象的引用;
        Set<Student> set = hm.keySet();
        Iterator<Student> it = set.iterator();
        while(it.hasNext()){
            Student key = it.next();
            String value = hm.get(key);
            System.out.println(key + "----"+ value);
        }
            2、用entrySet方法,返回Map中键值对的关系引用,放在Set<Map.Entry<k,v>>中,然后用Set的迭代器调用,迭代器返回的是Map.Entry的引用,然后调用其中的getKey和getValue获取具体内容
        
        Set<Map.Entry<Student,String>> set = tm.entrySet();
        Iterator<Map.Entry<Student,String>> it = set.iterator();
        while(it.hasNext()){
            Map.Entry<Student,String> me = it.next();
            Student key = me.getKey();
            String value = me.getValue();
            System.out.println(key+ "-----" + value);
        }
    }
}

 

      练习三:记录字符串中的每个字符出现的次数,例:String str = "abcadncgbndacb";要求结果:a(3),b(3)...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class MapTest{
    public static void main(String [] args){
        String str = "abbcccddddeeeee";
        str = charCount(str);
        System.out.println(str);
    }  
    public static String charCount(String str){
        char [] chs = str.toCharArray();
        TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
        //int count = 0;
        for(int i=0; i<chs.length; i++){
            if(!(char[i] > 'a' && char[i] < 'z' || char[i] > 'A' && char[i] < 'Z'))
                continue;
            Integer value = tm.get(chs[i]);        
            /*
            if(value != null)
                count = value;
            count++;
            tm.put(char[i], count);
            count = 0;
            */
            if(value == null)
                tm.put(chs[i],1);
            else{
                value = value +1;
                tm.put(chs[i],value);
            }
        }
        // System.out.println(tm);
         StringBuffer sb = new StringBuffer();
         Set<Character> set = tm.keySet();
         Iterator<Character> it = set.iterator();
         while(it.hasNext()){
             char key = it.next();
             int value = tm.get(key);
             sb.append(key+"("+value+")");
         }
        return sb.toString();
    }  
     
}
posted @ 2013-04-06 00:16  郭彦君  阅读(129)  评论(0编辑  收藏  举报