Eclipse Collections是一个开放源代码Java Collections框架。 请参阅博客末尾的资源以获取有关该框架的更多信息。 在此博客中,我将演示该框架的五个鲜为人知的功能。
-
distinct()
:要在List
获得唯一项时,获取它们的典型方法是将List
添加到Set
。 但是,您将失去原始顺序,并最终导致哈希表的顺序无法预测。 有时,您需要保留我们访问独特元素的顺序。 我们为此用例开发了distinct()
。 当您在Eclipse CollectionsMutableList
上调用distinct()
时,结果是唯一元素List
。@Test public void distinct() { MutableList<Integer> integers = Lists.mutable.with( 1, 2, 2, 3, 3, 3, 4, 4, 4, 4); Assert.assertEquals( Lists.mutable.with(1, 2, 3, 4), integers.distinct()); }
如果你不能你原来的转换
List
到Eclipse收藏列表,你可以使用ListAdapter
或ListIterate
得到相同的API。@Test public void distinctNonEclipseCollectionsList() { List<Integer> integersLinkedList = new LinkedList<>(integers); Assert.assertEquals( Lists.mutable.with(1, 2, 3, 4), ListAdapter.adapt(integersLinkedList).distinct()); Assert.assertEquals( Lists.mutable.with(1, 2, 3, 4), ListIterate.distinct(integersLinkedList)); }
如果您需要
distinct()
来进行惰性评估,那么它也可以在我们的LazyIterable
中使用。@Test public void distinctAsLazy() { MutableList<String> distinctStrings = integers.asLazy() .distinct() .collect(String::valueOf) .toList(); Assert.assertEquals( Lists.mutable.with("1", "2", "3", "4"), distinctStrings); }
-
partition()
:当您要基于谓词在一次通过中选择和拒绝元素时,可以使用partition()
@Test public void partition() { MutableList<Integer> integers = Lists.mutable.with( 1, 2, 3, 4, 5, 6, 7, 8, 9); PartitionMutableList<Integer> evenOddPartition = integers .partition(each -> each % 2 == 0); Assert.assertEquals( Lists.mutable.with(2, 4, 6, 8), evenOddPartition.getSelected()); Assert.assertEquals( Lists.mutable.with(1, 3, 5, 7, 9), evenOddPartition.getRejected()); }
如果您不能使用Eclipse Collections接口,则可以始终使用我们的适配器包装您的集合,或者使用我们的静态实用程序
Iterate
获取相同的API。@Test public void partition() { List<Integer> integerList = new ArrayList<>(integers); PartitionMutableList<Integer> evenOddUsingAdapter = ListAdapter.adapt(integerList) .partition(each -> each % 2 == 0); Assert.assertEquals( Lists.mutable.with(2, 4, 6, 8), evenOddUsingAdapter.getSelected()); Assert.assertEquals( Lists.mutable.with(1, 3, 5, 7, 9), evenOddUsingAdapter.getRejected()); PartitionIterable<Integer> evenOddUsingIterate = Iterate.partition( integerList, each -> each % 2 == 0); Assert.assertEquals( Lists.mutable.with(2, 4, 6, 8), evenOddUsingIterate.getSelected()); Assert.assertEquals( Lists.mutable.with(1, 3, 5, 7, 9), evenOddUsingIterate.getRejected()); }
-
selectInstancesOf()
:如果要过滤并仅返回特定类的实例,则可以使用selectInstancesOf()。@Test public void selectInstancesOf() { MutableList<Number> numbers = Lists.mutable.with( 1.0, 2, 3.0, 4.0, 5); MutableList<Integer> integers = numbers .selectInstancesOf(Integer.class); Assert.assertEquals(Lists.mutable.with(2, 5), integers); }
如果您不能使用Eclipse Collections接口,则可以始终使用我们的适配器包装您的集合,或者使用我们的静态实用程序
Iterate
获取相同的API。@Test public void selectInstancesOf() { List<Number> numberList = new ArrayList<>(numbers); MutableList<Integer> integersUsingAdapter = ListAdapter .adapt(numberList) .selectInstancesOf(Integer.class); Assert.assertEquals( Lists.mutable.with(2, 5), integersUsingAdapter); Collection<Integer> integersUsingIterate = Iterate .selectInstancesOf(numberList, Integer.class); Assert.assertEquals( Lists.mutable.with(2, 5), integersUsingIterate); }
-
chunk()
:如果您想将可迭代对象分为特定大小的多个可迭代对象,则可以使用chunk()
。@Test public void chunk() { MutableList<Integer> integers = Lists.mutable.with( 1, 2, 3, 4, 5); MutableList<MutableList<Integer>> expectedChunked = Lists.mutable.with( Lists.mutable.with(1, 2), Lists.mutable.with(3, 4), Lists.mutable.with(5)); Assert.assertEquals( expectedChunked, integers.chunk(2)); }
如果您不能使用Eclipse Collections接口,则可以始终使用我们的适配器包装您的集合,或者使用我们的静态实用程序
Iterate
获取相同的API。@Test public void chunk() { List<Integer> integerList = new ArrayList<>(integers); RichIterable<RichIterable<Integer>> chunkUsingAdapter = ListAdapter .adapt(integerList) .chunk(2); Assert.assertEquals( expectedChunked, chunkUsingAdapter); RichIterable<RichIterable<Integer>> chunkUsingIterate = Iterate .chunk(integerList, 2); Assert.assertEquals( expectedChunked, chunkUsingIterate); }
-
as
VSto
命名约定:在Eclipse集合,我们尽量按照就像这个所描述的共同约定博客以单词开始。在Eclipse的集合,方法“为”始终以恒定的时间,创造常数垃圾。 通常,这意味着返回包装器对象。 一些例子:-
-
asUnmodifiable()
–返回引发变异方法的集合包装器 -
asSynchronized()
–返回在委派之前获取锁定的集合包装器 -
asLazy()
–通过推迟对非终止操作的评估并仅在遇到终止操作时进行评估,返回支持延迟评估的包装器 -
asReversed()
–返回列表的延迟包装器,在强制执行计算时,列表以相反的顺序进行迭代 -
asParallel()
(Beta) –返回支持并行执行的惰性包装器
-
在Eclipse Collections中,以“ to”开头的方法可能会花费更多时间并产生更多垃圾。 大多数情况下,这意味着要在线性时间内创建一个新集合。 在排序集合的情况下,该值增长为O(n log n)。 一些例子:
-
-
toList()
–始终创建一个新副本,即使在列表上调用时也是如此 -
toSet()
,toBag()
,toStack()
,toMap()
,toArray()
–时间和内存中的O(n) -
toSortedList()
,toSortedListBy()
,toSortedSet()
,toSortedSetBy()
,toSortedMap()
– O(n log n)时间 -
toImmutable()
–可变集合的O(n)时间 -
toReversed()
-同asReversed()
但将有急于评价 -
toString()
–是的,它计数&#55357;&#56898;
-
-
摘要:
在此博客中,我解释了Eclipse集合中鲜为人知的一些功能,这些特性包括distinct(),partition(),selectInstancesOf(),chunk()以及as ()vs to ()方法命名约定。
希望您能从中找到有用的信息。 如果您以前没有使用过Eclipse Collections,请尝试一下。 下面的资源很少。 确保向我们展示您的支持并在我们的GitHub存储库上加注星号
Eclipse Collections资源:
Eclipse Collections附带了List , Set和Map的自己的实现。 它还具有其他数据结构,例如Multimap , Bag和整个Primitive Collections层次结构。 我们的每个集合都有一个丰富的API,可用于通常需要的迭代模式。
翻译自: https://www.javacodegeeks.com/2018/12/hidden-treasures-eclipse-collections.html