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; }}
原文地址:https://www.cnblogs.com/aojie/p/12369342.html
时间: 2024-11-02 16:12:32