2020.02.26 TreeMap集合

Posted aojie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020.02.26 TreeMap集合相关的知识,希望对你有一定的参考价值。

package com.guoyun.bean;


import java.sql.Time;

/**
* ClassName:
* Function: ADD FUNCTION
* Reason: ADD REASON
*
* @author
* @Date
* @since Ver 1.1
*/
public class TimeBean implements Comparable<TimeBean>{
//年
public int year;
//描述信息
public String desc;


@Override
public int compareTo(TimeBean o) {
//升序
return year-o.year;
}
}
*********************************************************************
package com.guoyun.view;

import com.guoyun.bean.TimeBean;

import java.io.Serializable;
import java.util.*;

/**
* ClassName:
* Function: ADD FUNCTION
* Reason: ADD REASON
*
* @author
* @Date
* @since Ver 1.1
*/
public class MainView {
public static void main(String[] args) {
TreeMap<Integer,String> map1=new TreeMap<>();
AbstractMap<Integer,String> map2=new TreeMap<>();
Map<Integer,String> map3=new TreeMap<>();
NavigableMap<Integer,String> map4=new TreeMap<>();
SortedMap<Integer,String> map5=new TreeMap<>();
Cloneable map6=new TreeMap<>();
Serializable map7=new TreeMap<>();
map3.put(1,"一个不知名上单");
map3.put(2,"一个不知名打野");
map3.put(3,"一个不知名中单");
map3.put(4,"一个不知名AD");
map3.put(5,"一个不知名辅助");
// System.out.println(map3.get(5));
Set<Integer> set=map3.keySet();
//String和包装类实现自动排序
for (Integer i:set
) {
// System.out.println(i+" "+map3.get(i));
}
Map<TimeBean,String> map8=new TreeMap<>(new MyComparator());
TimeBean t1=new TimeBean();
t1.year=1999;
t1.desc="Birthday";
TimeBean t2=new TimeBean();
t2.year=2000;
t2.desc="After";
TimeBean t3=new TimeBean();
t3.year=1998;
t3.desc="Your";
map8.put(t1,"aojiedechushengriqi");
map8.put(t2,"aojietayisuile");
map8.put(t3,"mourendeshengri");
Set<Map.Entry<TimeBean,String>> set1=map8.entrySet();
for (Iterator<Map.Entry<TimeBean, String>> iterator = set1.iterator(); iterator.hasNext(); ) {
Map.Entry<TimeBean, String> next = iterator.next();
System.out.println(next.getKey().year+" "+next.getValue());
}

}
}
****************************************************
package com.guoyun.view;

import com.guoyun.bean.TimeBean;

import java.util.Comparator;

/**
* ClassName:
* Function: ADD FUNCTION
* Reason: ADD REASON
*
* @author
* @Date
* @since Ver 1.1
*/
public class MyComparator implements Comparator<TimeBean> {
@Override
public int compare(TimeBean o1, TimeBean o2) {
return o1.year-o2.year;
}
}

以上是关于2020.02.26 TreeMap集合的主要内容,如果未能解决你的问题,请参考以下文章

Map集合:HashMap、TreeMap

集合框架系列 Map:TreeMap(1.8)

集合——Map

Map:HashMap和TreeMap

简单比较HashMap和TreeMap

Java集合框架 Map接口实现类--TreeMap的使用 & TreeMap和TreeSet的关系