TreeMap

treemap

A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

当时我想用一个集合,该集合里装的对象按照这些对象的某属性排序

于是我用了额treemap ,把那个要排序的属性放到key里,并且用ReverseComparator制定了如何排序

Map<Date, Object> map = new TreeMap<Date,Object>(new ReverseComparator());
		Query query = getSession().createSQLQuery("select * from reply r where r.send_reply_person_id=? and r.sendcollect=? and r.sendrubbish=? or r.get_reply_person_id=? and r.getcollect=? and r.getrubbish=?").addEntity(Reply.class).setParameter(0, author.getId()).setParameter(1, true).setParameter(2, false).setParameter(3, author.getId()).setParameter(4, true).setParameter(5, false);
		List<Reply> replies = query.list();
		 
	     Query query2 = getSession().createSQLQuery("select * from messagebox m where m.sendperson_id=? and m.sendcollect=? and m.sendrubbish=? or m.getperson_id=? and m.getcollect=? and m.getrubbish=?").addEntity(MessageBox.class).setParameter(0, author.getId()).setParameter(1, true).setParameter(2, false).setParameter(3, author.getId()).setParameter(4, true).setParameter(5, false);
	     List<MessageBox>messageBoxs = query2.list();
	     
	     for(Reply r:replies){
	    	 map.put(r.getSendReplyTime(), r);
	     }
	     for(MessageBox m:messageBoxs){
	    	 map.put(m.getSendTime(), m);
	     }
	     System.out.println(map);

 

public class ReverseComparator implements Comparator<Date> {

	@Override
	public int compare(Date o1, Date o2) {
		return o2.hashCode()-o1.hashCode();
	}	

}


所得的map则按照对象的sendreplytime和sendtime的倒序时间排序了(最新的放最前面)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值