简体   繁体   中英

How to sort guava Multimap keys in reverse order?

I'm fairly new to Guava API and is trying to sort the keys of a MultiMap in reverse order or descending value. I'm initiating the Map in the following way:

ListMultimap<Date, Map<String, String>> listMultimap = MultimapBuilder.treeKeys().arrayListValues().build();

This sorts the keys in ascending. Eg:

List multi map iteration: key -->Fri Jan 01 00:00:00 PST 2016   values -->[{test2=testval2}, {test3=testval3}]
List multi map iteration: key -->Sun Jan 01 00:00:00 PST 2017   values -->[{test1=testval1}]
List multi map iteration: key -->Mon Jan 01 00:00:00 PST 2018   values -->[{test0=testval0}]
List multi map iteration: key -->Tue Jan 01 00:00:00 PST 2019   values -->[{test4=testval4}]

I tried creating a custom Date Comparator with TreeMultiMap , but haven't figure a way to do it. This is not syntactically right,but just trying to demonstrate the idea.

static final Comparator<Date> DESC_ORDER = new Comparator<Date>() {
    public int compare(Date e1, Date e2) { 
        return e2.compareTo(e1);
    }
};

SortedSetMultimap<Date, Map<String, String>> treeMultimap = TreeMultimap.create(DESC_ORDER);

Any pointers will be appreciated.

尝试使用treeKeys(Comparator)

MultimapBuilder.treeKeys(DESC_ORDER).arrayListValues().build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM