简体   繁体   English

在具有多个对象的 ArrayList 中设置值

[英]Setting values in an ArrayList with multiple objects

Exaustive example are given on how to set a value in a 1D ArrayList, but could not find one on how to set a value of the ArrayList object after computations have been done from the dataset.给出了关于如何在 1D ArrayList 中设置值的详细示例,但找不到关于如何在从数据集完成计算后设置 ArrayList 对象的值的示例。 The image shows a null value in test and this is the value to be updated after computations.图像显示测试中的空值,这是计算后要更新的值。 Any hints?任何提示?

带对象的 ArrayList

If I understand you correctly, you have a List<> of objects on which you want to do computations.如果我理解正确的话,你有一个List<>对象,你想在这些对象上进行计算。 This is easily done with Streams.这可以通过 Streams 轻松完成。

        MyObject object1 = new MyObject(new Date(), "", "", null);
        MyObject object2 = new MyObject(new Date(), "", "", null);

        // for example purposes, I'm creating a List<> with two objects in it
        List<MyObject> myList = new ArrayList<>(); 
        myList.add(object1);
        myList.add(object2);
        
        myList = myList.stream()
                .map(myObject -> {
                    String result = "do some computations which result in a value to set";
                    myObject.setTest(result);
                    return myObject;
                })
                .collect(Collectors.toList());

// if you were to look into your objects, you'll see that the null-value is replaced
private class MyObject {

        private Date date;
        private String vwapspy;
        private String vwapgdx;
        private String test;

// constructor

// getters and setters

}

Your main goal would be to update your variable.您的主要目标是更新您的变量。 Within the .map() function of the Stream, you can easily place any logic you want.在 Stream 的.map()函数中,您可以轻松放置所需的任何逻辑。

Ofcourse, Streams are only available since Java 8. Such logic as in the example can be done with simple loops as well.当然,Streams 只从 Java 8 开始可用。示例中的逻辑也可以通过简单的循环来完成。 Look into some tutorials if I'm not clearly addressing your use-case.如果我没有清楚地解决您的用例,请查看一些教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM