Multiple markers at this line

1

I have several warnings with the following caption:

**Multiple markers at this line

- Map is a raw type. References to generic type Map<K,V> should be 
 parameterized
- Map is a raw type. References to generic type Map<K,V> should be 
 parameterized**

Does anyone know how to declare correctly?

This is my code.

 static Map ordenarPorValue(Map map) {

    List list = new LinkedList(map.entrySet());

    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
               return ((Comparable) ((Map.Entry) (o1)).getValue())
              .compareTo(((Map.Entry) (o2)).getValue());
          }
     });

    Map result = new LinkedHashMap();

    for (Iterator it = list.iterator(); it.hasNext();) {
        Map.Entry  entry = (Map.Entry)it.next();
        result.put(entry.getKey(), entry.getValue());
    }

    return result;
} 
    
asked by DANIEL 08.11.2018 в 15:12
source

2 answers

0

Your method requires that the values are comparable, so you could declare it as follows:

static <T extends Comparable<T>> Map<?, T> ordenarPorValue(Map<?, T> map) {

    List<Entry<?, T>> list = new LinkedList<>(map.entrySet());

    Collections.sort(list, new Comparator<Entry<?, T>>() {
        public int compare(Entry<?, T> o1, Entry<?, T> o2) {
               return o1.getValue().compareTo(o2.getValue());
          }
     });

    Map<Object, T> result = new LinkedHashMap<Object, T>();

    for (Iterator<Entry<?, T>> it = list.iterator(); it.hasNext();) {
        Entry<?, T> entry = it.next();
        result.put(entry.getKey(), entry.getValue());
    }

    return result;
} 
    
answered by 08.11.2018 в 15:56
0

If you do not want to say what type of object you can use:

@SuppressWarnings({ "unchecked", "rawtypes" })
public void ..
    
answered by 08.11.2018 в 16:06