码迷,mamicode.com
首页 > 其他好文 > 详细

2020.02.26 TreeMap集合

时间:2020-02-26 23:16:05      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:clone   cti   排序   rri   for   nav   ebean   system   val   

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+"\t"+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+"\t"+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集合

标签:clone   cti   排序   rri   for   nav   ebean   system   val   

原文地址:https://www.cnblogs.com/aojie/p/12369342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!