All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xdean.jex.util.collection.MapUtil Maven / Gradle / Ivy

The newest version!
package xdean.jex.util.collection;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public class MapUtil {
  public static  HashMap newHashMap(K[] keys, V[] values) {
    if (keys.length > values.length) {
      throw new IllegalArgumentException("Values is less than keys");
    }
    HashMap map = new HashMap<>();
    for (int i = 0; i < keys.length; i++) {
      map.put(keys[i], values[i]);
    }
    return map;
  }

  /**
   * Get the value of the key. If absent, put the default value.
   *
   * @param map
   * @param key
   * @param defaultGetter
   * @return
   */
  @Deprecated
  public static  V getOrPutDefault(Map map, K key, Supplier defaultGetter) {
    return map.computeIfAbsent(key, k -> defaultGetter.get());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy