Java TreeMap使用和底层原理_Comparable接口_HashTable特点 尚学堂157

https://www.sxt.cn/Java_jQuery_in_action/nine-treemap.html

https://www.bilibili.com/video/BV1ct411n7oG?p=158


接口Map的实现类有:HashMap类、Hashtable类、TreeMap类:

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.TreeMap;

public class Test {
	public static void main(String[] args) {
		Map<Integer, String> m1 = new TreeMap<Integer, String>();
		Map<Integer, String> m2 = new HashMap<Integer, String>();
		Map<Integer, String> m3 = new Hashtable<Integer, String>();
	}
}

HashMap最常用,效率最高。

TreeMap一般不用,需要排序的时候可以用,底层使用了红黑二叉树。

TreeMap源码:

public class TreeMap<K,V>
    extends AbstractMap<K,V>
    implements NavigableMap<K,V>, Cloneable, java.io.Serializable
{......}

里面有:

private transient Entry<K,V> root;

Entry是他的一个内部类:

static final class Entry<K,V> implements Map.Entry<K,V> {
    K key;
    V value;
    Entry<K,V> left;
    Entry<K,V> right;
    Entry<K,V> parent;
    boolean color = BLACK;
    ......
}

可以看到里面存储了本身数据、左节点、右节点、父节点、以及节点颜色。 这是一个典型的红黑二叉树的实现。


TreeMap类有一个keySet方法,返回一个Set集合,里面存放着Key的值。

import java.util.Map;
import java.util.TreeMap;

public class Test {
	public static void main(String[] args) {
		Map<Integer, String> treemap1 = new TreeMap<>();
		treemap1.put(20, "aa");
		treemap1.put(3, "bb");
		treemap1.put(6, "cc");
		for(Integer key: treemap1.keySet()) {
			System.out.println(key + "---" + treemap1.get(key));
		}
	}
}

输出结果:

3---bb
6---cc
20---aa

可以看到他是按照Key值的递增顺序打印的。

按照Key递增的方式排序。


如果Key是自己写的类,自己写排序规则:

import java.util.Map;
import java.util.TreeMap;

public class Test {
	public static void main(String[] args) {
		Map<Emp, String> treemap = new TreeMap<>();
		treemap.put(new Emp(100, "张三", 50000), "他叫张三");
		treemap.put(new Emp(200, "李四", 5000), "他叫李四");
		treemap.put(new Emp(150, "王五", 6000), "他叫王五");
		treemap.put(new Emp(50, "赵六", 6000), "他叫赵六");
		
		//按照Key递增的方式排序
		for(Emp key: treemap.keySet()) {
			System.out.println(key + "---" + treemap.get(key));
		}
	}
}

class Emp implements Comparable<Emp>{//泛型里面是Emp,因为是Emp对象和Emp对象比较
	int id;
	String name;
	double salary;
	
	public Emp(int id, String name, double salary) {
		super();
		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) {
		/**
		 * 返回:
		 * 负数-小于
		 * 零-等于
		 * 正数-大于
		 * 一般用1、-1、0
		 */
		if(this.salary > o.salary) {
			return 1;
		}
		else if (this.salary < o.salary) {
			return -1;
		}else {//如果2个人工资一样,用id来排序
			if (this.id > o.id) {
				return 1;
			}
			else if (this.id < o.id) {
				return -1;
			}
			else {
				return 0;
			}
		}
	}
	
}

输出结果:

Emp [id=200, name=李四, salary=5000.0]---他叫李四
Emp [id=50, name=赵六, salary=6000.0]---他叫赵六
Emp [id=150, name=王五, salary=6000.0]---他叫王五
Emp [id=100, name=张三, salary=50000.0]---他叫张三


 HashMap与HashTable的区别:

1.HashMap:线程不安全,效率高。允许key或value为null。

2.HashTable:线程安全,效率低。不允许key或value为null。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值