Eclipse系列的隐藏宝藏-2019年版
Eclipse Collections是一个开放源代码Java Collections框架。 在此博客中,我将演示该框架的五个鲜为人知的功能。 我在去年的Java Advent Calendar中发布了一个类似的博客 。 请参阅博客末尾的资源以获取有关该框架的更多信息。
1. countBy()
:要查找特定对象的计数时,可以使用countBy()
API来获取Bag。 Bag的目的是维护对象到计数的映射。 袋可用于查询O(1)
时间中的项目计数。 Bag还提供了其他有用的API,有助于计数。 在此博客中了解有关Bag数据结构的更多信息。
@Test public void countBy() { MutableList<String> strings = Lists.mutable.with( "A" , "B" , "C" , "A" , "B" , "A" ); Bag<String> stringToCount = strings.countBy(each -> each); assertEquals( 3 , stringToCount.occurrencesOf( "A" )); assertEquals( 2 , stringToCount.occurrencesOf( "B" )); assertEquals( 1 , stringToCount.occurrencesOf( "C" )); assertEquals( 3 , stringToCount.sizeDistinct()); assertEquals( 6 , stringToCount.size()); }
2. reject()
:当您要选择不满足谓词的元素时,可以使用reject()
API。 提供此API是为了增强可读性并使开发人员直观。 您可以使用reject()
代替使用select()
和取反布尔条件。 本质上,当使用reject()
时,将选择所有对于布尔条件不返回true
的元素。 reject(BooleanCondition)
的输出与通过执行select(!someBooleanCondition)
得到的输出相同。
@Test public void reject() { MutableList<Integer> numbers = Lists.mutable.with( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ); MutableList<Integer> odds = numbers.reject(num -> num % 2 == 0 ); // reject pattern used to find odd numbers. // Notice there is no negation in the predicate. assertEquals(Lists.mutable.with( 1 , 3 , 5 , 7 , 9 ), odds); MutableList<Integer> oddsUsingSelect = numbers.select(num -> num % 2 != 0 ); assertEquals(odds, oddsUsingSelect); }
3. makeString()
:当您想要RichIterable
可配置字符串表示形式时,可以使用makeString()
。 如果使用不带定界符的makeString()
,则使用默认的定界符"comma space" ( ", " )
。 如果需要特定的定界符,可以将其传递给makeString()
,输出字符串将具有字符串表示形式,其中每个元素都由定界符分隔。 如果Iterable
的大小为1,则不使用定界符。
@Test public void makeString() { MutableList<Integer> nums = Lists.mutable.with( 1 , 2 , 3 ); assertEquals( "[1, 2, 3]" , nums.toString()); // Notice the difference: toString() vs makeString(). // the ", " delimiter is used by default assertEquals( "1, 2, 3" , nums.makeString()); // Delimiter of choice can be passed assertEquals( "1;2;3" , nums.makeString( ";" )); MutableList<Integer> singleElement = Lists.mutable.with( 1 ); // Delimiter is not used for size = 1 assertEquals( "1" , singleElement.makeString()); assertEquals( "1" , singleElement.makeString( ";" )); }
4. zip()
:当您要将两个OrderedIterable
缝合在一起时,可以使用zip()
。 zip()
API在两个OrderedIterable
上进行操作并将它们缝合在一起,这样您就获得了Pair
of elements的OrderedIterable
。 在Pair
,第一Pair
是从第一元件OrderedIterable
和第二Pair
从第二元件OrderedIterable
。 如果OrderedIterable
的大小不同, OrderedIterable
忽略较长OrderedIterable
中多余的元素。 zip()
的输出是一个OrderedIterable
,其大小与较小的OrderedIterable
相同。
@Test public void zip() { MutableList<Integer> nums = Lists.mutable.with( 1 , 2 , 3 ); MutableList<String> strings = Lists.mutable.with( "A" , "B" , "C" ); assertEquals( Lists.mutable.with(Tuples.pair( 1 , "A" ), Tuples.pair( 2 , "B" ), Tuples.pair( 3 , "C" )), nums.zip(strings)); assertEquals( Lists.mutable.with(Tuples.pair( "A" , 1 ), Tuples.pair( "B" , 2 ), Tuples.pair( "C" , 3 )), strings.zip(nums)); MutableList<Integer> numsSmallerSize = Lists.mutable.with( 1 ); assertEquals( Lists.mutable.with(Tuples.pair( 1 , "A" )), numsSmallerSize.zip(strings)); assertEquals( Lists.mutable.with(Tuples.pair( "A" , 1 )), strings.zip(numsSmallerSize)); MutableList<String> stringsSmallerSize = Lists.mutable.with( "A" , "B" ); assertEquals( Lists.mutable.with(Tuples.pair( 1 , "A" ), Tuples.pair( 2 , "B" )), nums.zip(stringsSmallerSize)); assertEquals( Lists.mutable.with(Tuples.pair( "A" , 1 ), Tuples.pair( "B" , 2 )), stringsSmallerSize.zip(nums)); }
5. OrderedIterable
corresponds()
:当您要根据Predicate
查找两个OrderedIterable
的所有元素是否相等时,可以使用corresponds()
API。 首先,通过检查两个OrderedIterable
的大小是否相等,来检查corresponds()
API,如果它们具有相同的大小,则使用传递给corresponds()
的Predicate
来评估两个OrderedIterable
的对应元素。 如果OrderedIterable
s的大小相等,并且Predicate
对所有元素返回true
,则corresponds()
elements corresponds()
返回true
。 如果OrderedIterable
s的大小不相等,或者Predicate
对任何元素返回false
,则corresponds()
的OrderedIterable
corresponds()
返回false
。
@Test public void corresponds() { MutableList<Integer> lhs1 = Lists.mutable.with( 1 , 2 , 3 ); MutableList<Integer> rhs1 = Lists.mutable.with( 1 , 2 , 3 ); assertTrue(lhs1.corresponds(rhs1, Integer::equals)); MutableList<Integer> lhs2 = Lists.mutable.with( 1 , 2 , 3 ); MutableList<Integer> rhs2 = Lists.mutable.with( 2 , 4 , 6 ); assertTrue( lhs2.corresponds(rhs2, (lhs, rhs) -> rhs == 2 * lhs)); assertFalse( lhs2.corresponds(rhs2, (lhs, rhs) -> rhs == lhs * lhs)); assertFalse(lhs2.corresponds(rhs2, Integer::equals)); MutableList<Integer> lhs3 = Lists.mutable.with( 1 , 2 ); MutableList<Integer> rhs3 = Lists.mutable.with( 1 , 2 , 3 ); assertFalse(lhs3.corresponds(rhs3, Integer::equals)); }
Eclipse Collections资源:
Eclipse Collections附带了List , Set和Map的自己的实现。 它还具有其他数据结构,例如Multimap , Bag和整个Primitive Collections层次结构。 我们的每个集合都有一个流利且丰富的API,可用于通常需要的迭代模式。
- 网站
- GitHub上的源代码 ( 确保对存储库 加注 星标 )
- 贡献指南
- 参考指南
- Eclipse Collections 2018版的隐藏宝藏
翻译自: https://www.javacodegeeks.com/2019/12/hidden-treasures-of-eclipse-collections-2019-edition.html
Eclipse系列的隐藏宝藏-2019年版相关推荐
- Eclipse系列的隐藏宝藏– 2019年版
Eclipse Collections是一个开放源代码Java Collections框架. 在此博客中,我将演示该框架的五个鲜为人知的功能. 我在去年的Java Advent Calendar中发布 ...
- eclipse使用教程_Eclipse系列的隐藏宝藏– 2019年版
eclipse使用教程 Eclipse Collections是一个开放源代码Java Collections框架. 在此博客中,我将演示该框架的五个鲜为人知的功能. 我在去年的Java Advent ...
- Eclipse收藏品的隐藏宝藏
Eclipse Collections是一个开放源代码Java Collections框架. 请参阅博客末尾的资源以获取有关该框架的更多信息. 在此博客中,我将演示该框架的五个鲜为人知的功能. dis ...
- 计算机应用基础大纲2019,计算机应用基础考试大纲(2019 年版)电子刊物
全国计算机应用水平考试 计算机应用基础考试大纲(2019 年版) 一.考试对象 本考试针对完成 NIT 课程"计算机应用基础"学习的所有学员,以及掌握了计算机应用 相关知识和技能的 ...
- 全国计算机等级考试(2019年版python)用自己的思路编写课后习题代码(9-10章)
完结篇啦! 一个阶段的结束象征着另一个阶段的重新开始,未来还要继续加油呀. 下面贴上最后两章的编程题代码,如有错误欢迎指正,不胜感激. 第九章 python标准库概览 #1 使用turtle绘制一个蜂 ...
- 【玩转server 2019系列】Windows server 2019打开windows defender提示“需要新应用打开windows defender”的解决方法
[玩转server 2019系列]Windows server 2019打开windows defender提示"需要新应用打开windows defender"的解决方法 参考文 ...
- 计算机三级网络技术知识点大纲,全国计算机等级考试三级网络技术考试大纲(2019年版)...
全国计算机等级考试三级网络技术考试大纲(2019年版) 沿用2018年版 基本要求 1. 了解大型网络系统规划.管理方法; 2. 具备中小型网络系统规划.设计的基本能力; 3. 掌握中小型网络系统组建 ...
- 科技核心期刊目录_中医学2019年版中国科技核心期刊目录(附影响因子)
点击蓝字,关注我们 26种中医学类期刊平均影响因子为0.626,其中<中华中医药杂志><中医杂志>共两种期刊影响因子超过1. 13种中医药大学学报类期刊平均影响因子为0. ...
- 19款探岳刷隐藏教程_三星S10系列如何隐藏导航栏 官微“手把手”教你设置
三星于2月28日在乌镇正式发布国行版三星S10系列手机,无论是出色的外观设计,还是超强的性能,亦或是优秀的三摄成像,无不受到消费者的喜爱.但是,有消费者在使用三星S10系列搭载的One UI时却遇到了 ...
最新文章
- QIIME 2教程. 28社区Community(2021.2)
- 【iOS数据持久化】数据库(SQLite.swift)和FMDB
- 铁乐学python_Day40_进程池
- Java Web学习路线
- 【ZOJ - 4029】Now Loading!!!(整除分块,思维,二分,前缀和)
- day14(xml 编写及解析)
- 服务器发送消息到客户端互斥,分布式相关/Zookeeper.md · 。W/Java-info - Gitee.com
- c++ 数组初始化_C++入门篇(二十九),字符数组在内存中存储的情况
- 设置bootstrap modal模态框的宽度和宽度
- 图(用list邻接表表示)的深度优先和广度优先(递归和非递归实现)
- echarts3使用总结2
- Yii Framework2.0开发教程(4)在yii中定义全局变量
- 老罗Android视频教程第二季(全套视频+源码)
- 【产品工具使用】黑群晖史上最强安装教程
- 商务数据分析与应用学习笔记
- Linux之执行一个可执行文件
- 如何画OFDM频谱图
- 将Gif图转换成Unity Texture2D
- Redis 自动过期 使用 EXPIRE、PEXPIRE:设置生存时间
- 华为云服务器默认jdk版本,华为云服务器centos7.3 安装jdk