Eclipse Collections是一个开放源代码Java Collections框架。请参阅博客末尾的资源,以获取有关该框架的更多信息。在此博客中,我将演示该框架的五个鲜为人知的功能。
distinct()
List
Set
distinct()Eclipse Collections
MutableList
@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。
ListAdapter
ListIterate
@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。
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); }
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()
@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)); }
@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 vs. to 命名约定:在Eclipse Collections中,我们尝试遵循一些通用约定,例如本博客中描述的约定。在Eclipse Collections中,以“ as”开头的方法总是花费固定的时间并创建恒定的垃圾。通常,这意味着返回包装器对象。一些例子:
as
to
asUnmodifiable() –返回引发变异方法的集合包装器
asUnmodifiable()
asSynchronized()
asLazy()
asReversed()
asParallel()
在Eclipse Collections中,以“ to”开头的方法可能会花费更多的时间并产生更多的垃圾。在大多数情况下,这意味着要在线性时间内创建一个新集合。如果是已排序的集合,则变为 O(n log n)。一些例子:
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()
toString()
概括 在这篇博客中,我解释了Eclipse的集合的几个鲜为人知的特点: distinct(), partition(),selectInstancesOf(),chunk() ,和与 方法的命名约定。as() to()
distinct(), partition(),selectInstancesOf(),chunk()
我希望您发现这篇文章有益。如果您以前没有使用过Eclipse Collections,请尝试一下。下面的资源很少。确保向我们展示您的支持并在我们的GitHub存储库上加注星号
原文链接:http://codingdict.com