zoukankan      html  css  js  c++  java
  • 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);
            });
        }
    }
  • 相关阅读:
    .NET反编译工具:de4dot
    Hadoop API:遍历文件分区目录,并根据目录下的数据进行并行提交spark任务
    hive优化之——控制hive任务中的map数和reduce数
    Spark:将RDD[List[String,List[Person]]]中的List[Person]通过spark api保存为hdfs文件时一直出现not serializable task,没办法找到"spark自定义Kryo序列化输入输出API"
    spark算子:combineByKey
    spark分区数,task数目,core数,worker节点个数,excutor数量梳理
    spark算子:partitionBy对数据进行分区
    算子:sample(false, 0.1)抽样数据
    hive:默认允许动态分区个数为100,超出抛出异常:
    Oracle ADDM性能诊断利器及报告解读
  • 原文地址:https://www.cnblogs.com/bulrush/p/12698344.html
Copyright © 2011-2022 走看看