Eclipse 集合,在不可变列表之前添加

问题描述

如何将(理想情况下为 O(1)添加到(Eclipse 集合的)不可变列表中

解决方法

今天在 Eclipse Collections 中没有 ImmutableList 可以预先添加 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,1),list1);

// Eclipse Collections 11.x
ImmutableList<Integer> list2 = stack.toImmutableList();
Assert.assertEquals(Lists.immutable.with(3,list2);