0

I have following code where I want to break or continue in the for each loop:-

 List<Map<String, Object>> dataSource = new ArrayList<>();
Map<String, Object> rowMap2 = new LinkedHashMap<>();
  dataSource.stream().forEach(item -> {
        Map<String, Object> rowMap2 = new LinkedHashMap<>();
        if(item.get("Security").toString().equals("SECURITIES")){
            rowMap2 = item;
            for(Map.Entry<String, Object> entry : rowMap2.entrySet()){
                if(entry.getKey().equalsIgnoreCase("Security")){
                    entry.getKey().replace(entry.getKey(), mainType);
                }
            }
            dataSource2.add(rowMap2);
            // I want to continu here. 
        }
        if(entry.getKey().equals("Stock")){
            rowMap2.put(mainType, "SECURITIES");
            rowMap2.put(subType, entry.getValue());

            dataSource2.add(rowMap2);
            // I want to break from this loop here
        }
        dataSource2.add(rowMap2)
        }); 

Please let me know how can I do it?

masiboo
  • 4,537
  • 9
  • 75
  • 136
  • 1
    have you tried anything at all? – Eugene Aug 02 '18 at 12:54
  • 1
    You can’t. Just use an ordinary loop here. But statements like `entry.getKey().replace(entry.getKey(), mainType);` make no sense anyway. `String.replace` creates a new `String`, which is entirely ignored by your code, so it has no effect at all. – Holger Aug 02 '18 at 12:55
  • No that is my code and I would like to do it by stream().forEach API. Of course, i can iterate by old style as for(Map item : dataSource). – masiboo Aug 02 '18 at 12:56
  • @Holger This is kind of quick pseudo code, not the real code. But wil be possilbe at all in stream().forEach ? – masiboo Aug 02 '18 at 12:58
  • 1
    If all you are going to do, is `forEach`, you are not using any advantage of the Stream API. You could just use `dataSource.forEach(…)` here, but even that has no advantage over a `for` loop, given that huge loop body. Besides that, you could get the equivalent of `continue` by simply using `return` within the lambda expression, but there is no equivalent to `break` here. – Holger Aug 02 '18 at 13:00
  • 1
    Besides that, even for pseudo code, it’s awful. You check the presence of a particular mapping via `item.get("Security")` (case sensitive), followed by looping over the entire map to find the same key (but case insensitive) to perform an operation which has no effect, which seems to be a placeholder for an operation that is impossible (replacing the key of a map while iterating over it). It raises the question, what this code is supposed to demonstrate. Perhaps, that the entire task might be an xy-problem for which a simple Stream alternative could exist, if we only knew the actual purpose? – Holger Aug 02 '18 at 13:04
  • updated my pseudo code to make little more sensible. I just wanted to know is it possible to beak or continue in stream().forEach – masiboo Aug 02 '18 at 13:43
  • Possible duplicate of [Break or return from Java 8 stream forEach?](https://stackoverflow.com/questions/23308193/break-or-return-from-java-8-stream-foreach) – Druckles Aug 02 '18 at 14:50

0 Answers0