I have a class with data member:
JavaScript
x
private static final int DEFAULT_SIZE = 10;
private Stack<String> myStack = new Stack<>();
// want to add more element to the stack based on demand - but add incremental demand
public void addCapacity(int incremental) {
int diff = incremental - DEFAULT_SIZE;
myStack.forEach((diff) -> {
myStack.push("something");
});
}
The idea was to see if this forms a use case for lambda functions. But it will not allow me as it is expecting a boolean
in place of diff
. Is lambda forEach
a use-case here?
Advertisement
Answer
Since Stack
extends Vector
, if you wanted to increase the capacity, then you could have used ensureCapacity
(in Vector)
JavaScript
myStack.ensureCapacity(minCapacity);
If you wanted to do myStack.push("something")
diff
times, then you could have used:
JavaScript
IntStream.range(0, diff).forEach(i -> myStack.push("something"));