Java 排序一个完整的HashMap

Java 排序一个完整的HashMap,java,android,sorting,collections,expandablelistview,Java,Android,Sorting,Collections,Expandablelistview,我正在尝试使用一个看似复杂的HashMap对象来在android中生成可扩展的Listview 我的HashMap的通用参数如下所示: //HashMap<Map<YEAR,MONTH>,List<DAYS>> HashMap<Map<Integer,Integer>,List<Integer> //HashMap HashMap事件=新建HashMap,List(); HashMap yearMonth=新建HashMap()

我正在尝试使用一个看似复杂的HashMap对象来在android中生成可扩展的Listview

我的HashMap的通用参数如下所示:

//HashMap<Map<YEAR,MONTH>,List<DAYS>>
HashMap<Map<Integer,Integer>,List<Integer>
//HashMap
HashMap事件=新建HashMap,List();
HashMap yearMonth=新建HashMap();
年-月.投入产出(2013,5);
events.put(yearMonth,Arrays.asList(新整数[]{12,20,25}));
我已经为我的可扩展列表视图创建了一个适配器,它显示得很好,如下所示。现在我想能够对上面的HashMap进行排序,首先是按年份和月份,这样我的listview将显示2014年的事件,然后是2013年、2012年。。。。按这个顺序

这可能吗

谢谢

好吧,我刚刚读到了“排序hasmap”。如果您确实想要对数据进行排序,那么hashmap肯定是错误的


也许您应该考虑使用linkedlist…

创建您自己的类而不是hashmap,并调整适配器以适合这些对象

然后,通过在类中实现Comparable并创建
compareTo()
方法,您可以实现自己的排序

这将为您提供所需的所有控制。例如:

public class myEvent implements Serializable, Comparable<myEvent>
{
  private Integer day;
  private Integer month;
  private Integer year;

  public myEvent( Integer day, Integer month, Integer year, <your other data> )
  {
     // Save the stuff here
     this.day = day; 
     this.month = month; 
     this.year = year; 
  }

  // Create getDay(), getMonth(), getYear() methods for each parameter

  public int compareTo( myEvent another )
  {
     // Here, compare the two events year by year, month by month, and day by day
     if ( this.year.compareTo( another.getYear() ) == 0 )
     {
         if ( this.month.compareTo( another.getMonth() ) == 0 )
         {
             return this.day.compareTo( another.getDay() );
         } else {
             return this.month.compareTo( another.getMonth() );
         }
     } else {
         return this.year.compareTo( another.getYear() );
     }
  }

}
公共类myEvent实现了可序列化、可比较的
{
私人整数日;
私人整数月;
私人整数年;
公共myEvent(整数天、整数月、整数年)
{
//把东西保存在这里
this.day=天;
本月=月;
今年=年;
}
//为每个参数创建getDay()、getMonth()和getYear()方法
公共整数比较(myEvent另一个)
{
//在这里,逐年、逐月和逐日比较这两个事件
if(this.year.compareTo(另一个.getYear())==0)
{
if(this.month.compareTo(另一个.getMonth())==0)
{
返回this.day.compareTo(另一个.getDay());
}否则{
返回this.month.compareTo(另一个.getMonth());
}
}否则{
返回this.year.compareTo(另一个.getYear());
}
}
}
编辑:当您想要对这些myEvent对象的列表进行排序时,可以使用Collection api来使用类似的实现:

List<myEvent> allevents = new ArrayList<myEvent>();
// Add to the list
...
// Now sort it. 
Collections.sort( allevents );
List allevents=new ArrayList();
//添加到列表中
...
//现在把它分类。
集合。排序(allevents);

祝你好运

如何在链表中表示年、月和列表映射?可能吗?再次感谢。直接映射是不可能的,但您可以通过嵌套的数据结构来实现。但是,如果这对您很重要,您应该检查是否有其他字典实现,然后可以使用hashmaps(我相信是的),请转到以下内容:您不能使用这样的HashMap。特别是,使用可变对象(映射)作为映射的键将是灾难性的。
List<myEvent> allevents = new ArrayList<myEvent>();
// Add to the list
...
// Now sort it. 
Collections.sort( allevents );