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

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

    /**
     * lambda mapToInt循環轉換
     * @author syx
     */
    public static Consumer<List<String>> lambdaConversion() {
        return list -> Lists.newArrayList(list.stream()
                .mapToInt(Integer::valueOf)
                .iterator());
    }

    /**
     * lambda map循環轉換
     * @author syx
     */
    public static Consumer<List<String>> lambdaConversion1() {
        return list -> list.stream().map(Integer::valueOf).collect(Collectors.toList());
    }

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

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


    /**
     * transform 循環轉換
     *
     * @author syx
     */
    public static Consumer<List<String>> transformConversion() {
        return list -> Lists.transform(list, e -> {return Integer.valueOf(e);});
    }

    public static void test(Consumer<List<String>> consumer, List<String> 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<String> 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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章