通过泛型和反射构建一个简单的集合操作工具类

     

      平时在操作集合对象的时候(比如List);我想一次添加大于一个数据的时候,往往需要通过一个集合对象调用两次add方法,比如:

List<Person> personList=new ArrayList<>();
Person p1=new Person(26,"why","male");
Person p2=new Person(24,"jr","female");
personList.add(p1);
personList.add(p2);

这种写法不是太方便,我就想实现下面的添加方式:

List<Person> personList=new ArrayList<>();
Person p1=new Person(26,"why","male");
Person p2=new Person(24,"jr","female");
personList.add(p1).add(p2);

如果没有记错的话,在Android中我们构建一个AlertDialog的时候是通过AlertDialog.Builder这样一个构建器来实现的,我们可以通过链式编程的方式通过AlertDialog.Builder对象给AlertDialog设置属性等,其实就是java中构建器的使用。下面我们也来利用这种思想结合泛型和反射的内容实现一个简易的集合构建器的功能。

 package hfut.edu;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Date:2018年10月16日 下午8:30:32 Author:why
 */

        public class CollectionHelper {

            /**
             * map助手
             *
             * @author why
             *
             * @param <K>
             * @param <V>
             */
            public static class MapBuilder<K, V> {
                // private Map<K, V> defaultMap;
                private Map<K, V> mMap;

                public MapBuilder() {

                }

                public MapBuilder(Class<?> c) {

                    String name = c.getName();
                    if (name.equals(HashMap.class.getName())) {
                        this.mMap = new HashMap<>();
                    } else if (name.equals(TreeMap.class.getName())) {
                        this.mMap = new TreeMap<>();
                    } else if (name.equals(LinkedHashMap.class.getName())) {
                        this.mMap = new LinkedHashMap<>();
                    }

                    // 后续还有其他的Map子类等等
                }

                /**
                 * 添加数据
                 *
                 * @param key
                 * @param value
                 * @return
                 */
                public MapBuilder<K, V> put(K key, V value) {
                    // mMap.put(key, value);
                    this.mMap.put(key, value);
                    return this;
                }

                /**
                 * 删除数据
                 *
                 * @param key
                 * @return
                 */
                public MapBuilder<K, V> remove(K key) {
                    // mMap.remove(key);
                    this.mMap.remove(key);
                    return this;
                }

                /**
                 *
                 * @param c
                 * @return
                 */
                public MapBuilder<K, V> buildMapType(Class<?> c) {
                    // mMap.remove(key);

                    String name = c.getName();
                    if (name.equals(HashMap.class.getName())) {
                        this.mMap = new HashMap<>();
                    } else if (name.equals(TreeMap.class.getName())) {
                        this.mMap = new TreeMap<>();
                    } else if (name.equals(LinkedHashMap.class.getName())) {
                        this.mMap = new LinkedHashMap<>();
                    }

                    return this;
                }

                public Map<K, V> build() {
                    return mMap;
                }

            }



            /**
             * List助手
             *
             * @author why
             *
             * @param <T>
             */
            public static class ListBuilder<T> {

                private List<T> mList;

                public ListBuilder() {

                }

                public ListBuilder(List<T> list) {
                    this.mList = list;
                }

                /**
                 * 添加数据
                 *
                 * @param value
                 * @return
                 */
                public ListBuilder<T> add(T value) {
                    mList.add(value);
                    return this;
                }

                /**
                 * 删除数据
                 *
                 * @param index
                 * @return
                 */
                public ListBuilder<T> remove(int index) {
                    mList.remove(index);
                    return this;
                }


                /**
                 * 构建List类型
                 * @param list
                 * @return
                 */
                public ListBuilder<T> buildMapType(List<T> list) {
                    // mMap.remove(key);
                    this.mList=list;
                    return this;
                }


                /**
                 * 构建生成的List
                 * @return
                 */
                public List<T> build() {
                    return mList;
                }

            }

        }

里面的内容比较简单,这里只是列举了两类集合(List和Map),其他的可以自己加,上面我在构建集合对象的时候,第一种是使用了反射的内容(Map助手),第二种使用了传参的方式(List助手);下面我们就来看看具体的使用效果

  package hfut.edu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hfut.edu.CollectionHelper.ListBuilder;
import hfut.edu.CollectionHelper.MapBuilder;

/**
 * Date:2018年10月16日 下午8:43:24
 * Author:why
 */

        public class TestCollectionHelper {

            public static void main(String[] args) {

                /**
                 * Map助手使用
                 */
                MapBuilder<String,String> buider=new MapBuilder<>();//初始化Map的key和value数据类型
                buider=new MapBuilder<>();
                buider.buildMapType(HashMap.class)//设置集合类型
                        .put("why", "male")//添加数据
                        .put("jr", "female")//添加数据
                        .put("Tom", "unkown");//添加数据;
                Map<String, String> map=buider.build();
                System.out.println("Map助手生成Map的结果:"+map.toString());


                /**
                 * List助手使用
                 */
                List<String> list=new ArrayList<>();
                ListBuilder<String> listBuilder=new ListBuilder<>(list);
                listBuilder.add("why").add("jr").add("love").add("happy").build();
                System.out.println("List助手生成List的结果:"+list.toString());

            }
        }

测试结果:

可见,还是第二种方式使用起来会简单一些,而且对于工具类来说,第二种方式自己要做的工作也只是把传入的对象用来初始化构建器内部的集合对象即可,不需要获取其具体的子类类型;如果使用反射的话就会复杂很多,这里我只判断了Map的几种子类类型,事实上其有很多子类。

工具类本身可以有很多集合的功能可以添加进去,这里只添加了数据添加和删除的功能。

猜你喜欢

转载自blog.csdn.net/hfut_why/article/details/83246496