sort of descendant linkedhashmap by value


i have 3 hashmap :

Map<String, HashMap<String, Integer>> tokenUnikTF= new LinkedHashMap<>();
Map<String, HashMap<String, Integer>> tokenUnikDF= new LinkedHashMap<>();
Map<String, HashMap<String, Double>> tokenUnikWeight= new LinkedHashMap<>();

the difference between the 3 hashmap above only in value, their key is the same.

here's how i add tokenUnikWeight's key and value :

for (String key5 : tokenUnikTF.keySet()) {
    tokenUnikWeight.put(key5, calculateWeight());

i want to print them all together, but in descending order by tokenUnikWeight's value

here's my sorting code :

public static LinkedHashMap<String,Double> sortHashMap() {
    List<Map.Entry<String, Double>> list = new LinkedList<>(tokenUnikWeight.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {

        public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
            return (o1.getValue()).compareTo(o2.getValue());

    Map<String, Double> result = new LinkedHashMap<>();
    for (Map.Entry<String, Double> entry : list) {
        result.put(entry.getKey(), entry.getValue());
    return (LinkedHashMap<String, Double>) result;


and here is my print code :

tokenUnikWeight = sortHashMap();

for (String key5 : tokenUnikWeight.keySet()) {
   System.out.printf("%s - %d - %d - %f\n", key5, tokenUnikTF.get(key5), tokenUnikDF.get(key5), tokenUnikWeight.get(key5));

the output is i got null in some tf and df also my weight is sorted in ascending not descending :

someone help me please, i do lot of googling but still the same, it return null in some tf and df.

change your comparator to : return (o2.getValue()).compareTo(o1.getValue()); for descending order. As for null values, are you sure that those keys are present and their values are not null in tokenUnikTF, DF?