Stream的方式将 String字符串 转成 Integer[]数组

Stream的方式将 String字符串 转成 Integer[]数组

这是同事的博客里看到的一个问题,说标题上的做法暂时实现不了,我就晚上回来做了一下,不足之处,欢迎各位大神各位留言指教!代码里还有一些问题没有想清楚,我会尽力去更新的。

涉及的技术点:

  • jdk8的一些函数式接口

  • Stream.collect()方法

  • Collector(收集器)及具体实现(包含supplier,accumulator,combiner,finisher,characteristics)

  • Stream.reduce()方法

具体代码

代码块语法遵循标准markdown代码,例如:


package com.ytt.aaron.jdk8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Stream;


/**
 *  aaron
 */
public class StringToArray {


    //串行
    public final static Integer[] stringToArrayByCollect11(String string){

        //  *************
        // 注释掉的占时未找到原因,为什么?
        // *************

//        return Stream.of(string.split(""))
//                .map(Integer::valueOf)
//                .collect(
//                        () -> new Integer[1],
//                        (array, s) -> {
//                            if(array.length == 1){
//                                array[0] = s;
//                            }else {
//                                Integer[]  accumulatorArray= new Integer[array.length + 1];
//                                System.arraycopy(array, 0, accumulatorArray, 0, array.length);
//                                accumulatorArray[accumulatorArray.length - 1] = s;
//                                array = accumulatorArray;
//                            }
//                        },
//                        (left,right) -> {
//                            Integer[]  combinerArray= new Integer[left.length + right.length];
//                            System.arraycopy(left, 0, combinerArray, 0, left.length);
//                            System.arraycopy(right, 0, combinerArray, left.length, right.length);
//                            left = combinerArray;
//                        }
//                );

        final String[] strings = string.split("");

        return Stream.of(strings)
                .map(Integer::valueOf)
                .collect(
                        () -> new Integer[strings.length],
                        (array, s) -> {
                            for (int i = 0; i < array.length; i++) {
//                                System.out.println(array[i]);
                                if(array[i] == null){
                                    array[i] = s;
//                                    System.out.println("-----------------------------");
                                    break;
                                }

                            }
                        },
                        (left,right) -> {

                        }
                );


    }

    //并行
    public final static Integer[] stringToArrayByCollect12(String string){

        final String[] strings = string.split("");

        return Stream.of(strings)
                .map(Integer::valueOf)
                .parallel()
                .collect(
                        () -> new Integer[strings.length],
                        (array, s) -> {
                            for (int i = 0; i < array.length; i++) {
//                                System.out.println(array[i]);
                                if(array[i] == null){
                                    array[i] = s;
//                                    System.out.println("-----------------------------");
                                    break;
                                }

                            }
                        },
                        (left,right) -> {
                            Set<Integer> set = new HashSet<Integer>(Arrays.asList(left));
                            set.addAll(Arrays.asList(right));
                            left = set.toArray(new Integer[string.length()]);
                        }
                );


    }

    //串行
    public final static Integer[] stringToArrayByCollect21(String string){

        final String[] strings = string.split("");

        return Stream.of(string.split(""))
                .map(Integer::valueOf)
                .collect(
                    Collector.of(
                            () -> new Integer[string.length()],
                            (array, s) -> {
                                for (int i = 0; i < array.length; i++) {
//                                System.out.println(array[i]);
                                    if(array[i] == null){
                                        array[i] = s;
//                                    System.out.println("-----------------------------");
                                        break;
                                    }

                                }
                            },
                            (left,right) -> {
                                return null;
                            },
                            Collector.Characteristics.CONCURRENT
                    )
                );

    }

    //并行
    public final static Integer[] stringToArrayByCollect22(String string){

        final String[] strings = string.split("");

        return Stream.of(string.split(""))
                .map(Integer::valueOf)
                .parallel()
                .collect(
                        Collector.of(
                                () -> new Integer[string.length()],
                                (array, s) -> {
                                    for (int i = 0; i < array.length; i++) {
//                                System.out.println(array[i]);
                                        if(array[i] == null){
                                            array[i] = s;
//                                    System.out.println("-----------------------------");
                                            break;
                                        }

                                    }
                                },
                                (left,right) -> {
                                    Set<Integer> set = new HashSet<Integer>(Arrays.asList(left));
                                    set.addAll(Arrays.asList(right));
                                    return set.toArray(new Integer[string.length()]);
                                },
                                Collector.Characteristics.CONCURRENT
                        )
                );

    }

