cn.handyplus.lib.core.MapUtil Maven / Gradle / Ivy
The newest version!
package cn.handyplus.lib.core;
import java.util.HashMap;
/**
* Map工具类
*
* @author handy
* @since 3.7.2
*/
public class MapUtil {
private MapUtil() {
}
private final static Integer MIN_EXPECTED_SIZE = 3;
/**
* 预期大小的hashMap
*
* @param expectedSize 预期大小
* @param key类型
* @param 值类型
* @return HashMap
*/
public static HashMap newHashMapWithExpectedSize(int expectedSize) {
return new HashMap<>(capacity(expectedSize));
}
public static HashMap of() {
return new HashMap<>(capacity(0));
}
public static HashMap of(K k1, V v1) {
return new HashMap(capacity(1)) {{
put(k1, v1);
}};
}
public static HashMap of(K k1, V v1, K k2, V v2) {
return new HashMap(capacity(1)) {{
put(k1, v1);
put(k2, v2);
}};
}
public static HashMap of(K k1, V v1, K k2, V v2, K k3, V v3) {
return new HashMap(capacity(1)) {{
put(k1, v1);
put(k2, v2);
put(k3, v3);
}};
}
public static HashMap of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
return new HashMap(capacity(1)) {{
put(k1, v1);
put(k2, v2);
put(k3, v3);
put(k4, v4);
}};
}
public static HashMap of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
return new HashMap(capacity(1)) {{
put(k1, v1);
put(k2, v2);
put(k3, v3);
put(k4, v4);
put(k5, v5);
}};
}
/**
* 容量
*
* @param expectedSize 预期大小
* @return 容量
*/
private static int capacity(int expectedSize) {
// 负数判断
if (expectedSize < 0) {
expectedSize = 0;
}
// 小于3直接+1
if (expectedSize < MIN_EXPECTED_SIZE) {
return expectedSize + 1;
}
// 进行计算
return expectedSize < 1073741824 ? (int) ((float) expectedSize / 0.75F + 1.0F) : Integer.MAX_VALUE;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy