码迷,mamicode.com
首页 > 其他好文 > 详细

MapBuilder,操作集合工具类

时间:2018-08-27 21:47:43      阅读:557      评论:0      收藏:0      [点我收藏+]

标签:been   return   class   this   returns   final   stat   lstat   ati   


public class MapBuilder {
/**
* Creates an instance of {@code HashMap}
*/
public static <K, V> HashMap<K, V> newHashMap() {
return new HashMap<>();
}

/**
* Returns the empty map.
*/
public static <K, V> Map<K, V> of() {
return newHashMap();
}

/**
* Returns map containing a single entry.
*/
public static <K, V> Map<K, V> of(K k1, V v1) {
Map<K, V> map = of();
map.put(k1, v1);
return map;
}

/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
return map;
}

/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
return map;
}

/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
return map;
}

/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
map.put(k5, v5);
return map;
}

/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(
K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
map.put(k5, v5);
map.put(k6, v6);
return map;
}

/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(
K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
map.put(k5, v5);
map.put(k6, v6);
map.put(k7, v7);
return map;
}

/**
* Returns map containing the given entries.
*/
public static <K, V> Builder<K, V> builder() {
return new Builder<>();
}

public static final class Builder<K, V> {

private Map<K, V> map;
private boolean underConstruction;

private Builder() {
map = newHashMap();
underConstruction = true;
}

public Builder<K, V> put(K k, V v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying map has already been built");
}
map.put(k, v);
return this;
}

public Map<K, V> build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying map has already been built");
}
underConstruction = false;
return map;
}
}

}

MapBuilder,操作集合工具类

标签:been   return   class   this   returns   final   stat   lstat   ati   

原文地址:https://www.cnblogs.com/hengzhou/p/9543597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!