LinkedHashMap和TreeMap集合的遍历

LinkedHashMap:键的数据结构是哈希表和链表,键唯一,而且有序, 哈希表保证唯一,链表保证有序;

LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
//自动根据键排序
        map.put(10,"aaa");
        map.put(20, "aaa");

        map.put(30, "aaa");

        map.put(40, "aaa");
        map.put(50, "aaa");
        map.put(60, "aaa");

        Set<Integer> integers = map.keySet();
        for (Integer integer : integers) {
            String value = map.get(integer);
            System.out.println(integer+"==="+value);
        }

TreeMap:键的数据结构是二叉树,可以对键进行排序,排序:自然排序,比较器排序。

		TreeMap<Integer, String> map = new TreeMap<>();
		//自动根据键值排序;
        map.put(10, "aaa");
        map.put(200, "aaa");

        map.put(30, "aaa");

        map.put(4330, "aaa");
        map.put(150, "aaa");
        map.put(160, "aaa");

        Set<Integer> integers = map.keySet();
        for (Integer integer : integers) {
            String value = map.get(integer);
            System.out.println(integer + "===" + value);
        }

TreeMap之自然排序:要求元素实现Comparable接口,重写compareTo方法;

代码如下:

public class MyTest2 {
    public static void main(String[] args) {
      
        TreeMap<Student, String> map = new TreeMap<>();
        map.put(new Student("张三", 23333), "s001");
        map.put(new Student("李四", 24), "s002");
        map.put(new Student("王五", 25), "s003");
        map.put(new Student("赵六", 26), "s004");
        map.put(new Student("张三", 23), "s005");

        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> en : entries) {
            System.out.println(en);
            Student key = en.getKey();
            String value = en.getValue();
            System.out.println(key+"===="+value);

        }
    }
}


//student类:
package org.westos.demo4;
import java.util.Objects;
public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student(String name, int age) {
        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 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);
    }

    @Override
    public int compareTo(Student o) {
        int i = this.age - o.age;
        int j=i==0?this.name.compareTo(o.name):i;
        return j;
    }
}


比较器排序:传入比较器排序:

学生类
public class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        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 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);
    }


}

测试类:


public class MyTest3 {
    public static void main(String[] args) {
        TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {
            @Override
            //比较器根据姓名长度排序,但是同时要比较姓名内容,和年龄,防止录入失败;
            public int compare(Student s1, Student s2) {
                int i = s1.getName().length() - s2.getName().length();
                int j=i==0?s1.getName().compareTo(s2.getName()):i;
                int h=j==0?s1.getAge()-s2.getAge():j;
                return h;
            }
        });
        map.put(new Student("张三aaa", 23333), "s001");
        map.put(new Student("李四bbbbbb", 24), "s002");
        map.put(new Student("王五", 25), "s003");
        map.put(new Student("赵六ddfdfd", 26), "s004");
        map.put(new Student("张三", 23), "s005");


        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> en : entries) {
            Student key = en.getKey();
            String value = en.getValue();
            System.out.println(key + "====" + value);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值