美文网首页代码记忆程序员
java集合工具 填充 提取器(减少get set调用)

java集合工具 填充 提取器(减少get set调用)

作者: hihuzi | 来源:发表于2018-10-30 15:20 被阅读1次

    集合工具提取和填充器

    1.工具说明

    • 技术 : 反射+泛型+集合+可变参数+Threadlocal
    • 优点 :

    支持所有数据类型
    支持动态缓存
    支持时间和key自定义
    支持多线程并发
    支持递归提取父类属性进行取值塞值
    支持空值(防止null报异常)

    2.使用方法

    FillFactory.beach().需要方法见下图() 微信图片_20181030144632.png

    PickFactoryTest.beach().需要方法见下图() 微信图片_20181030144632.png

    3.效率效果

    一千万数据map<String,Object>数据填充为对象(list<Object>)需要27秒(不存在栈溢出)

    4.测试类

    微信图片_20181030144632.png
    
    /**
     * tips
     *
     * @author:hihuzi 2018/7/23 9:21
     */
    public class FillFactoryTest implements Runnable {
    
        private MockHttpServletRequest request;
    
        private FillFactory fillTool;
        private static Map map;
        private static String tip;
    
        public void setMap(Map map) {
            this.map = map;
        }
    
        public void setTip(String tip) {
            this.tip = tip;
        }
    
        @Before
        public void setUp() {
    
            request = new MockHttpServletRequest();
            request.setCharacterEncoding("utf-8");
            fillTool = new FillTool();
        }
    
        /**
         * tips HttpServletRequest-->MAP
         *
         * @author:hihuzi 2018/7/23 15:05
         */
        @Test
        public void fill() {
    
            request.setParameter("integerMax", "123456");
            request.setParameter("stringMax", "你好师姐!!!");
            request.setParameter("longMax", "12.3");
            request.setParameter("intMin", "   ");
            request.setParameter("intMin", "   ");
            request.setParameter("doubleMin", "");
            /**tips 填充到request---->Map*/
            Map map = FillFactory.batch().fill(request);
            map.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
            System.out.println("");
            /**tips 舍弃掉特定字段*/
            Map map0 = FillFactory.batch().fill(request, "stringMax");
            map0.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
            System.out.println("");
            /**tips 舍弃掉空值*/
            Map map1 = FillFactory.batch().fill(request, new FillConfig(FillConfig.SaveStyleEnum.REMOVE_NULL_EMPTY));
            map1.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
            System.out.println("");
            /**tips 默认属性不舍弃空值*/
            Map map2 = FillFactory.batch().fill(request, new FillConfig(FillConfig.SaveStyleEnum.DEFAULT));
            map2.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
            System.out.println("");
            /**tips 舍弃空值 并且去掉特定字段*/
            Map map3 = FillFactory.batch().fill(request, new FillConfig(FillConfig.SaveStyleEnum.REMOVE_NULL_EMPTY), "stringMax");
            map3.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
    
        }
    
        /**
         * tips HttpServletRequest--> obj
         * tips 对于空字符串处理
         *
         * @author:hihuzi 2018/6/14 14:50
         */
        @Test
        public void fill_entity_request() throws Exception {
    
            request.setParameter("booleanMax", "");
            request.setParameter("byteMax", "");
            request.setParameter("shortMax", "");
            request.setParameter("integerMax", "");
            request.setParameter("longMax", "");
            request.setParameter("floatMax", "");
            request.setParameter("doubleMax", "");
            request.setParameter("stringMax", "           ");
            request.setParameter("bigdecimalMax", "");
            request.setParameter("dateMax", "");
            request.setParameter("booleanMin", "");
            request.setParameter("charMin", "");
            request.setParameter("byteMin", "");
            request.setParameter("shortMin", "");
            request.setParameter("intMin", "");
            request.setParameter("longMin", "");
            request.setParameter("floatMin", "");
            request.setParameter("doubleMin", "");
            long start = System.currentTimeMillis();
            TestBean map1 = null;
            for (int i = 0; i < 10000000; i++) {
                map1 = FillFactory.batch().fillEntity(request, new TestBean());
            }
            long end = System.currentTimeMillis();
            System.err.println("------>一千万 耗时" + (end - start) / 1000 + "秒<------");
            System.out.println(Arrays.asList(map1));
        }
    
        /**
         * tips HttpServletRequest--> obj
         *
         * @author:hihuzi 2018/6/14 14:50
         */
        @Test
        public void fill_entity_request1() throws Exception {
    
            request.setParameter("booleanMax", "true");
            request.setParameter("byteMax", "1");
            request.setParameter("shortMax", "129");
            request.setParameter("integerMax", "123456");
            request.setParameter("longMax", "132542435");
            request.setParameter("floatMax", "12.9");
            request.setParameter("doubleMax", "3.55");
            request.setParameter("stringMax", "你好师姐!!!");
            request.setParameter("bigdecimalMax", "9825485.61551");
            request.setParameter("dateMax", "2012-12-12");
            request.setParameter("booleanMin", "true");
            request.setParameter("charMin", "a");
            request.setParameter("byteMin", "2");
            request.setParameter("shortMin", "5");
            request.setParameter("intMin", "55");
            request.setParameter("longMin", "555");
            request.setParameter("floatMin", "0.9");
            request.setParameter("doubleMin", "1.94");
            TestBean map1 = null;
            long start = System.currentTimeMillis();
            for (int i = 0; i < 10000000; i++) {
                map1 = FillFactory.batch().fillEntity(request, new TestBean());
            }
            long end = System.currentTimeMillis();
            System.err.println("------>一千万 耗时" + (end - start) / 1000 + "秒<------");
    
            System.out.println(Arrays.asList(map1).toString());
        }
    
        /**
         * tips HttpServletRequest--> obj
         * tips 针对不同格式针对的处理
         *
         * @author:hihuzi 2018/6/14 14:50
         */
        @Test
        public void fill_entity_request2() throws Exception {
    
            request.setParameter("stringMax", "你好师姐!!!");
            request.setParameter("dateMax", "2012-12-12");
            TestBean map = null;
            map = FillFactory.batch().fillEntity(request, new TestBean(),
                    new FillConfig());
            System.out.println(Arrays.asList(map).toString());
            map = FillFactory.batch().fillEntity(request, new TestBean(),
                    new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")));
            System.out.println(Arrays.asList(map).toString());
            request.setParameter("dateMax", "2012-12-12 22:21:20");
            map = FillFactory.batch().fillEntity(request, new TestBean(),
                    new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd HH:mm:ss")));
            System.out.println(Arrays.asList(map).toString());
        }
    
        /**
         * tips Map--> obj
         * tips 针对不同格式针对的处理
         *
         * @author:hihuzi 2018/6/14 14:50
         */
        @Test
        public void fill_entity_map() throws Exception {
    
            Map map = new HashMap(20);
            map.put("booleanMax", "true");
            map.put("byteMax", "1");
            map.put("shortMax", "129");
            map.put("integerMax", "123456");
            map.put("longMax", "132542435");
            map.put("floatMax", "12.99");
            map.put("stringMax", "你好师姐!!!");
            map.put("bigdecimalMax", "9825485.6");
            map.put("dateMax", "2012-12-12");
            map.put("booleanMin", "true");
            map.put("charMin", "a");
            map.put("byteMin", "2");
            map.put("shortMin", "5");
            map.put("intMin", "55");
            map.put("longMin", "555");
            map.put("floatMin", "0.9");
            map.put("doubleMin", "1.94");
            TestBean bean = FillFactory.batch().fillEntity(map, new TestBean());
            Map map1 = new HashMap(5);
            /**tips 从对象中取出map*/
            Map map2 = FillFactory.batch().fillMap(bean, map1);
            System.out.println(bean.toString());
            map2.forEach((o, o2) -> System.out.print(o + " " + o2));
        }
    
        /**
         * tips 针对不同时间格式处理不同时间配置(错误的属性直接丢掉)
         */
        @Test
        public void fill_entity_map0() throws Exception {
    
            Map map = new HashMap(20);
            map.put("stringMax", "你好师姐!!!");
            map.put("dateMax", "2012-12-12");
            /**tips 错误的属性直接丢掉*/
            map.put("dat32eMax", "2012-12-12");
    
            TestBean bean = FillFactory.batch().fillEntity(map, new TestBean());
            System.out.println(bean.toString() + "hashCode" + bean.hashCode());
    
            map.put("dateMax", "2012-12-12 24:23:22");
            TestBean bean0 = FillFactory.batch().fillEntity(map, new TestBean(),
                    new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd HH:mm:ss")));
            System.out.println(bean0.toString() + "hashCode" + bean.hashCode());
    
    
            Map map1 = new HashMap(5);
            /**tips 从对象中取出map*/
            Map map2 = FillFactory.batch().fillMap(bean0, map1,
                    new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")));
            map2.forEach((o, o2) -> System.out.print(o + "-->" + o2));
        }
    
    
        /**
         * tips List<Map> --> E --> List<E>
         * tips 针对不同格式对应处理
         *
         * @author: hihuzi 2018/6/26 14:51
         */
        @Test
        public void fill_entity_list() throws Exception {
    
            List list = new ArrayList();
            List list0 = new ArrayList();
            Map map = new HashMap(20);
            map.put("booleanMax", "true");
            map.put("byteMax", "1");
            map.put("shortMax", "129");
            map.put("integerMax", "123456");
            map.put("longMax", "132542435");
            map.put("floatMax", "12.99");
            map.put("doubleMax", "3.55");
            map.put("stringMax", "你好师姐!!!");
            map.put("bigdecimalMax", "9825485.6");
            map.put("dateMax", "2012-12-12");
            map.put("booleanMin", "true");
            map.put("charMin", "a");
            map.put("byteMin", "2");
            map.put("shortMin", "5");
            map.put("intMin", "55");
            map.put("longMin", "555");
            map.put("floatMin", "0.9");
            map.put("doubleMin", "1.94");
            list.add(map);
            List<TestBean> bean = FillFactory.batch().fillEntity(list, new TestBean());
            /**tips 特殊的时间格式处理*/
    
            map.put("dateMax", "2012!12@12#12-12:12");
            list0.add(map);
            List<TestBean> bean0 = FillFactory.batch().fillEntity(list0, new TestBean(),
                    new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy!MM@dd#HH-mm:ss")));
            System.out.println(bean.get(0).toString());
            System.out.println(bean0.get(0).toString());
    //        long start = System.currentTimeMillis();
    //        List<TestBean> bean3;
    //        for (int i = 0; i < 1000000; i++) {
    //            bean3 = FillFactory.batch().fillEntity(list, new TestBean(),
    //                    new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy!MM@dd#HH-mm:ss")));
    //        }
    //        long end = System.currentTimeMillis();
    //        System.err.println("------>一百万 耗时" + (end - start) / 1000 + "秒<------");
        }
    
        /**
         * tips list<String> --> E --> list<E> 针对数据库与实体类名有区别
         *
         * @author:hihuzi 2018/6/26 14:51
         */
    
        @Test
        public void fill_map() throws Exception {
    
            Map map = new HashMap(20);
            map.put("stringMax", "你好师姐!!!");
            map.put("dateMax", "2012-12-12");
            map.put("booleanMin", "");
            TestBean bean = FillFactory.batch().fillEntity(map, new TestBean());
            System.out.println(bean);
            Map map1 = new HashMap(5);
            /**tips 从对象中取出map*/
            map1 = FillFactory.batch().fillMap(bean, map1);
            map1.forEach((o, o2) -> System.out.print(o + "-->" + o2 + " "));
            System.out.println("");
            System.out.println("fillMap从对象中取出不为空的属性 并且时间自定义");
            map1 = FillFactory.batch().fillMap(bean, map1,
                    new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd HH:mm:ss")));
            map1.forEach((o, o2) -> System.out.print(o + "-->" + o2 + " "));
        }
    
        /**
         * tips E --> Map  针对E与map进行填充
         *
         * @parameter: E e
         * @parameter: Map map
         * @parameter: E
         * @Author: hihuzi 2018/6/26 14:51
         */
    
        @Test
        public void list_to_entity() throws Exception {
    
            List list = new ArrayList();
            list.add("true");
            list.add("1");
            list.add("129");
            list.add("123456");
            list.add("132542435");
            list.add("12.99");
            list.add("3.55");
            list.add("你好师姐!!!");
            list.add("9825485.6");
            list.add("2012-12-12");
            list.add("true");
            list.add("a");
            list.add("2");
            list.add("5");
            list.add("55");
            list.add("555");
            list.add("0.9");
            list.add("1.94");
            List<TestBean> bean = FillFactory.batch().listToEntity(list, new TestBean());
            System.out.println(bean.get(0).toString());
    //        long start = System.currentTimeMillis();
    //        for (int i = 0; i < 10000000; i++) {
    //            List<TestBean> bean0 = FillFactory.batch().listToEntity(list, new TestBean());
    //        }
    //        long end = System.currentTimeMillis();
    //        System.err.println("------>一千万 耗时" + (end - start) / 1000 + "秒<------");
            Map<String, Map<String, TypeCache>> classCache = ClassCache.cache;
            classCache.forEach((s, typeCache) -> System.err.println(typeCache.size()));
        }
    
        @Override
        public void run() {
    
            TestBean bean = null;
            try {
                bean = FillFactory.batch().fillEntity(map, new TestBean(),
                        new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle(this.tip)));
                System.out.println(bean.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * tips 多线程测试
         *
         * @parameter:
         * @return:
         * @author: hihuzi 2018/10/21 3:51
         */
        @Test
        public void mains() {
            Map map = new HashMap(1);
            map.put("dateMax", "333-33-33");
            FillFactoryTest test0 = new FillFactoryTest();
            test0.setMap(map);
            test0.setTip("yyyy-MM-dd");
            Map maps = new HashMap(1);
            maps.put("dateMax", "222!22@22#22-22:22");
            FillFactoryTest test = new FillFactoryTest();
            test0.setMap(maps);
            test0.setTip("yyyy!MM@dd#HH-mm:ss");
            List<Thread> threads = new ArrayList<>();
            for (int i = 0; i < 666; i++) {
                Thread thread;
                if (i % 2 == 0) {
                    thread = new Thread(test0, "" + i);
                } else {
                    thread = new Thread(test, "" + i);
                }
                threads.add(thread);
            }
            for (Thread thread : threads) {
    
                thread.start();
            }
    
    
        }
    
        public static void main(String[] args) {
            Map map = new HashMap(1);
            map.put("dateMax", "333-33-33");
            FillFactoryTest test0 = new FillFactoryTest();
            test0.setMap(map);
            test0.setTip("yyyy-MM-dd");
            Map maps = new HashMap(1);
            maps.put("dateMax", "222!22@22#22-22:22");
            FillFactoryTest test = new FillFactoryTest();
            test0.setMap(maps);
            test0.setTip("yyyy!MM@dd#HH-mm:ss");
            List<Thread> threads = new ArrayList<>();
            for (int i = 0; i < 666; i++) {
                Thread thread;
                if (i % 2 == 0) {
                    thread = new Thread(test0, "" + i);
                } else {
                    thread = new Thread(test, "" + i);
                }
                threads.add(thread);
            }
            for (Thread thread : threads) {
    
                thread.start();
            }
    
        }
    }
    

    执行结果如下:

    
    TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.99, doubleMax=3.55, stringMax=你好师姐!!!, bigdecimalMax=9825485.6, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)
    TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.99, doubleMax=3.55, stringMax=你好师姐!!!, bigdecimalMax=9825485.6, dateMax=Wed Dec 12 12:12:12 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)
    TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好师姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)hashCode-286194562
    TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好师姐!!!, bigdecimalMax=null, dateMax=Thu Dec 13 00:23:22 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)hashCode-286194562
    dateMax-->2012-12-13charMin--> shortMin-->0intMin-->0doubleMin-->0.0stringMax-->你好师姐!!!byteMin-->0floatMin-->0.0booleanMin-->falselongMin-->0TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好师姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)
    dateMax-->2012-12-12 charMin-->  shortMin-->0 intMin-->0 doubleMin-->0.0 stringMax-->你好师姐!!! byteMin-->0 floatMin-->0.0 booleanMin-->false longMin-->0 
    fillMap从对象中取出不为空的属性 并且时间自定义
    dateMax-->2012-12-12 00:00:00 charMin-->  shortMin-->0 intMin-->0 doubleMin-->0.0 stringMax-->你好师姐!!! byteMin-->0 floatMin-->0.0 booleanMin-->false longMin-->0 [TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=           , bigdecimalMax=null, dateMax=null, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)]
    ------>一千万 耗时9秒<------
    ------>一千万 耗时32秒<------
    [TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.9, doubleMax=3.55, stringMax=你好师姐!!!, bigdecimalMax=9825485.61551, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)]
    [TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好师姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)]
    [TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好师姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)]
    [TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好师姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 22:21:20 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)]
    longMax=12.3 integerMax=123456 intMin=    doubleMin= stringMax=你好师姐!!! 
    longMax=12.3 integerMax=123456 intMin=    doubleMin= 
    stringMax=你好师姐!!! longMax=12.3 integerMax=123456 
    longMax=12.3 integerMax=123456 intMin=    doubleMin= stringMax=你好师姐!!! 
    longMax=12.3 integerMax=123456 TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.99, doubleMax=null, stringMax=你好师姐!!!, bigdecimalMax=9825485.6, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)
    longMax 132542435shortMin 5stringMax 你好师姐!!!byteMin 2floatMax 12.99integerMax 123456booleanMax truedateMax 2012-12-12charMin abyteMax 1intMin 55doubleMin 1.94bigdecimalMax 9825485.6floatMin 0.9booleanMin trueshortMax 129longMin 555TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.99, doubleMax=3.55, stringMax=你好师姐!!!, bigdecimalMax=9825485.6, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)
    

    pick


    /**
     * tips 测试工具
     *
     * @author: hihuzi 2018/7/20 8:42
     */
    public class PickFactoryTest implements Runnable {
    
    
        private String tip;
    
        /**
         * tips  时间格式化 多级父类
         *
         * @parameter:
         * @return:
         * @author: hihuzi 2018/10/18 11:16
         */
        @Test
        public void pick0() throws Exception {
    
    
            List<TestBean> list = new ArrayList<>();
            for (int i = 2; i < 3; i++) {
                TestBean userPost = new TestBean();
                userPost.setName("你好师姐");
                userPost.setId(12345 * i + "");
                userPost.setEmail(null);
                userPost.setAddress("    ");
                list.add(userPost);
            }
            /**tips 时间格式化 多级父类*/
            List<Map> batch7 = PickFactory.batch().pick(list, new PickConfig(PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY,
                    PickBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")), "date", "date0", "date1");
            batch7.forEach(map -> System.out.println(map));
            List<Map> batch6 = PickFactory.batch().pick(list, new PickConfig(PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY,
                    PickBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")), "date", "date0", "date1");
            batch6.forEach(map -> System.out.println(map));
            List<Map> batch5 = PickFactory.batch().pick(list, new PickConfig(PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY,
                    PickBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd HH:mm:ss")), "date", "date0", "date1");
            batch5.forEach(map -> System.out.println(map));
            List<Map> batch4 = PickFactory.batch().pick(list, new PickConfig(PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "date", "date0", "date1");
            batch4.forEach(map -> System.out.println(map));
        }
    
        @Test
        public void pick() throws Exception {
    
            List<TestBean> list = new ArrayList<>();
            for (int i = 2; i < 3; i++) {
                TestBean userPost = new TestBean();
                userPost.setName("你好师姐");
                userPost.setId(12345 * i + "");
                userPost.setEmail(null);
                userPost.setAddress("    ");
                list.add(userPost);
            }
            /**tips 默认转态*/
            List<Map> batch0 = PickFactory.batch().pick(list, "id", "name", "email", "address");
            batch0.forEach(map -> System.out.println(map));
            /**tips 和 默认一样 首字母大写*/
            List<Map> batch = PickFactory.batch().pick(list, new PickConfig(
                    PickBase.ReturnNameEnum.INITIAL_CAPITAL), "id", "name", "email", "date");
            batch.forEach(map -> System.out.println(map));
    /**         tips 空值丢掉(null 或者 "" "  ") 并且全部大写*/
    
            List<Map> batch3 = PickFactory.batch().pick(list, new PickConfig(
                    PickBase.ReturnNameEnum.UPPER_CASE,
                    PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "id", "name", "email", "date");
            batch3.forEach(map -> System.out.println(map));
            /**tips 空值不丢掉 并且全部小写*/
            List<Map> batch2 = PickFactory.batch().pick(list, new PickConfig(
                    PickBase.ReturnNameEnum.LOWER_CASE), "id", "name", "email", "date", "address");
            batch2.forEach(map -> System.out.println(map));
    
            /**tips 空值不丢掉 重新命名Key*/
            List<Map> batch4 = PickFactory.batch().pick(list, new PickConfig(
                    PickBase.ReturnNameEnum.CUSTOM_SUFFIX.setKey("我就是我!!")), "id", "name", "email", "date", "address");
            batch4.forEach(map -> System.out.println(map));
    
            /**tips 时间格式化*/
            List<Map> batch5 = PickFactory.batch().pick(list, new PickConfig(
                    PickBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")), "date", "date0");
            batch5.forEach(map -> System.out.println(map));
        }
    
        /**
         * tips 同一对象集合 返回选定字段 返回value(去重)
         *
         * @author: hihuzi 2018/4/30 15:49
         */
        @Test
        public void pickValue() throws Exception {
    
            List<TestBean> list = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                TestBean userPost = new TestBean();
                userPost.setName("你好师姐" + i);
                userPost.setId(12345 * i + "");
                userPost.setEmail(null);
                userPost.setAddress("    ");
                list.add(userPost);
            }
            /**tips 默认设置*/
            Set batch1 = PickFactory.batch().pickValue(list, "id", "name", "email");
            System.out.println(Arrays.asList(batch1).toString());
            /**tips (去掉 NUll 和 "" or "      ")*/
            Set batch = PickFactory.batch().pickValue(list, new PickConfig(
                    PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "id", "name", "email", "address");
            System.out.println(Arrays.asList(batch).toString());
    
        }
    
        /**
         * tips 单个对象 返回选定字段
         *
         * @author: hihuzi 2018/4/30 15:49
         */
        @Test
        public void pickValue0() throws Exception {
    
            TestBean bean = new TestBean();
            bean.setName("你好师姐");
            bean.setId(UUID.randomUUID().toString());
            bean.setEmail("");
            bean.setAddress(UUID.randomUUID().toString().substring(32) + "@163.com");
            /**tips 默认 保留 空值*/
            Map batch0 = PickFactory.batch().pickValue(bean, "id", "name", "email", "date", "address");
            System.out.println(batch0.toString());
            /**tips 保留 空值*/
            Map batch1 = PickFactory.batch().pickValue(bean, new PickConfig(
                    PickBase.ReturnNameEnum.DEFAULT), "id", "name", "email", "date", "address");
            System.out.println(batch1.toString());
            /**tips 舍弃 空值*/
            Map batch = PickFactory.batch().pickValue(bean, new PickConfig(
                    PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "id", "name", "email", "date", "address");
            System.out.println(batch.toString());
            Map<String, Map<String, TypeCache>> classCache = ClassCache.cache;
            classCache.forEach((s, typeCacheMap) -> System.err.println(typeCacheMap.size()));
        }
    
        /**
         * tips 单个对象 返回选定字段
         *
         * @author: hihuzi 2018/4/30 15:49
         */
        @Test
        public void pickMap() throws Exception {
    
            Map bean = new HashMap(5);
            bean.put("id", UUID.randomUUID());
            bean.put("name", "你好师姐");
            bean.put("age", "");
            bean.put("email", "54465@163.com");
            /**tips 默认 保留 空值*/
            Map batch0 = PickFactory.batch().pickMap(bean, "id", "name", "email", "age");
            System.out.println(batch0.toString());
            /**tips 保留 空值*/
            Map batch1 = PickFactory.batch().pickMap(bean, new PickConfig(
                    PickBase.ReturnNameEnum.DEFAULT), "id", "name", "email", "age");
            System.out.println(batch1.toString());
            /**tips 舍弃 空值*/
            Map batch = PickFactory.batch().pickMap(bean, new PickConfig(
                    PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "id", "name", "email", "age");
            System.out.println(batch.toString());
            Map<String, Map<String, TypeCache>> classCache = ClassCache.cache;
            classCache.forEach((s, typeCacheMap) -> System.err.println(typeCacheMap.size()));
        }
    
        @Override
        public void run() {
    
            TestBean bean = new TestBean();
            bean.setDate(new Date());
            try {
                Map map = PickFactory.batch().pickValue(bean,
                        new PickConfig(PickBase.DateStyleEnum.DEFAULT.setFormartStyle(this.tip)), "date");
                System.out.println(map.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        public void setTip(String tip) {
    
            this.tip = tip;
        }
    
    //    @Test
    //    public void mutlThreadTest() {
    //
    //        PickFactoryTest test0 = new PickFactoryTest();
    //        test0.setTip("yyyy-MM-----dd");
    //        Map maps = new HashMap(1);
    //        maps.put("dateMax", "2018!22@22#22-22:22");
    //        PickFactoryTest test = new PickFactoryTest();
    //        test.setTip("yyyy!MM@dd#HH-mm:ss");
    //        List<Thread> threads = new ArrayList<>();
    //        for (int i = 0; i < 666; i++) {
    //            Thread thread;
    //            if (i % 2 == 0) {
    //                thread = new Thread(test0, "" + i);
    //            } else {
    //                thread = new Thread(test, "" + i);
    //            }
    //            threads.add(thread);
    //        }
    //        for (Thread thread : threads) {
    //
    //            thread.start();
    //        }
    //    }
    
    //    public static void main(String[] args) {
    //
    //        PickFactoryTest test0 = new PickFactoryTest();
    //        test0.setTip("yyyy-MM-----dd");
    //        Map maps = new HashMap(1);
    //        maps.put("dateMax", "2018!22@22#22-22:22");
    //        PickFactoryTest test = new PickFactoryTest();
    //        test.setTip("yyyy!MM@dd#HH-mm:ss");
    //        List<Thread> threads = new ArrayList<>();
    //        for (int i = 0; i < 666; i++) {
    //            Thread thread;
    //            if (i % 2 == 0) {
    //                thread = new Thread(test0, "" + i);
    //            } else {
    //                thread = new Thread(test, "" + i);
    //            }
    //            threads.add(thread);
    //        }
    //        for (Thread thread : threads) {
    //
    //            thread.start();
    //        }
    //    }
    
    }
    

    测试结果如下

    {name=你好师姐, date=2018-11-06, id=cc93df6b-17fe-4338-8dd0-c1bd8bbbe4a5, address=ed15@163.com, email=}
    {name=你好师姐, date=2018-11-06, id=cc93df6b-17fe-4338-8dd0-c1bd8bbbe4a5, address=ed15@163.com, email=}
    {name=你好师姐, date=2018-11-06, id=cc93df6b-17fe-4338-8dd0-c1bd8bbbe4a5, address=ed15@163.com}
    [[0, null, 49380, 24690, 37035, 你好师姐0, 12345, 你好师姐4, 你好师姐3, 你好师姐2, 你好师姐1]]
    [[37035, 你好师姐0, 你好师姐4, 你好师姐3, 你好师姐2, 你好师姐1, 0, 49380, 24690, 12345]]
    {name=你好师姐, id=95a8d201-b307-4a7d-a093-a6759250817e, email=54465@163.com, age=}
    {name=你好师姐, id=95a8d201-b307-4a7d-a093-a6759250817e, email=54465@163.com, age=}
    {name=你好师姐, email=54465@163.com, id=95a8d201-b307-4a7d-a093-a6759250817e}
    {name=你好师姐, id=24690, address=    , email=}
    {Id=24690, Email=, Date=2018-11-06, Name=你好师姐}
    5
    {DATE=2018-11-06, ID=24690, NAME=你好师姐}
    {name=你好师姐, date=2018-11-06, id=24690, address=    , email=}
    {我就是我!!id=24690, 我就是我!!name=你好师姐, 我就是我!!date=2018-11-06, 我就是我!!email=, 我就是我!!address=    }
    {date=2018-11-06, date0=2018-11-06}
    {date=2018-11-06, date1=2018-11-06, date0=2018-11-06}
    {date=2018-11-06, date1=2018-11-06, date0=2018-11-06}
    {date=2018-11-06 22:45:57, date1=2018-11-06 22:45:57, date0=2018-11-06 22:45:57}
    {date=2018-11-06, date1=2018-11-06, date0=2018-11-06}
    

    5.readme

    也是第一次在这里发点东西,希望有梦想盆友多多指点
                                                                ---hihuzi 2018-10-30 pm
    

    4.地址

    https://github.com/hioo520/collectionTool.git
    https://gitee.com/hihuzi-top/collection_tool.git

    相关文章

      网友评论

        本文标题:java集合工具 填充 提取器(减少get set调用)

        本文链接:https://www.haomeiwen.com/subject/ubjgtqtx.html