之前已经写过一篇通用Builder来构建实例对象的(基于Java8的新特性写一个通用的Builder工具类_java手动写一个builder-CSDN博客)但是这个builder不能构建集合,也就是list和map,今天突然心血来潮,想写写集合类的builder,这两个builder相比于通用的builder要简单易懂的多,废话不多说,直接上代码:
Mapbuilder:
public class MapBuilder<K, V> {
private Map<K, V> map;
private MapBuilder(Map<K, V> map) {
this.map = map;
}
public static <K, V> MapBuilder<K, V> ofMap() {
return new MapBuilder<>(new HashMap<K, V>());
}
public MapBuilder<K, V> put(K key, V value) {
this.map.put(key, value);
return this;
}
public Map<K, V> build() {
return this.map;
}
public static void main(String[] args) {
Map<String, Integer> map = MapBuilder.<String, Integer>ofMap()
.put("a", 1)
.put("b", 2)
.put("c", 3)
.build();
System.out.println(map); // 输出:{a=1, b=2, c=3}
}
}
ListBuilder:
public class ListBuilder<T> {
private List<T> list;
private ListBuilder(List<T> list) {
this.list = list;
}
public static <T> ListBuilder<T> ofList() {
return new ListBuilder(new ArrayList<T>());
}
public ListBuilder<T> add(T t) {
this.list.add(t);
return this;
}
public List<T> build() {
return this.list;
}
public static void main(String[] args) {
List<Integer> integers = ListBuilder.<Integer>ofList().add(1).build();
System.err.println(integers);
}
}
也可以通过前文提到的通用Builder来构建list和Map:
public class Builder<T> {
private T instance;
private Builder(T instance) {
this.instance = instance;
}
public static <T> Builder<T> of(Class<T> clazz) {
T instance = null;
if (List.class.isAssignableFrom(clazz)) {
instance = (T) new ArrayList<>();
} else if (Map.class.isAssignableFrom(clazz)) {
instance = (T) new HashMap<>();
} else {
try {
instance = (T)clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return new Builder<>(instance);
}
public static <T> Builder<T> of(T target) {
return new Builder<T>(target);
}
public <U> Builder<T> with(BiConsumer<T, U> setter, U value) {
setter.accept(instance, value);
return this;
}
public T build() {
return instance;
}
public static void main(String[] args) {
List<String> list = Builder.of(List.class)
.with(List::add, "item1")
.with(List::add, "item2")
.build();
Map<String, String> map = Builder.of(Map.class)
.with((m, v) -> m.put("key1", "value1"), null)
.with((m, v) -> m.put("key2", "value2"), null)
.build();
System.out.println(list);
System.out.println(map);
}
}
基于这个Builder,可以改写上面的ListBuilder和MapBuilder(比前面的会稍微晦涩难懂麻烦一点):
public class ListBuilder<T> {
private Builder<List<T>> listBuilder;
private ListBuilder(Builder<List<T>> listBuilder) {
this.listBuilder = listBuilder;
}
public static <T> ListBuilder<T> ofList() {
Builder<List<T>> listBuilder = Builder.of(new ArrayList<T>());
return new ListBuilder<>(listBuilder);
}
public ListBuilder<T> add(T t) {
this.listBuilder.with(List::add, t);
return this;
}
public List<T> build() {
return this.listBuilder.build();
}
public static void main(String[] args) {
ListBuilder<Integer> integerListBuilder = ListBuilder.<Integer>ofList();
for (int i = 0; i < 10; i++) {
integerListBuilder.add(i);
}
System.err.println(integerListBuilder.build());
}
}
public class MapBuilder<K, V> {
private Builder<Map<K, V>> mapBuilder;
private MapBuilder(Builder<Map<K, V>> mapBuilder) {
this.mapBuilder = mapBuilder;
}
public static <K, V> MapBuilder<K, V> ofMap() {
Builder<Map<K, V>> builder = Builder.of(new HashMap<K, V>());
return new MapBuilder<K, V>(builder);
}
public MapBuilder<K, V> put(K key, V value) {
this.mapBuilder.with((m, val) -> m.put(key, value), null);
return this;
}
public Map<K, V> build() {
return this.mapBuilder.build();
}
public static void main(String[] args) {
Map<String, Integer> map = MapBuilder.<String, Integer>ofMap()
.put("a", 1)
.put("b", 2)
.put("c", 3)
.build();
System.out.println(map); // 输出:{a=1, b=2, c=3}
}
}