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的倒序时间排序了(最新的放最前面)