map,以及底层存储分析

本章内容:map的底层原理,底层存储过程,hashmap\treemap的使用

map(键值对):有hashMap实现类:hashMap底层实现了哈希表,这是一个非常重要的数据结构。
 *               哈希表:基本结构是“数组+链表”,将两个的优点结合起来了。
 *                链表:指的是entry类型的对象,每一个entry对象就是一个单向链表结构,存储结构:
 *                 hash,key,value,next  hash:键对象的hash值; next:下一个节点(链表)
 *                数组:entry链表放在entry[]数组里,默认大小是16.
 *             treeMap:树结构。
 *  如何存储:1.存储map对象的时候,先通过对象.hashCode()方法,获得hashCode,也就是一串号码
 *           2.根据hashCode计算出hash值(要求在(0,数组长度-1)区间);hashCode是一个整
 *           数,需要将它转换为(0,数组长度-1)区间,转化后的hash值尽量均匀的分布在(0,
 *           数组长度-1)区间这个区间,减少“hash冲突”
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class TestMap {
    public static void main(String[] args) {
        test3();
    }

    //hashmap常用方法
    public static void test1(){
        Map<Integer,String> m1= new HashMap<>();
        Map<Integer,String> m2= new HashMap<>();
        m1.put(1,"a");//设值
        m1.put(2,"b");
        m1.put(3,"c");
        m1.put(4,"d");

        m2.put(5,"one");
        m2.put(6,"two");

        String s =m1.get(2);
        System.out.println(m1.size());
        System.out.println(m1.containsKey(2));
        System.out.println(m1.containsValue("d"));
        m2.put(4,"four");//map中键值不能重复,新的会把旧的覆盖
        m1.putAll(m2);
        System.out.println(m1);
    }
    //hashmap常用方法
    public static void test2(){
        Employee e1 = new Employee(01,"小白",8000);
        Employee e2 = new Employee(02,"小花",80000);
        Employee e3 = new Employee(03,"小黑",10000);

        Map<Integer,Employee> map = new HashMap<>();
        map.put(001,e1);
        map.put(002,e2);
        map.put(003,e3);
        System.out.println(map.hashCode());
    }

    //treemap
    public static void test3(){
        Map<Integer,String> tm = new TreeMap<>();
        tm.put(9,"h");
        tm.put(3,"f");
        tm.put(10,"k");

        //按照key递增的方式排序
        for(Integer key:tm.keySet()){
              System.out.println(key+"---"+tm.get(key));
        }

        Map<Emp,String> empMap = new TreeMap<>();
        empMap.put(new Emp(100,"jack",30000),"工作努力");
        empMap.put(new Emp(10,"tom",3000),"工作偷懒");
        empMap.put(new Emp(500,"mile",10000),"工作还行");

        //根据salary递增来排序
        for(Emp key:empMap.keySet()){
            System.out.println(key+"---"+empMap.get(key));
        }

    }

}

class Emp implements Comparable<Emp>{
    int id ;
    String name;
    double salary;

    public Emp(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }

    @Override
    public int compareTo(Emp o) {//正数:大于;负数:小于;0:等于
        if(this.salary > o.salary){
            return 1;
        }else if(this.salary < o.salary){
            return -1;
        }else{
            if(this.id > o.id){
                return 1;
            }else if(this.id<o.id){
                return -1;
            }else{
                return 0 ;
            }
        }
    }
}
//雇员信息
class Employee{
    private int id ;
    private String name;
    private double salary;

    public Employee(int id,String name,double salary){
        this.id = id;
        this.name = name;
        this.salary = salary;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值