天天看点

8.Map集合(HashMap&&TreeMap)

一、Map集合概述和使用

1.Map集合概述

Interface Map<K,V> K:键的类型 V:值的类型

将键映射到值的对象;不能包含重复的键;每个键可以映射到最多一个值

创建Map集合的对象,采用的是多态的方式,具体的实现类HashMap集合

Map集合是双列集合,一个键对应一个值,键不可以重复,值可以重复

2.Map集合的基本功能

8.Map集合(HashMap&amp;&amp;TreeMap)

3.Map集合的获取功能

8.Map集合(HashMap&amp;&amp;TreeMap)

4.Map集合的两种遍历方式

8.Map集合(HashMap&amp;&amp;TreeMap)
package com.company;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Main {

    public static void main(String[] args) {

        Map<String,String> map = new HashMap<>();

        map.put("杨过","小龙女");
        map.put("郭靖","杨蓉");
        map.put("迪丽热巴","古力娜扎");

        //遍历方式一
        //首先创建Set集合,获得map的所有键
        Set<String> keySet = map.keySet();
        //使用增强for循环,进行遍历
        for (String key : keySet) {
            String value = map.get(key);
            System.out.println(key+" "+value);
        }

        System.out.println("=================");

        //遍历方式二
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+" "+value);
        }

    }
}

           

二、TreeMap集合

1.TreeMap集合概述和特点

(1) TreeMap底层是红黑树结构

(2) 依赖自然排序或者比较器排序,对键进行排序

(3) 如果键存储的是自定义对象,需要实现Comparable接口或者创建TreeMap对象时候给出比较器排序规则

2.案例实现

创建一个TreeMap集合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,按照年龄进行排序并遍历

要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序

学生类

public class Student implements Comparable<Student>{
      private String name;
      private int age;

      public Student() {
      }

      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 int compareTo(Student o) {
          //按照年龄进行排序
          int result = o.getAge() - this.getAge();
          //次要条件,按照姓名排序。
          result = result == 0 ? o.getName().compareTo(this.getName()) : result;
          return result;
      }
  }
           

测试类

public class Test1 {
      public static void main(String[] args) {
        	// 创建TreeMap集合对象
          TreeMap<Student,String> tm = new TreeMap<>();
        
  		// 创建学生对象
          Student s1 = new Student("xiaohei",23);
          Student s2 = new Student("dapang",22);
          Student s3 = new Student("xiaomei",22);
        
  		// 将学生对象添加到TreeMap集合中
          tm.put(s1,"江苏");
          tm.put(s2,"北京");
          tm.put(s3,"天津");
        
  		// 遍历TreeMap集合,打印每个学生的信息
          tm.forEach(
                  (Student key, String value)->{
                      System.out.println(key + "---" + value);
                  }
          );
      }
  }