I have a list of "Item" objects list like:
public class Item {
private String name;
private int qty;
public Item() { }
public Item(String name, int qty) {
this.name = name;
this.qty = qty;
}
public List<Item> unpack() {
List<Item> items = new ArrayList<>();
items.add(new Item("foo", 2));
items.add(new Item("bar", 3));
items.add(new Item("baz", 1));
List<Item> unpackedItems = unpack(items);
System.out.println(unpackedItems.size()); // it should be == 6
return unpackedItems;
}
private List<Item> unpack(List<Item> items) {
// ..
}
}
Is there a way to "unpack" those object using streams to have a list of items eventually repeated if qty is greater than 1 so at the end I'll have objects with qty equals to 1?