    //并行
    // ****************(仅仅能并行,串行就挂,为什么?)
    public final static Integer[] stringToArrayByCollect23(String string){
        return Stream.of(string.split(""))
                .map(Integer::valueOf)
                .parallel()
                .collect(
                        Collector.of(
                                () -> new Integer[1],
                                (array, s) -> {
                                    if(array.length == 1){
                                        array[0] = s;
                                    }else {
                                        Integer[]  accumulatorArray= new Integer[array.length + 1];
                                        System.arraycopy(array, 0, accumulatorArray, 0, array.length);
                                        accumulatorArray[accumulatorArray.length - 1] = s;
                                        array = accumulatorArray;
                                    }
//                                System.out.println(array[0] + " > " + array.length);
                                },
                                (left,right) -> {
                                    Integer[]  combinerArray= new Integer[left.length + right.length];
                                    System.arraycopy(left, 0, combinerArray, 0, left.length);
                                    System.arraycopy(right, 0, combinerArray, left.length, right.length);
                                    return combinerArray;
                                },
                                Collector.Characteristics.CONCURRENT
                        )
                );

    }


    //******************************
    // set的方式和数组那个方式好?
    //**************************
    //或者用其他类型,有什么改进优化方式呢?
    //******************************


    //串并行都可以
    public final static Integer[] stringToArrayByReduce1(String string){

        return Stream.of(string.split(""))
//                .parallel()
                .map(s -> new Integer[]{Integer.valueOf(s)})
                .reduce(
                    new Integer[0],
                    (left,right) -> {
                        Integer[]  accumulatorArray= new Integer[left.length + right.length];
                        System.arraycopy(left, 0, accumulatorArray, 0, left.length);
                        System.arraycopy(right, 0, accumulatorArray, left.length, right.length);
                        return accumulatorArray;
                    }
                );

    }

    //串行并都可以
    public final static Integer[] stringToArrayByReduce2(String string){

        return Stream.of(string.split(""))
//                .parallel()
                .map(s -> new Integer[]{Integer.valueOf(s)})
                .reduce(
                    (left,right) -> {
                        Integer[]  accumulatorArray= new Integer[left.length + right.length];
                        System.arraycopy(left, 0, accumulatorArray, 0, left.length);
                        System.arraycopy(right, 0, accumulatorArray, left.length, right.length);
                        return accumulatorArray;
                    }
                ).orElse(null);

    }

    //并行
    // ****************(仅仅能并行,串行就挂,为什么?)
    public final static Integer[] stringToArrayByReduce3(String string){

        return Stream.of(string.split(""))
//                .parallel()
                .map(Integer::valueOf)
                .reduce(
                    new Integer[0],
                    (array, s) -> {
                        if(array.length == 1){
                            array[0] = s;
                        }else {
                            Integer[]  accumulatorArray= new Integer[array.length + 1];
                            System.arraycopy(array, 0, accumulatorArray, 0, array.length);
                            accumulatorArray[accumulatorArray.length - 1] = s;
                            array = accumulatorArray;
                        }
                        return array;
                    },
                    (left,right) -> {
                        Integer[]  combinerArray= new Integer[left.length + right.length];
                        System.arraycopy(left, 0, combinerArray, 0, left.length);
                        System.arraycopy(right, 0, combinerArray, left.length, right.length);
                        return combinerArray;
                    }
                );

    }



    public static void main(String[] args) {

        String str = "912345678";

//        Stream.of(str.split(""))
//                .collect(
//                        () -> new ArrayList(),
//                        (left,s) -> {
//                            left.add(s);
//                            System.out.println(left);
//                        },
//                        (left,right) -> {
//                            left.addAll(right);
//                            System.out.println("right> " + right);
//                            System.out.println("left> " + left);
//                        }
//                ).forEach(System.out::println);

        System.out.println("--------------stringToArrayByCollect11-------------");

        for (Integer i: stringToArrayByCollect11(str)) {
            System.out.print(i + "> ");
        }

        System.out.println();
        System.out.println("--------------stringToArrayByCollect12-------------");

        for (Integer i: stringToArrayByCollect12(str)) {
            System.out.print(i + "> ");
        }

        System.out.println();
        System.out.println("--------------stringToArrayByCollect21-------------");

        for (Integer i: stringToArrayByCollect21(str)) {
            System.out.print(i + "> ");
        }

        System.out.println();
        System.out.println("--------------stringToArrayByCollect22-------------");

        for (Integer i: stringToArrayByCollect21(str)) {
            System.out.print(i + "> ");
        }

        System.out.println();
        System.out.println("--------------stringToArrayByCollect23-------------");

        for (Integer i: stringToArrayByCollect21(str)) {
            System.out.print(i + "> ");
        }

        System.out.println();
        System.out.println("--------------stringToArrayByReduce1-------------");

        for (Integer i: stringToArrayByReduce1(str)) {
            System.out.print(i + "> ");
        }

        System.out.println();
        System.out.println("--------------stringToArrayByReduce2-------------");

        for (Integer i: stringToArrayByReduce2(str)) {
            System.out.print(i + "> ");
        }

        System.out.println();
        System.out.println("--------------stringToArrayByReduce3-------------");

        for (Integer i: stringToArrayByReduce3(str)) {
            System.out.print(i + "> ");
        }

        System.out.println();

    }


}



我要努力尽快想通。。。。。。。。。。。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值