hooyantsing's Blog

第34次课程

字数统计: 987阅读时长: 5 min
2019/07/11

源辰74班

第34次课程

07.11.2019

**内容    **

案例解析[蒋斌]

0.List Set Map的区别

b73f9ec541f933ad0b98a400e6272c7b.png

1.重复对象

list允许重复的对象,而set方法不允许重复对象

2.null元素

list可以插入多个null元素,而set只允许插入一个null元素

List:列表 Set:集合 Map:映射

1.HashMap类

01e7a3f3f333f6ef5e682e49a4fb2556.png

例题分析:

1.List声明对ArrayList的引用

ArrayList类(Class)实现(implement)了List接口(interface)

1
2
//List接口名<泛型> 变量名 = new ArrayList类名<泛型>();
List<Student> list = new ArrayList<Student>();

2.将元素添加到List中

List.add(对象)

3.创建一个HashMap

1
2
//Map<K(key值),V(value值)> 变量名 = new HashMap<K,B>(); 
Map<String,List<Student>> yc = new HashMap<String,List<Student>>();

4.将元素添加到HashMap中

HashMap.put(K,V)  ->K和V都是泛型变量(数据类型的变量)

5.获取HashMap中以Set打包好的Entry

**Hash.entrySet() ** ->返回的是Set

1
2
//参考:
Set<Map.Entry<String, List<Student>>> entrySet = yc.entrySet();

6.添加迭代器()

Set.iterator()

推断:Set是集合类,可以挨个遍历。

1
2
//迭代器接口名<泛型> 变量名 = Set.iterator()
Iterator<Entry<String,List<Student>>> iterator = entrySet.iterator();

7.获取HashMap中以Set打包好的Key(Entry内部类中的Key)

HashMap.KeySet()  ->返回的是Set

8.获取Entry中的Key和Value

Entry.getKey()

Entry.getValue()

Main TestHashMap

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pm.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TestHashMap {
    public static void main(String[] args) {
        List<Student> list1 = new ArrayList<Student>();
        for(int i=0;i<5;i++) {
            list1.add(new Student("张"+i,i+1));
        }
        List<Student> list2 = new ArrayList<Student>();
        for(int i=0;i<5;i++) {
            list2.add(new Student("李"+i,i+1));
        }
        List<Student> list3 = new ArrayList<Student>();
        for(int i=0;i<5;i++) {
            list3.add(new Student("王"+i,i+1));
        }
        Map<String,List<Student>> yc = new HashMap<String,List<Student>>();
        yc.put("一班", list1);
        yc.put("二班", list2);
        yc.put("三班", list3);
        System.out.println("班级共有"+yc.size());
        //循环遍历第一种方式
        /*Map中包含内部类Entry,调用entrySet()方法时,将所有创建好的Entry元素打包成Set返回。Set是
             种元素集合,可以通过Iterator迭代。因此,Set内部元素是Entry,Entry内部元素是Key和Value
        */
        int total = 0;
        Set<Map.Entry<String, List<Student>>> entrySet = yc.entrySet();
        Iterator<Entry<String,List<Student>>> iterator = entrySet.iterator();
        while(iterator.hasNext()) {
            Entry<String,List<Student>> entry = iterator.next();
            System.out.println("班级名"+entry.getKey());
            List<Student> value = entry.getValue();
            for(Student s:value) {
                System.out.println(s);
                total++;
            }
        }
        System.out.println("总共有:"+total+"个学生");
        System.out.println("总共有:"+yc.size()+"个班");
        //循环遍历第二种方式
        Set<String> keySet = yc.keySet();
        Iterator<String> iterator2 = keySet.iterator();
        while(iterator2.hasNext()) {
            String key = iterator2.next();
            System.out.println(key);
            List<Student> list = yc.get(key);
            for(Student student : list) {
                System.out.println(student);
            }
            if(yc.containsKey("一班")) {
                System.out.println("这个班存在");
            }else {
                System.out.println("班不存在");
            }
        }
    }
}

Class Student

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package pm.map;
class Student implements Comparable {
    private String name;
    private int age;
    public Student() {
        super();
    }
    public Student(String name, int age)  {
        super();
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name +  ",  age=" + age + "]";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name  ==  null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
               return false;
        } else if  (!name.equals(other.name))
            return false;
        return true;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public int compareTo(Object o) {
        if(!(o instanceof Student)) {
            throw new  RuntimeException("错误");
        }
        return   this.hashCode()-((Student)o).hashCode();
    }
}
CATALOG