Eclipse Collections框架中有一些隐藏的gems。


Eclipse Collections是一个开放源代码Java Collections框架。请参阅博客末尾的资源,以获取有关该框架的更多信息。在此博客中,我将演示该框架的五个鲜为人知的功能。

  • distinct():当您想以独特List, 的方式获得独特的物品时,将它们添加List到一个物品中Set。但是,您将失去原始顺序,并最终导致哈希表的顺序无法预测。有时,您需要保留我们访问独特元素的顺序。我们distinct()针对此用例进行了开发。当您调用distinct()Eclipse CollectionsMutableList,结果是一个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收藏夹列表,则可以使用ListAdapterListIterate获取相同的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);
}
  • 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 vs. to 命名约定:在Eclipse Collections中,我们尝试遵循一些通用约定,例如本博客中描述的约定。在Eclipse Collections中,以“ as”开头的方法总是花费固定的时间并创建恒定的垃圾。通常,这意味着返回包装器对象。一些例子:

  • asUnmodifiable() –返回引发变异方法的集合包装器

  • asSynchronized()–返回在委派之前获取锁定的集合包装器
  • asLazy() –通过推迟对非终止操作的评估并仅在遇到终止操作时进行评估,返回支持延迟评估的包装器
  • asReversed() –在强制执行计算时以相反顺序迭代的列表周围返回一个惰性包装器
  • asParallel()(测试版) –返回支持并行执行的惰性包装器

在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() –是的,很重要!

概括 在这篇博客中,我解释了Eclipse的集合的几个鲜为人知的特点: distinct(), partition(),selectInstancesOf(),chunk() ,和与 方法的命名约定。as() to()

我希望您发现这篇文章有益。如果您以前没有使用过Eclipse Collections,请尝试一下。下面的资源很少。确保向我们展示您的支持并在我们的GitHub存储库上加注星号


原文链接:http://codingdict.com