没有找到合适的构造函数用于TreeMap with Comparator <map.entry

时间:2015-11-25 20:24:44

标签: java generics comparator

=“”

我想创建一个带有匿名类比较器的TreeMap比较值。但是,我收到了一个错误。我哪里错了?

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;
                            }
                        }
                )
        );
    }
}

完全错误是

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>)

2 个答案:

答案 0 :(得分:4)

我认为您提供给TreeMap<Integer,Integer>的比较者应该知道如何比较Integer,而不是Map.Entry个对象。

答案 1 :(得分:2)

<?php namespace Ourentec\OurentecApiBundle\Controller\v1; use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; use Symfony\Component\HttpFoundation\Request; class CustomerController extends FOSRestController{ /** * @return array * @Rest\Get("/customers/{id}") * @Rest\View */ public function getCustomerAction($id) { $customer = $this->getDoctrine()->getRepository('OurentecBundle:Customer')->find($id); return array('customer' => $customer); } /** * * @return array * @Rest\Get("/customers/") * @Rest\View */ public function getUsersAction() { $customers = $this->getDoctrine()->getRepository('OurentecBundle:Customer')->findAll(); return array('customers' => $customers); } } 按其键排序(不是它的条目)。你需要像

这样的东西
TreeMap