-4

What is the best solution to avoid instantiations inside loops? By CAST we are checked our code and now we wont to solve the problem.

Part of code are follow

List<Long> darkList = new ArrayList<>();
for (Threshold thresholdObj : threshold) {
    DarkDTO dto = new DarkDTO();
    dto.setID(1L);
    darkList.add(dto);
}

The problem is in DarkDTO dto = new DarkDTO(); line
How can I avoid avoid instantiations inside loops?

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Hayk Harutyunyan
  • 117
  • 1
  • 3
  • 17
  • 5
    You can't here... What is the actual problem you would like to solve? Otherwise, read about the [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – OneCricketeer Feb 17 '17 at 06:30
  • Instantiation inside loop sometimes indicates bad logic but I don't think it applies to your code. There is nothing can be done to improve your code. You can also read this [question](http://softwareengineering.stackexchange.com/questions/167938/does-it-make-a-difference-if-i-declare-variables-inside-or-outside-a-loop-in-jav) as reference – Yohanes Gultom Feb 17 '17 at 06:33
  • I don't know what CAST is, but warning you to "avoid instantiations inside loops" is nonsense. Also this code doesn't compile - your `DarkDTO` can't extend `Long`, so you can't add it to a `List`. – dimo414 Feb 17 '17 at 06:33
  • 1
    You want to create threshold.size() number of DarkDTO objects, but still want to avoid creating them at all? Really? – Petter Nordlander Feb 17 '17 at 06:34
  • You can use stream & map to better readability code – Viet Feb 17 '17 at 06:37
  • 1
    @HaykHarutyunyan People are downvoting because you didn't actually ask the question correctly. As it is stated the answer can be "To avoid doing something inside the loop, remove it from the inside of the loop". Is this the answer you are waiting for? Probably not, and likely you will not accept that answer, because you think that people should read your mind and understand what you are trying to accomplish here. – v010dya Feb 17 '17 at 08:48

3 Answers3

1

You cannot avoid if you want to have a DarkDTO object for each Threshold object. If you avoid instantiating it, you end up having one dto object which is not desired obviously. Surely you are looking at wrong place to solve your actual problem.

Looks like a tool/product that you are using might reporting this issue. If I were you I just skip/stop using that software as we know you the dto object for sure.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
-3

You could use the forEach Operation in Java8. But it's absolutly the same effect and I don't know why you want to avoid object instantiation in a loop.

final List<Long> darkList = new ArrayList<>();
threshold.forEach(threshold->{
  DarkDTO dto = new DarkDTO();
  dto.setID(1L);
  darkList.add(dto);
});
Dominik Vincenz
  • 445
  • 3
  • 10
-4

Avoid initializing inside loop.Try

        DarkDTO dto = new DarkDTO();
        List<Long> darkList = new ArrayList<>();
        for (Threshold thresholdObj : threshold) {
        dto.setID(1L);
        darkList.add(dto);
        }

From Garbage Collector's point of view both the approaches (the code in the question and my code) work the same i.e. no memory is leaked.But with my approach when DarkDTO dto = new DarkDTO(); the previous DarkDTO() that was being referenced becomes an orphan and is eligible for garbage collection.

The difference is that once the loop runs out you would have the last instance of DarkDTO still reachable through the DarkDTO reference originally created outside the loop.Hence I gave that code.

Also the original question was

How can I avoid avoid instantiations inside loops? Hence it was a pretty simple answer

Akshay
  • 1,161
  • 1
  • 12
  • 33
  • 3
    The same object reference will exist N times in the list. In other words, edit one, they all change. – OneCricketeer Feb 17 '17 at 06:32
  • 2
    What makes you think this is a good idea, or solves OP's problem? – dimo414 Feb 17 '17 at 06:35
  • I'm not sure who upvoted this, but shame... – OneCricketeer Feb 17 '17 at 06:39
  • 1
    This is the correct answer to the question as it is stated. – v010dya Feb 17 '17 at 08:49
  • @cricket_007..the above code will solve OP's problem..Also in this case we need the entire code..weather the for loop is inside another loop etc.It is difficult to tell from only this loop. – Akshay Feb 17 '17 at 08:58
  • Try the following link http://stackoverflow.com/questions/407255/difference-between-declaring-variables-before-or-in-loop – Akshay Feb 17 '17 at 09:03
  • 1
    @Akshay No, that link is not the same. Primitives outside a loop are perfectly acceptable. Objects on the other hand, are not. For example, `darkList.get(0).setID(2L)` would set **all** the objects in the list. Go ahead and try it – OneCricketeer Feb 17 '17 at 16:22