java基础之LinkedHashMap类

java基础之LinkedHashMap类,第1张

java基础之LinkedHashMap类

目录

一:概念

二:例子

1:key与value是Integer和String

2:key是Integer,value 是Student

3:key是Student,value 是Integer


一:概念

是实现了Map接口,底层是依赖于哈希表和链表的,具有可预知的遍历顺序

哈希表保证唯一性,保证的是Key的唯一性

链表保证有序,保证的是键的有序(存储和取出顺序一致)

(再次输入一样的key然后输入vaule时会更新值)

注意:linkedHashMap类继承于HashMap而HashMap继承于Map所以重写了他们具有的方法

二:例子 1:key与value是Integer和String
package protice5;

import java.util.linkedHashMap;
import java.util.Map;
import java.util.Set;

public class Test5 {
    public static void main(String[] args) {
        //创建对象指定泛型
        linkedHashMap i = new linkedHashMap();
        //添加元素
        i.put(1, "hello");
        i.put(2, "world");
        i.put(3,  "java");
        i.put(4, "hadoop");
        //遍历得到key和value并输出
        Set> entries = i.entrySet();
        for (Map.Entry entry : entries) {
            System.out.println(entry.getKey()+"**"+entry.getValue());
        }

    }
}

2:key是Integer,value 是Student
package protice5;

import java.util.Objects;

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    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 String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, age);
    }
}
package protice5;

import java.util.linkedHashMap;
import java.util.Map;
import java.util.Set;

public class Test6 {
    public static void main(String[] args) {
        //创建linkedHashMap对象并指定泛型
        linkedHashMap i = new linkedHashMap();
        //创建学生对象
        Student s1 = new Student("小花", 19);
        Student s2 = new Student("小花", 19);
        Student s3 = new Student("小李", 20);
        Student s4 = new Student("小可", 17);
        //向map中添加元素
        i.put( 2,s1);
        i.put( 7,s2);
        i.put( 6,s3);
        i.put( 1 ,s4);
        //遍历得到key和value
        Set> entries = i.entrySet();
        for (Map.Entry entry : entries) {
            System.out.println(entry.getKey()+"**"+entry.getValue());
        }
    }
}

3:key是Student,value 是Integer
package protice5;

import java.util.linkedHashMap;
import java.util.Map;
import java.util.Set;

public class Test4 {
    public static void main(String[] args) {
        linkedHashMap s = new linkedHashMap();
        Student s1 = new Student("小花", 19);
        Student s2 = new Student("小花", 19);
        Student s3 = new Student("小李", 20);
        Student s4 = new Student("小可", 17);
        s.put( s1,"001");
        s.put( s2,"002");
        s.put( s3,"003");
        s.put( s4,"004");
        Set> entries = s.entrySet();
        for (Map.Entry entry : entries) {
            System.out.println(entry.getKey()+"**"+entry.getValue());
        }

    }
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4829436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-10
下一篇 2022-11-10

发表评论

登录后才能评论

评论列表(0条)

保存