我正在使用java.util.Stack,但我错过了多流行音乐

stack.pop(10);

这应该会给我列出堆栈中10个项目的清单(或者堆栈中包含的堆栈不足)(并将它们从堆栈中删除)。 java中有任何标准类吗?还是我自己实施它?

最佳答案

Eclipse Collections中,有一种名为 Stack 的替代ArrayStack实现,正是这种实现提供了这种类型的行为。

MutableStack<String> stack =
    Stacks.mutable.with("j", "i", "h", "g", "f", "e", "d", "c", "b", "a");

ListIterable<String> result = stack.pop(2);
Assert.assertEquals(Lists.mutable.with("a", "b"), result);

ArrayList<String> arrayList = stack.pop(4, new ArrayList<>());
Assert.assertEquals(Arrays.asList("c", "d", "e", "f"), arrayList);

Assert.assertEquals(Stacks.mutable.withReversed("g", "h", "i", "j"), stack);

MutableBag<String> bag = stack.pop(4, Bags.mutable.empty());
Assert.assertEquals(Bags.mutable.with("g", "h", "i", "j"), bag);

Assert.assertTrue(stack.isEmpty());

还可以通过peek(int)支持多次查看。

注意:我是Eclipse Collections的提交者。

10-07 19:17
查看更多