List和Map的链式添加
package com.wing.ai.cloud.system.modular.proxy; import java.util.ArrayList; import java.util.Collection; /** * @ProjectName: ai-cloud * @Package: com.wing.ai.cloud.system.modular.proxy * @ClassName: ArrayListProxy * @Author: heluwei * @Description: ArrayList代理类,用来实现链式添加 * @Date: 2020/4/14 14:01 * @Version: 1.0 */ public class ArrayListProxy<E> extends ArrayList<E> { /** * 重写ArrayList的所有构造函数---start */ public ArrayListProxy(Collection<? extends E> c) { super(c); } public ArrayListProxy(int initialCapacity) { super(initialCapacity); } public ArrayListProxy() { super(); } //重写ArrayList的所有构造函数---end /** * 对 ArrayList 的 add() 的方法进行封转返回 ArrayListProxy 来实现 链式添加 * @param e * @return */ public ArrayListProxy addObject(E e){ if(this.add(e)){ return this; } throw new ArrayStoreException("ArrayListProxy add element fail!"); } /** * 对 ArrayList 的 addAll() 的方法进行封转返回 ArrayListProxy 来实现 链式添加 */ public ArrayListProxy addAllObject(Collection<? extends E> e){ if(this.addAll(e)){ return this; } throw new ArrayStoreException("ArrayListProxy addAll element fail!"); } }
使用:
package com.wing.mall.base.test; import java.util.ArrayList; import java.util.List; /** * @ProjectName: baby * @Package: com.wing.mall.base.test * @ClassName: AddAllList * @Author: heluwei * @Description: 测试合并两个list * @Date: 2020/4/14 14:47 * @Version: 1.0 */ public class AddAllList { public static void main(String[] args) { ArrayListProxy<String> listA = new ArrayListProxy<>(); //链式添加元素 listA.addObject("A_a").addObject("A_b"); ArrayListProxy<String> listB = new ArrayListProxy<>(); listB.addObject("B_a").addObject("B_b"); ArrayListProxy<String> list = new ArrayListProxy<>(); //链式合并元素 list.addAllObject(listA).addAllObject(listB); list.forEach(s -> { System.out.println(s); }); } }
Map:
package com.wing.ai.cloud.system.modular.proxy; /** * @ProjectName: ai-cloud * @Package: com.wing.ai.cloud.system.modular.proxy * @ClassName: HashMapProxy * @Author: heluwei * @Description: HashMap 的代理类 实现链式添加元素 * @Date: 2020/4/14 14:03 * @Version: 1.0 */ import java.util.HashMap; import java.util.Map; /** *HashMap 的代理类 实现链式添加元素 * example: HashMapProxy hashMapProxy = new HashMapProxy(); * hashMapProxy.putObject("a","b").putObject("c","d"); * @param <K> * @param <V> */ public class HashMapProxy<K,V> extends HashMap<K,V> { /** * @Description 重写HashMap的所有构造函数---start * @Date 2020/4/14 14:03 */ public HashMapProxy(int initialCapacity) { super(initialCapacity); } public HashMapProxy() { super(); } public HashMapProxy(Map<? extends K, ? extends V> m) { super(m); } public HashMapProxy(int initialCapacity, float loadFactor) { super(initialCapacity,loadFactor); } //重写HashMap的所有构造函数---end /** * 对 HashMap 的 put() 的方法进行封转返回 HashMapProxy 来实现 链式添加 * @param key * @param value * @return */ public HashMapProxy putObject(K key,V value){ this.put(key, value); return this; } }
测试:
package com.wing.mall.base.test; /** * @ProjectName: baby * @Package: com.wing.mall.base.test * @ClassName: TestLianShiMap * @Author: heluwei * @Description: 链式使用Map * @Date: 2020/4/14 15:32 * @Version: 1.0 */ public class TestLianShiMap { public static void main(String[] args) { HashMapProxy<Object, Object> mapProxy = new HashMapProxy<>(); mapProxy.putObject("a", "a") .putObject("b","b"); mapProxy.forEach((k,v) ->{ System.out.println(k+":"+v); }); } }