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

Set集合:是一个无序的但不重复的容器

         练习一:在HashSet中存入自定义元素,要求名字和年龄相同则为重复元素,不能存入;

               提示:1、自定义类中必须复写equals方法和hashCode方法,因为集合中保证元素不重复的判断依据是:先判断元素的hashCode值,然后才判断元素的内容;

                         2、与LinkedList的区别是多复写一个hashCode方法;

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
class Person{
    private String name;
    private int age;
    Person(String name, int age){
        this.name = name;
        this.age = age;
    }
    public int hashCode(){
        return this.name.hashCode() + age;
    }
    public boolean equals(Object obj){
        if(!(obj instanceof Person))
            return false;
        Person p = (Person)obj;
        return this.name.equals(p.name) && this.age == p.age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
}
class SetTest{
    public static void main(String [] args){
        HashSet hs = new HashSet();
          
        hs.add(new Person("zhangsan01", 21));
        hs.add(new Person("zhangsan02", 22));
        hs.add(new Person("zhangsan02", 22));
        hs.add(new Person("zhangsan03", 23));
        hs.add(new Person("zhangsan04", 24));
          
        Iterator it = hs.iterator();
        while(it.hasNext()){
            Person p = (Person)it.next();
            System.out.println(p.getName()+"----"+p.getAge());
        }
    }
}

练习二:在TreeSet中存入自定义元素,要求不能有重复;

      提示:1、自定义类的时候要实现Comparable接口,并复写comparaTo方法,该方法中将obj向下转型,然后判断条件返回整数;

                2、与LinkedList的区别是该列表需要复写equals方法;

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
class SetTest{
    public static void main(String [] args){       
        TreeSet ts = new TreeSet();
         
        ts.add(new Student("zhangsan01", 21));
        ts.add(new Student("zhangsan02", 22));
        ts.add(new Student("zhangsan03", 23));
        ts.add(new Student("zhangsan03", 23));
        ts.add(new Student("zhangsan04", 24));
         
        Iterator it = ts.iterator();
        while(it.hasNext()){
            Student st = (Student)it.next();
            System.out.println(st.getName()+ "---"+ st.getAge());
        }  
    }
}
class Student implements Comparable{
    private String name;
    private int age;
    Student(String name, int age){
        this.name = name;
        this.age = age;
    }
    public int compareTo(Object obj){
        if(!(obj instanceof Student))
            throw new ClassCastException("对象不是学生");
        Student stu = (Student)obj;
        if(this.age > stu.age)
            return 1;
        if(this.age == stu.age){
            return this.name.compareTo(stu.name);
        }      
        return -1;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return 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
45
class Student //implements Comparable
{
    private String name;
    private int age;
    Student(String name, int age){
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
}
class myComparator implements Comparator{
    public int compare(Object obj1, Object obj2){      
        Student st1 = (Student)obj1;
        Student st2 = (Student)obj2;
         
        int num = st1.getName().compareTo(st2.getName());
        if(num == 0){
            return new Integer(st1.getAge()).compareTo(new Integer(st2.getAge()));         
        }
        return num;
    }
}
class SetTest{
    public static void main(String [] args){       
        TreeSet ts = new TreeSet(new myComparator());
         
        ts.add(new Student("zhangsan03", 26));
        ts.add(new Student("zhangsan01", 21));
        ts.add(new Student("zhangsan05", 22));
        ts.add(new Student("zhangsan03", 23));
        ts.add(new Student("zhangsan03", 23));
        ts.add(new Student("zhangsan04", 24));
         
        Iterator it = ts.iterator();
        while(it.hasNext()){
            Student st = (Student)it.next();
            System.out.println(st.getName()+ "---"+ st.getAge());
        }  
    }
}

练习四:自定义比较器,按照字符串的长度进行排序

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
class SetTest{
    public static void main(String [] args){
        TreeSet ts = new TreeSet(new myComparator());
        ts.add("abd");
        ts.add("abc");
        ts.add("abdc");
        ts.add("g");
        ts.add("werer");
        ts.add("ui");
         
        Iterator it = ts.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}
class myComparator implements Comparator{
    public int compare(Object obj1, Object obj2){
        String s1 = (String)obj1;
        String s2 = (String)obj2;
         
        int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
        if(num == 0){
            return s1.compareTo(s2);
        }
        return num;
    }
}
posted @ 2013-04-06 00:15  郭彦君  阅读(133)  评论(0编辑  收藏  举报