eclipse 隐藏项目_Eclipse收藏品的隐藏宝藏

eclipse 隐藏项目

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

  1. distinct() :要在List获得唯一项时,获取它们的典型方法是将List添加到Set 。 但是,您将失去原始顺序,并最终导致哈希表的顺序无法预测。 有时,您需要保留访问独特元素的顺序。 我们为此用例开发了distinct() 。 当您在Eclipse Collections MutableList上调用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收藏列表,你可以使用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);
    }
  2. 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());
    }
  3. 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);
    }
  4. 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);
    }
  5. as VS to命名约定:在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附带了ListSetMap自己的实现。 它还具有其他数据结构,例如MultimapBag和整个Primitive Collections层次结构。 我们的每个集合都有一个丰富的API,可用于通常需要的迭代模式。

翻译自: https://www.javacodegeeks.com/2018/12/hidden-treasures-eclipse-collections.html

eclipse 隐藏项目

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值