Map 4种集合遍历

一、HashMap<K,V> 存储数据采用的哈希表结构,元素的存取顺序不能保证一致(怎么存不一定怎么取);

 

       LinkedHashMap<K,V> 存储数据采用的哈希表结构+链表结构。通过链表结构可以保证元素的存取顺序一致(怎么存怎么取);

二、以自定义类型进行遍历

把get和set方法点出来 tostring方法点出来 hashcode方法点出来 不写的话重复元素可以存入;

 

public class Student {
    private String name;
    private int age;
    public Student() {
        super();
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    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 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;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}

 

public class Demo88 {

    public static void main(String[] args) {
        Map<Student,String> map=new HashMap<Student,String>();
        map.put(new Student("詹姆斯",35) ,"洛杉矶湖人队");              //存储数据 使用put关键字
        map.put(new Student("伦纳德",28) ,"洛杉矶快船队");
        map.put(new Student("恩比德",26) ,"费城76队");
        map.put(new Student("戈贝尔",24) ,"犹他爵士");
        // 增强for 获取key所在的集合
        Set<Student> set = map.keySet();                               //使用keyset关键字
        for(Student s:set){
            System.out.println(s+"...."+map.get(s));
        }
        System.out.println("=======================");
        //迭代器 
        Set<Student> set2 = map.keySet();  
        Iterator<Student> it=set2.iterator();
        while(it.hasNext()){
            Student s=it.next();
            System.out.println(s+"....."+map.get(s));
        }
        System.out.println("=======================");
        //3.entryset 增强for
        Set<Entry<Student, String>> set3 = map.entrySet();             //使用entryset关键字
        for(Entry<Student, String> e:set3){
            System.out.println(e.getKey()+"....."+e.getValue());
        }
        System.out.println("=======================");
        //4.entryset 迭代器
        Set<Entry<Student, String>> set4 = map.entrySet();
        Iterator<Entry<Student, String>> it1=set4.iterator();
        while(it1.hasNext()){
            Entry<Student, String> e=it1.next();
            System.out.println(e.getKey()+"......."+e.getValue());     //获取key,和values 分别使用getkey、getvalue关键字;
        }
    }
}

以上四种获取结果相同;

 

posted @ 2020-05-06 16:57  丿狂奔的蜗牛  阅读(194)  评论(0编辑  收藏  举报