java8 小计

3 篇文章 0 订阅

以逗号分割的id集合字符串转化为List

有关定义

定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定义定

使用

  • one
  • two
  • three

测试

  1. one
  2. two
  3. three

原始方法

@Test
public void idsStringToListTest2(){
    String ids = "1,2,3,4,5";
    List<Long> idList = new LinkedList<>();
    String[] idArr = StringUtils.split(ids,',');
    for (String s : idArr) {
        idList.add(Long.valueOf(s));
    }
    System.out.println(idList);
}

应用 Predicate 接口

public static List<Long> idsStringToList(String ids, Predicate<Long> predicate) {
    return idsStringToList(ids, predicate, false, ',');
}

public static List<Long> idsStringToList(String ids, Predicate<Long> predicate, boolean checkAll) {
    return idsStringToList(ids, predicate, checkAll, ',');
}

/**
 * id字符串集合转为List<Long>
 * @param ids id字符串
 * @param predicate 判断条件
 * @param checkAll 是否检查全部
 * @param separatorChar 分隔符
 * @return List<Long>
 */
public static List<Long> idsStringToList(String ids, Predicate<Long> predicate, boolean checkAll, char separatorChar) {
    List<Long> idList = new LinkedList<>();
    String[] idArr = StringUtils.split(ids, separatorChar);
    for (String s : idArr) {
        Long id = Long.valueOf(s);
        if (predicate.test(id)) {
            idList.add(id);
        } else {
            if (checkAll) {
                return null;
            }
        }
    }
    return idList;
}

@Test
public void idsStringToListTest(){
    String ids = "1,2,3,4,5";
    List<Long> list = ToolUtil.idsStringToList(ids);
    // [1, 2, 3, 4, 5]
    System.out.println(list);
    List<Long> list1 = ToolUtil.idsStringToList(ids, id -> id < 4, false);
    // [1, 2, 3]
    System.out.println(list1);
    List<Long> list2 = ToolUtil.idsStringToList(ids, id -> id < 4, true);
    // null
    System.out.println(list2);
}

定义泛型工具类



import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;


/**
 * id集合字符串转化为List
 * @author laolang
 * @param <T> 要转换的id类型
 */
public class IdstrConvert<T> {

    /**
     * id集合字符串
     */
    private String ids;

    /**
     * 单个id字符串转换方法
     */
    private Function<String, T> translation;

    public IdstrConvert(String ids, Function<String, T> translation) {
        this.ids = ids;
        this.translation = translation;
    }

    public List<T> converToList() {
        return converToList(t -> true, false, ',');
    }

    public List<T> converToList(Predicate<T> condition, boolean checkAll) {
        return converToList(condition, checkAll, ',');
    }

    /**
     * 转换为List
     * @param condition 判断条件
     * @param checkAll 是否检查全部 ,为true时有一项检查不通过,则返回null
     * @param separatorChar 分隔符
     * @return List<T>
     */
    public List<T> converToList(Predicate<T> condition, boolean checkAll, char separatorChar) {
        List<T> idList = new ArrayList<>();
        String[] idArr = StringUtils.split(ids, ',');
        for (String s : idArr) {
            T id = translation.apply(s);
            if (condition.test(id)) {
                idList.add(id);
            } else {
                if (checkAll) {
                    return null;
                }
            }
        }
        return idList;
    }
}




使用

@Test
public void idstrConvertTest(){
    String ids = "1,2,3,4,5";
    List<Long> list = new IdstrConvert<Long>(ids, new Function<String, Long>() {
        @Override
        public Long apply(String s) {
            return Long.valueOf(s);
        }
    }).converToList();
    // [1, 2, 3, 4, 5]
    System.out.println(list);
    List<Integer> list1 = new IdstrConvert<Integer>(ids, new Function<String, Integer>() {
        @Override
        public Integer apply(String s) {
            return Integer.valueOf(s);
        }
    }).converToList(new Predicate<Integer>() {
        @Override
        public boolean test(Integer id) {
            return id < 4;
        }
    },false);
    // [1, 2, 3]
    System.out.println(list1);

    List<Long> list2 = new IdstrConvert<Long>(ids, new Function<String, Long>() {
        @Override
        public Long apply(String s) {
            return Long.valueOf(s);
        }
    }).converToList(new Predicate<Long>() {
        @Override
        public boolean test(Long id) {
            return id < 4;
        }
    },true);
    // null
    System.out.println(list2);
}

真正的用法

@Test
public void idstrConvertTest(){
    String ids = "1,2,3,4,5";
    List<Long> list = new IdstrConvert<>(ids, Long::valueOf).converToList();
    // [1, 2, 3, 4, 5]
    System.out.println(list);
    List<Integer> list1 = new IdstrConvert<>(ids, Integer::valueOf).converToList(id -> id < 4,false);
    // [1, 2, 3]
    System.out.println(list1);

    List<Long> list2 = new IdstrConvert<>(ids, Long::valueOf).converToList(id -> id < 4,true);
    // null
    System.out.println(list2);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值