Sorting a Map by key (date,string)

Viewed 562
Map<XMLGregorianCalendar, String> SortedByTimeForJourney = new HashMap<XMLGregorianCalendar, String>();

I have to sort it by key(XMLGregorianCalendar). I tried this

 SortedByTimeForJourney.entrySet().stream().sorted(Map.Entry.<XMLGregorianCalendar, String>comparingByKey()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));

but it is showing Type Parameter 'javax.xml.datatype.XMLGregorianCalendar' is not withing its bound; should implement 'java.lang.Comparable'

I also tried to do by saving it in LinkedHashMap and also using Collections.util method but unable to do.

Someone Please Help me.

4 Answers

To sort a Map by key, use a TreeMap.

As the javadoc says:

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.

Since XMLGregorianCalendar doesn't implement Comparable, i.e. doesn't have a natural ordering, you have to specify a Comparator in the constructor.

In Java 8+, use:

new TreeMap<>(XMLGregorianCalendar::compare)

In older versions of Java, use:

new TreeMap<>(new Comparator<XMLGregorianCalendar>() {
    @Override
    public int compare(XMLGregorianCalendar cal1, XMLGregorianCalendar cal2) {
        return cal1.compare(cal2);
    }
})

Your problem is with key object XMLGregorianCalendar is not Comparable.

Type Parameter 'javax.xml.datatype.XMLGregorianCalendar' is not withing its bound; should implement 'java.lang.Comparable'

You can use Date which is a Comparable.

Use as follows:

Map<Date, Integer> sortedByTimeForJourney = new TreeMap<Date, Integer>();

Get the time from XMLGregorianCalendar:

[XMLGregorianCalendar Instance].toGregorianCalendar().getTime()

Example: sortedByTimeForJourney as default sorted based on date.

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class Main {

    public static void main(String[] args) throws DatatypeConfigurationException {
        Map<Date, Integer> sortedByTimeForJourney = new TreeMap<>();
        for (int i = 12; i > 0; i--) {
            XMLGregorianCalendar xCal = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2019, i, i, 0);
            sortedByTimeForJourney.put(xCal.toGregorianCalendar().getTime(), i);
        }
        System.out.println(sortedByTimeForJourney);
    }
}

The error message clearly states that - XMLGregorianCalendar doesn't implement Comparable and hence you won't be able to perform sorting operation. So, just to help you out quickly please refer to this post.

Also, using TreeMap is better option than Reinventing the wheel.

Based on Andreas answer you can use this:

Map<XMLGregorianCalendar, String> SortedByTimeForJourney = new TreeMap<>(Comparator.comparing(XMLGregorianCalendar::toGregorianCalendar));
Related