Converting Nested For Loops To Streams
问题 I'm having some trouble understanding streams. I've looked around and can't seem to find an example that matches my use case. I have an existing nested for loop: List<ObjectB> objectBs = new ArrayList<ObjectB>(); for (ObjectA objA: objectAList) { for (ObjectB objB: objA.getObjectBList()) { if (objB.getNumber() != 2) { objectBs.add(objB); } } } Alot of exampls show how to add objB.getNumber() to a list but not objB . 回答1: You can use flatMap to obtain a Stream<ObjectB> of all the ObjectB