code: public static <K, V> Map<K, V> toSortedMap(Map<K, V> map, Comparator<? super K> comparator) {
          Map<K, V> sortedMap;
          if (comparator == null)
              sortedMap = new LinkedHashMap<>();
          else
              sortedMap = new TreeMap<>(comparator);
          sortedMap.putAll(map);
          return sortedMap;
      }
docs: Returns the the Map either ordered or as-is, if the comparator is null.
  
  @param map        the Map
  @param comparator the comparator to use.
  @return the keySet of the Map