首页 > 其他分享 >TreeMap

TreeMap

时间:2023-06-19 21:05:44浏览次数:51  
标签:map TreeMap System put key println out

TreeMap的使用

public static void main(String[] args) {

		TreeMap<String,Integer> map = new TreeMap<>();

		//添加元素
		Integer put1 = map.put("大文", 25);
		Integer put2 = map.put("小文", 26);
		Integer put3 = map.put("小王", 29);
		Integer put4 = map.put("小李", 28);
		Integer put5 = map.put("小博", 21);
		Integer put6 = map.put("小黄", 26);
		System.out.println("put1:" + put1);//null
		System.out.println("put2:" + put2);//null
		System.out.println("put3:" + put3);//null
		System.out.println("put4:" + put4);//null
		System.out.println("put5:" + put5);//null
		System.out.println("put6:" + put6);//null


		//替换,返回被替换的值
		Integer put = map.put("小黄", 27);
		System.out.println("put:" + put);//26

		//替换,返回被替换的值
		Integer replace1 = map.replace("小黄", 28);
		System.out.println("replace1:" + replace1);//27

		//替换,返回是否替换成功的boolean值
		boolean replace2 = map.replace("小黄", 28, 29);//key - 被替换值 -替换值
		System.out.println("replace2:" + replace2);//true

		//将newMap1中所有的映射关系添加到map集合中
		TreeMap<String, Integer> newMap1 = new TreeMap<>();
		newMap1.put("aaa", 10);
		newMap1.put("bbb", 20);
		newMap1.put("ccc", 30);
		newMap1.put("ddd", 40);
		newMap1.put("ddd", 50);
		map.putAll(newMap1);

		//如果key存在则返回value,如果key不存在就添加
		Integer putIfAbsent = map.putIfAbsent("小王", 123);
		System.out.println("putIfAbsent:" + putIfAbsent);//29

		//通过key获取对应的value值
		Integer integer = map.get("小李");
		System.out.println("通过key获取对应的value值:" + integer);

		//通过key获取对应的value值,如果没有key就返回默认值666
		Integer orDefault = map.getOrDefault("小王111", 666);
		System.out.println("通过key获取对应的value值:" + orDefault);

		//清空所有的元素
		//map.clear();

		System.out.println("判断map集合中是否包含某个key:" + map.containsKey("小王"));//true
		System.out.println("判断map集合中是否包含某个value:" + map.containsValue(28));//true
		System.out.println("判断map集合是否有元素:" + map.isEmpty());//false
		//有元素就返回false

		//根据key删除映射关系
		map.remove("小博");
		//根据key+value删除映射关系
		map.remove("小李", 28);

		System.out.println("获取集合元素个数:" + map.size());//8

		//获取map集合中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));//集合->数组->字符串

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

		//遍历 -- keySet()
		//思路:获取map中所有的key,存入Set集合中,遍历Set集合依次把key获取出来,利用map.get(key)获取出对应的value值
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Integer value = map.get(key);
			System.out.println(key + "--" + value);//获取出来的也是无序的

		}
		System.out.println("--------------------");

		//遍历 - entrySet()
		//思路:获取map中所有的映射关系对象(Entry),存入Set集合,遍历Set集合依次把Entry取出来,获取Entry中的key和value
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + "--" + value);
		}
		
	}

TreeMap的特点

特点:key自然排序

public static void main(String[] args) {
		
		TreeMap<String, Integer> map = new TreeMap<>();
		
		map.put("b", 10);
		map.put("a", 20);
		map.put("c", 30);
		map.put("d", 40);
		map.put("e", 50);
		map.put("e", 60);
		
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println(key + " -- " + value);
		}
	}

内置与外置比较器

外置比较器

TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {//new一个外置比较和对象

内置比较器

TreeMap<Student, String> map = new TreeMap<>();
注意:在使用比较器时都需要写一个对象类


标签:map,TreeMap,System,put,key,println,out
From: https://blog.51cto.com/u_16154651/6517154

相关文章

  • Map系列集合:TreeMap集合的原理、使用
        ......
  • java treemap
    TreeMap是Java中的一个类,它实现了Map接口,利用红黑树数据结构来有序存储键值对。TreeMap中的键按升序排序,若要自定义排序方式,则可以提供自定义的比较器。TreeMap实现了高效的数据访问、插入和删除操作,大多数常规操作的时间复杂度为O(logn)。importjava.util.TreeMap;public......
  • Java-Day-19( 对集合实现类的选择 + TreeSet + TreeMap )
    Java-Day-19总结-开发中如何选择集合实现类在开发中,选择什么集合实现类,主要取决于业务操作特点,然后根据集合实现类特性进行选择先判断存储的类型(一组对象或一组键值对)一组对象(单列):Collection接口允许重复:List增删多:LinkedList[底层维护了一个双向链......
  • 如何决定使用 HashMap 还是 TreeMap?
    @[toc]问:如何决定使用HashMap还是TreeMap?介绍TreeMap<k,v>的Key值是要求实现java.lang.Comparable,所以迭代的时候TreeMap默认是按照Key值升序排序的;TreeMap的实现是基于红黑树结构。适用于按自然顺序或自定义顺序遍历键(key)。HashMap<k,v>的Key值实现散列hashCode(),分布是散列的......
  • echarts treemap当份额太小时文字显示不全,解决为垂直显示全部文字
    before:after:解决: ......
  • Map - TreeSet & TreeMap 源码解析
    Java7-TreeSet&TreeMap总体介绍前者仅仅是对后者做了一层包装,也就是说TreeSet里面有一个TreeMap(适配器模式)。因此本文将重点分析TreeMap。JavaTreeMap实现了SortedMap接口,也就是说会按照key的大小顺序对Map中的元素进行排序,key大小的评判可以通过其本身的自然顺序(natu......
  • java 用 Java 将 HashMap 转换为 TreeMap 的程序
    转载自:https://www.moonapi.com/news/24923.html HashMap 是Java1.2以来Java集合的一部分。它提供了以(键、值)对存储数据的JavaMap接口的基本实现。要访问HashMap中的值,必须知道它的键。哈希映射被称为哈希映射,因为它使用哈希技术来存储数据。Java中的树图和抽象......
  • 一篇文章让你面试畅谈HashMap,Hashtable,TreeSet,TreeMap
    HashMapMap与Collection并列存在。用于保存具有映射关系的数据:Key-ValueMap中的key和value可以是任何引用类型,会封装到HashMap$Node对象中Map中的key不允许重复,原因和HashSet一样,前面分析过源码Map中的value可以重复Map的key可以是null,value也可以为null,注意key为null只能有一个,val......
  • TreeMap源码
          常见面试题:    ......
  • TreeMap
        ......