Java Map Reverse sortByUsedSpace(Map> dataNodes, boolean reverse)

Here you can find the source of sortByUsedSpace(Map> dataNodes, boolean reverse)

Description

sort By Used Space

License

Apache License

Declaration

public static Map<String, Long> sortByUsedSpace(Map<String, Map<Long, Long>> dataNodes, boolean reverse) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import static java.util.Collections.reverseOrder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static Map<String, Long> sortByUsedSpace(Map<String, Map<Long, Long>> dataNodes, boolean reverse) {
        Map<Long, List<String>> sorted = sort(dataNodes, reverse);
        Map<String, Long> result = new LinkedHashMap<>();
        for (long space : sorted.keySet()) {
            List<String> hosts = sorted.get(space);
            for (String host : hosts) {
                result.put(host, space);
            }/*  ww  w  .j  ava  2  s.  co  m*/
        }
        return result;
    }

    private static Map<Long, List<String>> sort(Map<String, Map<Long, Long>> dataNodes, boolean reverse) {
        Map<Long, List<String>> result = getSortedMap(reverse);
        for (String hostName : dataNodes.keySet()) {
            Map<Long, Long> usage = dataNodes.get(hostName);
            long space = usage.values().iterator().next();
            List<String> hosts = result.get(space);
            if (hosts == null) {
                hosts = new ArrayList<>();
            }
            hosts.add(hostName);
            result.put(space, hosts);
        }
        return result;
    }

    private static Map<Long, List<String>> getSortedMap(boolean reverse) {
        if (reverse) {
            return new TreeMap<>(reverseOrder());
        } else {
            return new TreeMap<>();
        }
    }
}

Related

  1. reverseMapLookup(Map map, V value)
  2. reverseMapping(Map value2keys, Map map)
  3. reverseMapping(Map map)
  4. sort(Map> dataNodes, boolean reverse)
  5. sort(Map source, boolean reverse)
  6. sortByValue(Map map, boolean reverse)
  7. sortByValue(Map map, boolean doReverse)
  8. toReverseIndex( Map> forwardIndex)