字符串集合转整型集合List => List

 /**
     * 普通循环转换
     */
    public static Consumer> forConversion() {
        return list -> {
            List integers = Lists.newArrayList();
            for (String str : list) {
                integers.add(Integer.valueOf(str));
            }
        };
    }

    /**
     * lambda mapToInt循环转换
     * @author syx
     */
    public static Consumer> lambdaConversion() {
        return list -> Lists.newArrayList(list.stream()
                .mapToInt(Integer::valueOf)
                .iterator());
    }

    /**
     * lambda map循环转换
     * @author syx
     */
    public static Consumer> lambdaConversion1() {
        return list -> list.stream().map(Integer::valueOf).collect(Collectors.toList());
    }

    /**
     * guava iterables transform循环转换
     *
     * @author syx
     */
    public static Consumer> iterablesConversion() {
        return list ->
                Lists.newArrayList(Iterables.transform(list, new Function() {
                    @NullableDecl
                    @Override
                    public Integer apply(@NullableDecl String input) {
                        return Integer.valueOf(input);
                    }
                }));
    }

    /**
     * iterators transform循环转换
     *
     * @author syx
     */
    public static Consumer> iteratorsConversion() {
        return list -> Lists.newArrayList(Iterators.transform(list.iterator(), new Function() {
            @NullableDecl
            @Override
            public Integer apply(@NullableDecl String input) {
                return Integer.valueOf(input);
            }
        }));
    }


    /**
     * transform 循环转换
     *
     * @author syx
     */
    public static Consumer> transformConversion() {
        return list -> Lists.transform(list, e -> {return Integer.valueOf(e);});
    }

    public static void test(Consumer> consumer, List list, String msg) {
        long start = System.currentTimeMillis();
        consumer.accept(list);
        long end = System.currentTimeMillis();
        System.out.println(msg + " need time:" + (end - start));
    }

    @Test
    public void listStringToIntegerTest(){
        List list = org.assertj.core.util.Lists.newArrayList();
        Integer len = 100;
        for (int i = 0; i < len; i++) {
            list.add(i + "");
        }

        test(forConversion(), list, "普通for循环 forConversion");
        test(lambdaConversion(), list, "Stream mapToInt lambdaConversion");
        test(lambdaConversion1(), list, "Stream map Conversion1");
        test(iterablesConversion(), list, "Guava iterables.transform Conversion");
        test(iteratorsConversion(), list, "Guava iterators.transform Conversion");
        test(transformConversion(), list, "Guava Lists.transform Conversion");
    }

结果如下:

普通for循环 forConversion need time:4
Stream mapToInt lambdaConversion need time:17
Stream map Conversion1 need time:5
Guava iterables.transform Conversion need time:5
Guava iterators.transform Conversion need time:0
Guava Lists.transform Conversion need time:1

 

你可能感兴趣的:(java,集合)