集合处理利器 - CollectionsUtil

    主要由下面8部分组成:

    对于以下代码:

    可以重构成:

    1. if (isNullOrEmpty(lines)){
    2. return Collections.emptySet();
    3. }
    4. Set<String> set = new HashSet<>();
    5. for (ShoppingCartLineCommand line : lines){
    6. CollectionsUtil.addAllIgnoreNull(set, line.getComboIds());
    7. }
    8. return set;
    9. }

    重构之后,方法的复杂度会更小,阅读性更高

    1.2 addIgnoreNullOrEmpty(Collection<T>, T)

    对于以下代码:

    1. List<Object[]> dataList = new ArrayList<>();
    2. for (T bean : iterable){
    3. Object[] objectArray = toObjectArray(bean, propertyNameList);
    4. if (isNotNullOrEmpty(objectArray)){
    5. dataList.add(objectArray);
    6. }
    7. }
    8. return dataList;

    可以重构成:

    1. List<Object[]> dataList = new ArrayList<>();
    2. for (T bean : iterable){
    3. addIgnoreNullOrEmpty(dataList, toObjectArray(bean, propertyNameList));
    4. }
    5. return dataList;

    重构之后,方法的复杂度会更小,阅读性更高

    2.查找

    方法Description
    find(Iterable<O>, String, V)找到 iterable中,第一个 propertyName属性名称值是 propertyValue 的对应元素.
    find(Iterable<O>, Predicate<O>)迭代查找匹配predicate 的第一个元素并返回.

    2.1 find(Iterable<O>, String, V)

    比如以下代码:

    1. public Boolean hasDistributionMode(CalcFreightCommand calcFreightCommand,Long shopId){
    2. // 通过收货地址获取支持的物流方式
    3. List<DistributionMode> distributionModeList = findDistributeMode(
    4. shopId,
    5. calcFreightCommand.getProvienceId(),
    6. calcFreightCommand.getCityId(),
    7. calcFreightCommand.getCountyId(),
    8. calcFreightCommand.getTownId());
    9. Boolean flag = false;
    10. if (Validator.isNotNullOrEmpty(distributionModeList)){
    11. if (null != calcFreightCommand.getDistributionModeId()){
    12. for (DistributionMode distributionMode : distributionModeList){
    13. if (distributionMode.getId().equals(calcFreightCommand.getDistributionModeId())){
    14. flag = true;
    15. }
    16. }
    17. }else{
    18. flag = true;
    19. }
    20. }
    21. return flag;
    22. }

    22行代码,可以重构成

    1. public Boolean hasDistributionMode(CalcFreightCommand calcFreightCommand,Long shopId){
    2. // 通过收货地址获取支持的物流方式
    3. List<DistributionMode> distributionModeList = findDistributeMode(shopId, calcFreightCommand.getProvienceId(), calcFreightCommand.getCityId(), calcFreightCommand.getCountyId(), calcFreightCommand.getTownId());
    4. if (isNullOrEmpty(distributionModeList)){
    5. return false;
    6. }
    7. if (isNullOrEmpty(calcFreightCommand.getDistributionModeId())){
    8. return true;
    9. }
    10. DistributionMode distributionMode = CollectionsUtil.find(distributionModeList, "id", calcFreightCommand.getDistributionModeId());
    11. return null != distributionMode;
    12. }

    只需要14行代码,而且可阅读性更高

    2.2 find(Iterable<O>, Predicate<O>)

    场景: 从list中查找name是 关羽,并且 age等于30的User对象

    1. List<User> list = toList(//
    2. new User("张飞", 23),
    3. new User("关羽", 24),
    4. new User("刘备", 25),
    5. new User("关羽", 30));
    6. Map<String, Object> map = new HashMap<>();
    7. map.put("name", "关羽");
    8. map.put("age", 30);
    9. Predicate<User> predicate = BeanPredicateUtil.equalPredicate(map);
    10. User user = CollectionsUtil.find(list, predicate);
    11. LOGGER.debug(JsonUtil.format(user));

    返回:

    1. {
    2. "age": 30,
    3. "name": "关羽"
    4. }

    3. 定位

    方法Description
    indexOf(List<O>, String, V)在list中,查找第一个属性 propertyName 值是指定值 propertyValue 对象的索引位置.

    示例:

    1. List<User> list = new ArrayList<>();
    2. list.add(new User("张飞", 23));
    3. list.add(new User("关羽", 24));
    4. list.add(new User("刘备", 25));
    5. CollectionsUtil.indexOf(list, "name", "张飞") = 0
    6. CollectionsUtil.indexOf(null, "age", 24) = -1
    7. CollectionsUtil.indexOf(new ArrayList<User>(), "age", 24) = -1

    说明:

    • 常用于浏览历史记录,当前的商品id是否在历史记录中第一条位置,如果是,可能就不会操作Cookie,诸如此类的操作

    4.1 select(Collection<O>, String, V…)

    循环 objectCollection,获得元素 bean的 propertyName的值,判断是否在propertyValues 数组中;如果在,将该对象存入list中返回.

    注意:

    • 查询的结果的顺序按照原来 objectCollection里面的顺序,和参数 propertyValues 无关,如果你需要结果里面的元素按照指定的propertyValues 顺序排序的话,可以将结果再调用SortUtil.sortListByFixedOrderPropertyValueArray(List, String, Object)示例:
    1. List<User> list = new ArrayList<>();
    2. list.add(new User("张飞", 23));
    3. list.add(new User("关羽", 24));
    4. list.add(new User("刘备", 25));
    5. String[] array = { "刘备", "关羽" };
    6. LOGGER.info(JsonUtil.format(CollectionsUtil.select(list, "name", array)));

    返回:

    1. [{
    2. "age": 24,
    3. "name": "关羽"
    4. },{
    5. "age": 25,
    6. "name": "刘备"
    7. }]

    4.2 select(Collection<O>, String, Collection<V>)

    循环 objectCollection,获得元素 bean 的propertyName的值,判断是否在propertyValueList 集合中;如果在,将该对象存入list中返回.

    说明:

    • 查询的结果的顺序按照原来 objectCollection里面的顺序,和参数 propertyValueList 无关,如果你需要结果里面的元素按照指定的 propertyValueList顺序排序的话,可以将结果再调用SortUtil.sortListByFixedOrderPropertyValueList(List, String, List)
    • 和该方法正好相反的是 selectRejected(Collection, String, Collection)示例:场景: 查询 name属性是"张飞"或者是"刘备"的 User list
    1. List<User> list = new ArrayList<>();
    2. list.add(new User("张飞", 23));
    3. list.add(new User("关羽", 24));
    4. list.add(new User("刘备", 25));
    5. List<String> propertyValueList = new ArrayList<>();
    6. propertyValueList.add("张飞");
    7. propertyValueList.add("刘备");
    8. LOGGER.info(JsonUtil.format(CollectionsUtil.select(list, "name", propertyValueList)));

    返回:

    1. [{
    2. "age": 23,
    3. "name": "张飞"
    4. },{
    5. "age": 25,
    6. "name": "刘备"
    7. }]

    重构:

    对于以下代码:

    1. // 当前店铺 的物流方式Id set
    2. Set<Long> distributionModeIdSet = new HashSet<>();
    3. for (TemeplateDistributionMode tdCmd : temeplateDistributionModeList){
    4. distributionModeIdSet.add(tdCmd.getDistributionModeId());
    5. }
    6. // 拿到所有的物流方式 列表
    7. List<DistributionCommand> distributionCommandList = freigthMemoryManager.getDistributionList();
    8. // 根据 物流方式ID 找出 支持本商铺的 DistributionCommand
    9. List<DistributionCommand> curShopDistributionCommandList = new ArrayList<>();
    10. for (Long modeId : distributionModeIdSet){
    11. for (DistributionCommand distributionCmd : distributionCommandList){
    12. if (modeId.equals(distributionCmd.getDistributionModeId())){
    13. curShopDistributionCommandList.add(distributionCmd);
    14. }
    15. }
    16. }

    可以重构成:

    1. // 当前店铺 的物流方式Id set
    2. Set<Long> distributionModeIdSet = CollectionsUtil.getPropertyValueSet(temeplateDistributionModeList, "distributionModeId");
    3. // 拿到所有的物流方式 列表
    4. List<DistributionCommand> distributionCommandList = freigthMemoryManager.getDistributionList();
    5. // 根据 物流方式ID 找出 支持本商铺的 DistributionCommand
    6. List<DistributionCommand> curShopDistributionCommandList = CollectionsUtil.select(distributionCommandList, "distributionModeId", distributionModeIdSet);

    4.3 select(Collection<O>, Predicate<O>)

    按照指定的 Predicate,返回查询出来的集合.

    说明:

    • 和该方法正好相反的是 selectRejected(Collection, Predicate)示例1:场景: 查找等于 1的元素
    1. List<Long> list = new ArrayList<>();
    2. list.add(1L);
    3. list.add(1L);
    4. list.add(2L);
    5. list.add(3L);
    6. LOGGER.info(JsonUtil.format(CollectionsUtil.select(list, new EqualPredicate<Long>(1L))));

    返回:

    1. [1,1]

    示例2:场景: 查找大于 10的元素

    1. Predicate<Integer> predicate = new ComparatorPredicate<Integer>(10, comparator, Criterion.LESS);
    2. List<Integer> select = CollectionsUtil.select(toList(1, 5, 10, 30, 55, 88, 1, 12, 3), predicate);
    3. LOGGER.debug(JsonUtil.format(select, 0, 0));

    返回:

    1. [30,55,88,12]

    5. 反查

    方法Description
    selectRejected(Collection<O>, String, V…)循环 objectCollection,获得元素 bean 的 propertyName 属性值都不在 propertyValues 时候的list.
    selectRejected(Collection<O>, String, Collection<V>)循环 objectCollection,获得元素 bean 的 propertyName的值,判断是否不在propertyValueList 集合中;如果不在,将该对象存入list中返回.
    selectRejected(Collection<O>, Predicate<O>)循环 objectCollection,获得元素 bean,判断是否不匹配predicate,如果不匹配 ,将该对象存入list中返回.

    5.1 selectRejected(Collection<O>, String, V…)

    循环 objectCollection,获得元素 bean 的 propertyName 属性值都不在 propertyValues 时候的list.

    示例:场景: 查询name 不是刘备 也不是张飞的 User list元素

    1. List<User> list = new ArrayList<>();
    2. list.add(new User("张飞", 23));
    3. list.add(new User("关羽", 24));
    4. list.add(new User("刘备", 25));
    5. List<User> selectRejected = CollectionsUtil.selectRejected(list, "name", "刘备", "张飞");
    6. LOGGER.info(JsonUtil.format(selectRejected));

    返回:

    1. [{
    2. "age": 24,
    3. "name": "关羽"
    4. }]

    示例:场景: 查询 name属性是不是"张飞",也不是"刘备"的 User list

    返回:

    1. "age": 24,
    2. "name": "关羽"
    3. }]

    5.3 selectRejected(Collection<O>, Predicate<O>)

    循环 objectCollection,获得元素 bean,判断是否不匹配predicate,如果不匹配 ,将该对象存入list中返回.

    说明:

    • 和该方法正好相反的是 select(Collection, Predicate)示例:场景: 从list中查找不等于1的元素
    1. List<Long> list = toList(1L, 1L, 2L, 3L);
    2. CollectionsUtil.selectRejected(list, new EqualPredicate<Long>(1L));

    返回:

    1. 2L, 3L

    6. 提取

    方法Description
    getPropertyValueList(Collection<O>, String)循环集合 objectCollection,取到对象指定的属性 propertyName的值,拼成List(ArrayList).
    getPropertyValueSet(Collection<O>, String)解析迭代集合 objectCollection ,取到对象指定的属性 propertyName的值,拼成Set(LinkedHashSet).
    getPropertyValueMap(Collection<O>, String, String)循环 objectCollection ,以 keyPropertyName属性值为key, valuePropertyName属性值为value,组成map返回.

    6.1 getPropertyValueList(Collection<O>, String)

    循环集合 objectCollection,取到对象指定的属性 propertyName的值,拼成List(ArrayList).

    示例:

    场景: 获取user list每个元素的id属性值,组成新的list返回

    1. List<User> list = toList(//
    2. new User(2L),
    3. new User(5L),
    4. new User(5L));
    5. List<Long> resultList = CollectionsUtil.getPropertyValueList(list, "id");
    6. LOGGER.debug(JsonUtil.format(resultList));

    返回:

    1. [2,5,5]

    6.2 getPropertyValueSet(Collection<O>, String)

    解析迭代集合 objectCollection ,取到对象指定的属性 propertyName的值,拼成Set(LinkedHashSet).

    说明:

    返回的是 LinkedHashSet,顺序是参数 objectCollection 元素的顺序

    示例:

    1. List<User> list = new ArrayList<>();
    2. list.add(new User(2L));
    3. list.add(new User(5L));
    4. list.add(new User(5L));
    5. LOGGER.info(JsonUtil.format(CollectionsUtil.getPropertyValueSet(list, "id")));

    返回:

    1. [2,5]

    6.3 getPropertyValueMap(Collection<O>, String, String)

    循环 objectCollection ,以 keyPropertyName属性值为key, valuePropertyName属性值为value,组成map返回.

    说明:

    • 返回的是 LinkedHashMap,顺序是参数 objectCollection 元素的顺序
    • 如果有元素 keyPropertyName属性值相同,那么后面的值会覆盖前面的值示例:
    1. List<User> list = new ArrayList<>();
    2. list.add(new User("张飞", 23));
    3. list.add(new User("关羽", 24));
    4. list.add(new User("刘备", 25));
    5. LOGGER.info(JsonUtil.format(CollectionsUtil.getPropertyValueMap(list, "name", "age")));

    返回:

    1. {
    2. "张飞": 23,
    3. "关羽": 24,
    4. "刘备": 25
    5. }

    如果有元素 keyPropertyName属性值相同,那么后面的值会覆盖前面的值

    1. List<User> list = new ArrayList<>();
    2. list.add(new User("张飞", 23));
    3. list.add(new User("关羽", 24));
    4. list.add(new User("张飞", 25));
    5. LOGGER.info(JsonUtil.format(CollectionsUtil.getPropertyValueMap(list, "name", "age")));

    返回:

    1. {
    2. "张飞": 25,
    3. "关羽": 24,
    4. }

    7.1 group(Collection<O>, String)

    循环 objectCollection,以 元素的 propertyName属性值为key,相同值的元素组成list作为value,封装成map返回.

    说明:

    • 返回的LinkedHashMap,key是 objectCollection中的元素对象中 propertyName的值,value是objectCollection 中的元素对象;
    • 顺序是 objectCollection propertyName的值顺序,如果需要排序,可自行调用 SortUtil.sortMapByKeyAsc(Map), SortUtil.sortMapByKeyDesc(Map), SortUtil.sortMapByValueAsc(Map), SortUtil.sortMapByValueDesc(Map)或者, SortUtil.sortMap(Map, java.util.Comparator)
    • 属性propertyName值相同的元素,组成集合 list
    • 如果value只需要单值的话,可以调用 groupOne(Collection, String)方法示例:
    1. List<User> list = toList(
    2. new User("张飞", 23),
    3. new User("刘备", 25),
    4. new User("刘备", 30));
    5. Map<String, List<User>> map = CollectionsUtil.group(list, "name");
    6. LOGGER.debug(JsonUtil.format(map));

    返回:

    1. {
    2. "张飞": [ {
    3. "age": 23,
    4. "name": "张飞",
    5. }],
    6. "刘备": [
    7. {
    8. "age": 25,
    9. "name": "刘备",
    10. },
    11. {
    12. "age": 30,
    13. "name": "刘备",
    14. }
    15. ]
    16. }

    7.2 group(Collection<O>, String, Predicate<O>)

    循环 objectCollection,找到符合条件的 includePredicate的元素,以元素的 propertyName 属性值为key,相同值的元素组成list作为value,封装成map返回.

    说明:

    • 返回的LinkedHashMap,key是 objectCollection中的元素对象中 propertyName的值,value是objectCollection 中的元素对象;
    • 顺序是 objectCollection propertyName的值顺序,如果需要排序,可自行调用 SortUtil.sortMapByKeyAsc(Map), SortUtil.sortMapByKeyDesc(Map), SortUtil.sortMapByValueAsc(Map), SortUtil.sortMapByValueDesc(Map)或者, SortUtil.sortMap(Map, java.util.Comparator)示例:

    场景: 将age > 20的User,按照name 进行 group

    1. List<User> list = new ArrayList<>();
    2. list.add(new User("张飞", 10));
    3. list.add(new User("张飞", 28));
    4. list.add(new User("刘备", 32));
    5. list.add(new User("刘备", 30));
    6. list.add(new User("刘备", 10));
    7. Map<String, List<User>> map = CollectionsUtil.group(list, "name", new Predicate<User>(){
    8. @Override
    9. public boolean evaluate(User user){
    10. return user.getAge() > 20;
    11. }
    12. });
    13. LOGGER.info(JsonUtil.format(map));

    返回:

    1. {
    2. "张飞": [{
    3. "age": 28,
    4. "name": "张飞"
    5. }],
    6. "刘备": [{
    7. "age": 32,
    8. "name": "刘备"
    9. },{
    10. "age": 30,
    11. "name": "刘备"
    12. }
    13. ]
    14. }

    当然,对于上述代码,你还可以优化成:

    1. Predicate<User> comparatorPredicate = BeanPredicateUtil.comparatorPredicate("age", 20, Criterion.LESS);
    2. Map<String, List<User>> map = CollectionsUtil.group(list, "name", comparatorPredicate);

    7.3 group(Collection<O>, Transformer<O, T>)

    循环 objectCollection,将元素使用keyTransformer转成key,相同值的元素组成list作为value,封装成map返回.

    说明:

    • 返回的LinkedHashMap,key是 objectCollection中的元素 使用keyTransformer转换的值,value是 objectCollection中的元素对象(相同key值,组成list);
    • 返回的LinkedHashMap顺序,是 objectCollection 元素顺序,如果需要排序,可自行调用 SortUtil.sortMapByKeyAsc(Map), SortUtil.sortMapByKeyDesc(Map), SortUtil.sortMapByValueAsc(Map), SortUtil.sortMapByValueDesc(Map)或者, SortUtil.sortMap(Map, java.util.Comparator)示例:场景: 从user list中,提取user的姓名的姓为key,user组成list,返回map
    1. User mateng55 = new User("马腾", 55);
    2. User machao28 = new User("马超", 28);
    3. User madai27 = new User("马岱", 27);
    4. User maxiu25 = new User("马休", 25);
    5. User zhangfei28 = new User("张飞", 28);
    6. User liubei32 = new User("刘备", 32);
    7. User guanyu50 = new User("关羽", 50);
    8. User guanping32 = new User("关平", 32);
    9. User guansuo31 = new User("关索", 31);
    10. User guanxing20 = new User("关兴", 18);
    11. //************************************************************************
    12. List<User> list = toList(mateng55, machao28, madai27, maxiu25, zhangfei28, liubei32, guanyu50, guanping32, guansuo31, guanxing20);
    13. //************************************************************************
    14. Map<String, List<User>> map = CollectionsUtil.group(list,new Transformer<User, String>(){
    15. @Override
    16. public String transform(User user){
    17. //提取名字 的姓
    18. return user.getName().substring(0, 1);
    19. }
    20. });
    21. LOGGER.debug(JsonUtil.format(map));

    返回:

    1. {
    2. "马":[{
    3. "age": 55,
    4. "name": "马腾",
    5. },{
    6. "age": 28,
    7. "name": "马超",
    8. },{
    9. "age": 27,
    10. "name": "马岱",
    11. },{
    12. "age": 25,
    13. "name": "马休",
    14. }
    15. ],
    16. "张": [{
    17. "age": 28,
    18. "name": "张飞",
    19. }],
    20. "刘": [{
    21. "age": 32,
    22. "name": "刘备",
    23. }],
    24. "关": [{
    25. "age": 50,
    26. "name": "关羽",
    27. },{
    28. "age": 32,
    29. "name": "关平",
    30. },{
    31. "age": 31,
    32. "name": "关索",
    33. },{
    34. "age": 18,
    35. "name": "关兴",
    36. }
    37. ]
    38. }

    循环 objectCollection,找到符合条件的 includePredicate的元素,将元素使用keyTransformer转成key ,相同值的元素组成list作为value,封装成map返回.

    说明:

    • 返回的LinkedHashMap,key是 objectCollection中的元素 使用keyTransformer转换的值,value是 objectCollection中的元素对象(相同key值,组成list);
    1. User mateng55 = new User("马腾", 55);
    2. User machao28 = new User("马超", 28);
    3. User madai27 = new User("马岱", 27);
    4. User maxiu25 = new User("马休", 25);
    5. User zhangfei28 = new User("张飞", 28);
    6. User liubei32 = new User("刘备", 32);
    7. User guanyu50 = new User("关羽", 50);
    8. User guanping32 = new User("关平", 32);
    9. User guansuo31 = new User("关索", 31);
    10. User guanxing20 = new User("关兴", 18);
    11. //************************************************************************
    12. //************************************************************************
    13. Predicate<User> comparatorPredicate = BeanPredicateUtil.comparatorPredicate("age", 20, Criterion.LESS);
    14. Map<String, List<User>> map = CollectionsUtil.group(list, comparatorPredicate, new Transformer<User, String>(){
    15. @Override
    16. public String transform(User user){
    17. //提取名字 的姓
    18. return user.getName().substring(0, 1);
    19. }
    20. });
    21. LOGGER.debug(JsonUtil.format(map));
    1. {
    2. "马":[{
    3. "age": 55,
    4. "name": "马腾",
    5. },{
    6. "age": 28,
    7. "name": "马超",
    8. },{
    9. "age": 27,
    10. "name": "马岱",
    11. },{
    12. "age": 25,
    13. "name": "马休"
    14. }],
    15. "张": [{
    16. "age": 28,
    17. "name": "张飞"
    18. }],
    19. "刘": [{
    20. "age": 32,
    21. "name": "刘备"
    22. }],
    23. "关": [{
    24. "age": 50,
    25. "name": "关羽"
    26. },{
    27. "age": 32,
    28. "name": "关平"
    29. },{
    30. "age": 31,
    31. "name": "关索"
    32. }]
    33. }

    7.5 groupOne(Collection<O>, String)

    循环 objectCollection,以元素的 propertyName属性值为key,元素为value,封装成map返回(map只put第一个匹配的元素,后面出现相同的元素将会忽略).

    说明:

    • 返回的LinkedHashMap,key是 objectCollection中的元素对象中 propertyName的值,value是 objectCollection中的元素对象;
    • 顺序是 objectCollection propertyName的值 顺序,如果需要排序,可自行调用 SortUtil.sortMapByKeyAsc(Map), SortUtil.sortMapByKeyDesc(Map), SortUtil.sortMapByValueAsc(Map), SortUtil.sortMapByValueDesc(Map)或者, SortUtil.sortMap(Map, java.util.Comparator)
    • 间接的可以做到基于某个属性值去重的效果
    • 如果value需要是集合的话,可以调用 group(Collection, String)方法示例:

    返回:

    1. {
    2. "张飞": {
    3. "age": 23,
    4. "name": "张飞"
    5. },
    6. "刘备": {
    7. "age": 25,
    8. "name": "刘备"
    9. }
    10. }

    8.删除

    方法Description
    remove(Collection<O>, O)从 objectCollection中 删除removeElement (原集合对象不变).
    removeAll(Collection<O>, Collection<O>)从 objectCollection中删除所有的 removeCollection (原集合对象不变).
    removeDuplicate(Collection<O>)去重,返回没有重复元素的新list (原集合对象不变).

    8.1 remove(Collection<O>, O)

    objectCollection中 删除removeElement (原集合对象不变).

    说明:

    • 返回剩余的集合 (原集合对象不变),这个方法非常有用,如果你不想修改 collection的话,不能调用 collection.remove(removeElement);.
    • 底层实现是调用的 ListUtils.removeAll(Collection, Collection),将不是removeElement 的元素加入到新的list返回.示例:
    1. List<String> list = new ArrayList<>();
    2. list.add("xinge");
    3. list.add("feilong1");
    4. list.add("feilong2");
    5. list.add("feilong2");
    6. LOGGER.info(JsonUtil.format(CollectionsUtil.remove(list, "feilong2")));

    返回:

    1. ["xinge","feilong1"]

    此时,原来的list不变:

    1. LOGGER.info(JsonUtil.format(list));

    输出 :

    1. ["xinge","feilong1","feilong2","feilong2"]

    8.2 removeAll(Collection<O>, Collection<O>)

    objectCollection中删除所有的 removeCollection (原集合对象不变).

    说明:

    • 返回剩余的集合 (原集合对象objectCollection不变),如果你不想修改 objectCollection的话,不能直接调用 collection.removeAll(remove);,这个方法非常有用.
    • 底层实现是调用的 ListUtils.removeAll(Collection, Collection),将不是removeElement的元素加入到新的list返回.示例:场景: 从list中删除 "feilong2","feilong1"元素
    1. List<String> list = toList("xinge", "feilong1", "feilong2", "feilong2");
    2. List<String> removeList = CollectionsUtil.removeAll(list, toList("feilong2", "feilong1"));

    返回:

    1. ["xinge"]

    8.3 removeDuplicate(Collection<O>)

    去重,返回没有重复元素的新list (原集合对象不变).

    示例:

    1. List<String> list = new ArrayList<>();
    2. list.add("feilong1");
    3. list.add("feilong2");
    4. list.add("feilong2");
    5. list.add("feilong3");
    6. LOGGER.info(JsonUtil.format(CollectionsUtil.removeDuplicate(list)));

    返回:

    1. ["feilong1","feilong2","feilong3"]

    注意:

    • 如果原 objectCollection 是有序的,那么返回的结果参照原 objectCollection元素顺序
    • objectCollection不变

    9.转换

    方法Description
    collect(Iterable<O>, Transformer<? super O, ? extends T>)循环 inputIterable,将每个元素使用 transformer 转换成新的对象,返回新的list.
    collect(Iterator<O>, Transformer<? super O, ? extends T>)循环 inputIterator,将每个元素使用 transformer 转换成新的对象 返回新的list.
    collect(Iterable<I> inputBeanIterable, Class<O> outputListBeanType, String… includePropertyNames)循环 inputBeanIterable,将每个元素使用转换程成新的 outputListBeanType 类型对象(如有需要只copy传入的includePropertyNames属性) 返回新的list..

    9.1 collect(Iterable<O>, Transformer<? super O, ? extends T>)

    循环 inputIterable,将每个元素使用 transformer 转换成新的对象,返回新的list.

    示例:

    1. List<String> list = new ArrayList<>();
    2. list.add("xinge");
    3. list.add("feilong1");
    4. list.add("feilong2");
    5. list.add("feilong2");
    6. Transformer<String, Object> nullTransformer = TransformerUtils.nullTransformer();
    7. List<Object> collect = CollectionsUtil.collect(list, nullTransformer);
    8. LOGGER.info(JsonUtil.format(collect, 0, 0));

    返回:

    1. [null,null,null,null]

    更多的,使用这个方法来处理两个不同类型的转换:

    • 比如购物车功能,有游客购物车CookieShoppingCartLine以及内存购物车对象 ShoppingCartLineCommand,两个数据结构部分元素相同,
    • 用户登陆需要把cookie中的购物车转成内存购物车ShoppingCartLineCommand list,这时我们可以先创建ToShoppingCartLineCommandTransformer代码示例:
    1. class ToShoppingCartLineCommandTransformer implements Transformer<CookieShoppingCartLine, ShoppingCartLineCommand>{
    2. private static final String[] COPY_PROPERTY_NAMES = {"skuId","extentionCode","quantity","createTime","settlementState","lineGroup" };
    3. public ShoppingCartLineCommand transform(CookieShoppingCartLine cookieShoppingCartLine){
    4. // 将cookie中的购物车 转换为 shoppingCartLineCommand
    5. ShoppingCartLineCommand shoppingLineCommand = new ShoppingCartLineCommand();
    6. PropertyUtil.copyProperties(shoppingLineCommand, cookieShoppingCartLine, COPY_PROPERTY_NAMES);
    7. shoppingLineCommand.setId(cookieShoppingCartLine.getId());
    8. shoppingLineCommand.setGift(null == cookieShoppingCartLine.getIsGift() ? false : cookieShoppingCartLine.getIsGift());
    9. return shoppingLineCommand;
    10. }
    11. }

    然后调用:

    1. public List<ShoppingCartLineCommand> load(HttpServletRequest request){
    2. // 获取cookie中的购物车行集合
    3. List<CookieShoppingCartLine> cookieShoppingCartLineList = getCookieShoppingCartLines(request);
    4. if (isNullOrEmpty(cookieShoppingCartLineList)){
    5. return null;
    6. }
    7. return CollectionsUtil.collect(cookieShoppingCartLineList, new ToShoppingCartLineCommandTransformer());
    8. }

    9.2 collect(Iterator<O>, Transformer<? super O, ? extends T>)

    循环 inputIterator,将每个元素使用 transformer 转换成新的对象,返回新的list.

    示例:

    场景: 一个简单的将list中的所有元素转成null

    1. List<String> list = toList("xinge", "feilong1", "feilong2", "feilong2");
    2. Transformer<String, Object> nullTransformer = TransformerUtils.nullTransformer();
    3. List<Object> collect = CollectionsUtil.collect(list.iterator(), nullTransformer);
    4. LOGGER.info(JsonUtil.format(collect, 0, 0));

    返回:

    1. [null,null,null,null]

    9.3 collect(Iterable<I> inputBeanIterable, Class<O> outputListBeanType, String… includePropertyNames)

    循环 inputBeanIterable,将每个元素使用转换程成新的 outputListBeanType 类型对象(如有需要只copy传入的 includePropertyNames 属性) 返回新的 list.

    示例:

    已知有以下两个类 User 和 Customer

    1. public class User{
    2. // The id.
    3. private Long id = 0L;
    4. //** The name.
    5. private String name = "feilong";
    6. //** 年龄.
    7. private Integer age;
    8. //setter /getter
    9. public User(Long id, String name){
    10. this.id = id;
    11. this.name = name;
    12. }
    13. }
    14. public class Customer{
    15. //** The id.
    16. private long id;
    17. //* The name.
    18. private String name;
    19. //setter /getter
    20. }

    此时有以下的 List<User> 需要转换成 List<Customer>

    1. List<User> list = toList(//
    2. new User(23L, "张飞"),
    3. new User(24L, "关羽"),
    4. new User(25L, "刘备"));

    以前你需要如此这般写:

    1. List<Customer> customerList = new ArrayList<>();
    2. for (User user : list){
    3. Customer customer = new Customer();
    4. customer.setId(user.getId());
    5. customer.setName(user.getName());
    6. customerList.add(customer);
    7. }

    如果属性很多,书写代码很繁琐

    此时你可以这么写:

    1. List<Customer> customerList = CollectionsUtil.collect(list, Customer.class);

    一行代码搞定集合转换问题

    1. List<Customer> customerList = CollectionsUtil.collect(list, Customer.class,"id");

    说明:

    • outputListBeanType 需要有默认的构造函数