1

如何(理想情况下O(1))添加到不可变列表(Eclipse Collections)

4

1 回答 1

2

ImmutableList今天的 Eclipse Collections 中,您无法在 o(1) 行为之前添加任何内容,但您可以ImmutableStack根据您要执行的操作来使用类似的行为。

ImmutableStack<Integer> stack = Stacks.immutable.empty();
stack = stack.push(1);
stack = stack.push(2);
stack = stack.push(3);
Assert.assertEquals(Stacks.immutable.with(1, 2, 3), stack);

// Eclipse Collections 10.x
ImmutableList<Integer> list1 = stack.toList().toImmutable();
Assert.assertEquals(Lists.immutable.with(3, 2, 1), list1);

// Eclipse Collections 11.x
ImmutableList<Integer> list2 = stack.toImmutableList();
Assert.assertEquals(Lists.immutable.with(3, 2, 1), list2);
于 2021-05-14T19:23:16.083 回答