private static LinkedHashMap sortMapByValue()

in opennlp-similarity/src/main/java/opennlp/tools/similarity/apps/utils/ValueSortMap.java [111:238]


  private static <K, V> LinkedHashMap<K, V> sortMapByValue(Map<K, V> inMap,
      Comparator<V> comparator, Boolean ascendingOrder) {
    int iSize = inMap.size();

    // Create new LinkedHashMap that need to be returned
    LinkedHashMap<K, V> sortedMap = new LinkedHashMap<>(iSize);

    Collection<V> values = inMap.values();
    List<V> valueList = new ArrayList<>(values); // To get List of all values in
                                                 // passed Map
    Set<V> distinctValues = new HashSet<>(values); // To know the distinct values
                                                  // in passed Map

    // Handling for null values: remove them from the list that will be used
    // for sorting
    int iNullValueCount = 0; // Total number of null values present in passed Map
    if (distinctValues.contains(null)) {
      distinctValues.remove(null);
      for (int i = 0; i < valueList.size(); i++) {
        if (valueList.get(i) == null) {
          valueList.remove(i);
          iNullValueCount++;
          i--;
        }
      }
    }

    // Sort the values of the passed Map
    if (ascendingOrder == null) {
      // If Boolean ascendingOrder is null, use passed comparator for order of
      // sorting values
      valueList.sort(comparator);
    } else if (ascendingOrder) {
      // If Boolean ascendingOrder is not null and is true, sort values in
      // ascending order
      valueList.sort(comparator);
    } else {
      // If Boolean ascendingOrder is not null and is false, sort values in
      // descending order
      valueList.sort(comparator);
      Collections.reverse(valueList);
    }

    // Check if there are multiple same values exist in passed Map (not
    // considering null values)
    boolean bAllDistinct = true;
    if (iSize != (distinctValues.size() + iNullValueCount))
      bAllDistinct = false;

    K key;
    V value, sortedValue;
    Set<K> keySet;
    Iterator<K> itKeyList;
    Map<K, V> hmTmpMap = new HashMap<>(iSize);
    Map<K, V> hmNullValueMap = new HashMap<>();

    keySet = inMap.keySet();
    itKeyList = keySet.iterator();// Add Null Values in the last of the LinkedHasMap
    if (bAllDistinct) {
      // There are no multiple same values of the passed map (without considering null)
      while (itKeyList.hasNext()) {
        key = itKeyList.next();
        value = inMap.get(key);

        if (value != null)
          hmTmpMap.put((K) value, (V) key); // Prepare new temp HashMap with value=key combination
        else
          hmNullValueMap.put(key, value); // Keep all null values in a new temp Map
      }

      if (ascendingOrder != null && !ascendingOrder) {
        // As it is descending order, Add Null Values in first place of the
        // LinkedHasMap
        sortedMap.putAll(hmNullValueMap);
      }

      // Put all not null values in returning LinkedHashMap
      for (V o : valueList) {
        value = o;
        key = (K) hmTmpMap.get((V) value);

        sortedMap.put(key, value);
      }

    } else {
      // There are some multiple values (without considering null)
      while (itKeyList.hasNext()) {
        key = itKeyList.next();
        value = inMap.get(key);

        if (value != null)
          hmTmpMap.put(key, value); // Prepare new temp HashMap with key=value combination
        else
          hmNullValueMap.put(key, value); // Keep all null values in a new temp Map
      }

      if (ascendingOrder != null && !ascendingOrder) {
        // As it is descending order, Add Null Values in first place of the
        // LinkedHasMap
        sortedMap.putAll(hmNullValueMap);
      }

      // Put all not null values in returning LinkedHashMap
      for (V o : valueList) {
        sortedValue = o;

        // Search this value in temp HashMap and if found remove it
        keySet = hmTmpMap.keySet();
        itKeyList = keySet.iterator();
        while (itKeyList.hasNext()) {
          key = itKeyList.next();
          value = hmTmpMap.get(key);
          if (value.equals(sortedValue)) {
            sortedMap.put(key, value);
            hmTmpMap.remove(key);
            break;
          }
        }
      }

    }
    if (ascendingOrder == null || ascendingOrder) {
      // Add Null Values in the last of the LinkedHasMap
      sortedMap.putAll(hmNullValueMap);
    }

    return sortedMap;
  }