Split a collection of objects based on a condition
问题 I have a list of objects to be added in a bag and bags capacity is 100 qty The Object and Bag look like below public class MyObject{ String id; int qty; } public class MyBag{ String id; int qty; } Is there any way to split MyObject in multiple MyBags grouping on the qty limit using Java 8 streams For example: myObjects are [myObject1:{id1, 150}, myObject2:{id2, 30}, myObject3:{id3, 150}] Since bag has a capacity of 100. Bags should be grouped as [ bag1:[{id1, 100}], bag2:[{id1, 50},{id2, 30},