Java 8 逗号分隔字符串到对象属性

我有三个逗号分隔的列表(公共汽车、汽车、自行车的列表),我正在尝试使用 Java 8 流将它们写入 Java 对象属性。


请在下面找到我尝试过的内容:


public class Traffic {


    public int car;

    public int bus;

    public int cycle;


    public Traffic(int car, int bus,int cycle){

        this.car = car;

        this.bus = bus;

        this.cycle = cycle;

    }


}


public class Test {


    public static void main(String[] args) {


        String bus = "5,9,15,86";

        String car = "6,12,18,51";

        String cycle = "81,200,576,894";

        String[] busArray = bus.split(",");

        String[] carArray = car.split(",");

        String[] cycleArray = cycle.split(",");


        List<Traffic> trafficList =

                Arrays.stream(values)

                        .mapToInt(Integer::parseInt)

                        .mapToObj((int i,j) -> new Traffic(i,j))

                        .collect(Collectors.toList());

    }


}

我正在努力让所有流启动并注入对象属性。(在这种情况下,我想创建 4 个对象来填充所有 3 个属性。)


基本上,我正在寻找如下内容:


List<Traffic> trafficList =

                Arrays.stream(carArray,busArray,cycleArray)

                        .mapToInt(Integer::parseInt)

                        .mapToObj((int i,j,k) -> new Traffic(i,j,k))

                        .collect(Collectors.toList());


智慧大石
浏览 576回答 3
3回答

慕工程0101907

如果要创建4个 Traffic 对象,则可以使用以下命令:List<Traffic>&nbsp;collect&nbsp;=&nbsp;IntStream.range(0,&nbsp;busArray.length) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mapToObj(i&nbsp;->&nbsp;new&nbsp;Traffic(Integer.parseInt(busArray[i]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer.parseInt(carArray[i]), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer.parseInt(cycleArray[i]))) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());

慕侠2389804

您只需拆分字符串,然后将每个值映射到您的对象。在这里,我假设值可以通过Traffic对象的构造函数传递。如果没有,您可以创建它并将其值设置在 2 个单独的行中。如果期望是整数,mapToInt则 是必需的。valueString&nbsp;original&nbsp;=&nbsp;"5,9,15,86"; String[]&nbsp;values&nbsp;=&nbsp;original.split(","); List<Traffic>&nbsp;trafficList&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;Arrays.stream(values) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mapToInt(Integer::parseInt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(Traffic::new)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());

冉冉说

在类 Traffic 中定义一个构造函数,该构造函数将整数作为参数并将其分配给类中的 value 属性。&nbsp; &nbsp; static class Traffic {&nbsp; &nbsp; &nbsp; private int value;&nbsp; &nbsp; &nbsp; public Traffic(int value) {&nbsp; &nbsp; &nbsp; &nbsp; this.value = value;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }现在假设逗号分隔的字符串在字符串 commandList 中,如下所示。String commaList = "1,3,5,6,7,8,9,100";以下流指令将返回一个带有分配值的交通对象列表。&nbsp; &nbsp; List<Traffic> listOfIntegers =&nbsp;&nbsp; &nbsp; &nbsp; Arrays.asList(commaList.split(","))&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; .map(e -> new Traffic(Integer.valueOf(e)))&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java