1

I would like to create a TreeMap with an anonymous class comparator that compares the values. But, I get an error. Where am I going wrong?

private List<Map<Integer,Integer>> edges;
/*
*   init edges database
*/
private void initEdges() {
    edges = new ArrayList<Map<Integer,Integer>>();
    for(int i = 0; i < this.amountOfVertices; i++) {
        edges.add(
                new TreeMap<Integer, Integer>(
                        new Comparator<Map.Entry<Integer, Integer>>() {
                            @Override
                            public int compare(Map.Entry<Integer, Integer> i1, Map.Entry<Integer, Integer> i2)
                            {
                                int res = i1.getValue().compareTo(i2.getValue());
                                return res != 0 ? res : 1;
                            }
                        }
                )
        );
    }
}

The exact error is

Error:(21, 21) java: no suitable constructor found for TreeMap(<anonymous java.util.Comparator<java.util.Map.Entry<java.lang.Integer,java.lang.Integer>>>)
    constructor java.util.TreeMap.TreeMap(java.util.Comparator<? super java.lang.Integer>) is not applicable
      (argument mismatch; <anonymous java.util.Comparator<java.util.Map.Entry<java.lang.Integer,java.lang.Integer>>> cannot be converted to java.util.Comparator<? super java.lang.Integer>)
    constructor java.util.TreeMap.TreeMap(java.util.Map<? extends java.lang.Integer,? extends java.lang.Integer>) is not applicable
      (argument mismatch; <anonymous java.util.Comparator<java.util.Map.Entry<java.lang.Integer,java.lang.Integer>>> cannot be converted to java.util.Map<? extends java.lang.Integer,? extends java.lang.Integer>)
    constructor java.util.TreeMap.TreeMap(java.util.SortedMap<java.lang.Integer,? extends java.lang.Integer>) is not applicable
      (argument mismatch; <anonymous java.util.Comparator<java.util.Map.Entry<java.lang.Integer,java.lang.Integer>>> cannot be converted to java.util.SortedMap<java.lang.Integer,? extends java.lang.Integer>)
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
PianistaMichal
  • 305
  • 1
  • 2
  • 14
  • Do not try to sort a `TreeMap` based on the values. http://stackoverflow.com/q/109383/3973077 – Paul Boddington Nov 25 '15 at 20:30
  • In this link You sends there is solution how to do this, but I would like to do this with anonymous class :/ – PianistaMichal Nov 25 '15 at 20:35
  • What are you trying to do exactly? Usually when people say they want a TreeMap sorted by values, it's an example of an XY problem. http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Paul Boddington Nov 25 '15 at 20:39
  • You want to sort a `Map` based on value with an anonymous class? Is that correct ? – gonzo Nov 25 '15 at 20:43
  • The result I would like to achieve is to have sorted collection in this way: var[vertex1][key=vertex2, value=distance] by its distance. So I could go with var[0].firstEntry().getKey(); and have vertex2 that is closest to vertex1. – PianistaMichal Nov 25 '15 at 20:51
  • 1
    @PianistaMichal It would be pretty easy to sort a Map by values using a TreeMap and a custom comparator, as long as it does not have to be an anonymous class. Anonymous classes can't have constructors so it is difficult to do. – gonzo Nov 25 '15 at 21:10

2 Answers2

4

I think the Comparator you give to a TreeMap<Integer,Integer> should know how to compare Integer, not Map.Entry objects.

Eric Lindauer
  • 1,813
  • 13
  • 19
2

A TreeMap is ordered by it's keys (not it's entries). You need something like,

edges.add(new TreeMap<Integer, Integer>(new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o1.compareTo(o2);
    }
}));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I know, but I need sort Tree by values. Is there is something wrong to do this? – PianistaMichal Nov 25 '15 at 20:37
  • Then it wouldn't be a `TreeMap`... It sounds like you want to create a `List>` (or maybe a `TreeSet`, if you need uniqueness).... then you can sort it with a `Comparator>`. – Elliott Frisch Nov 25 '15 at 20:39