Eclipse系列的隐藏宝藏– 2019年版
Eclipse Collections是一個(gè)開放源代碼Java Collections框架。 在此博客中,我將演示該框架的五個(gè)鮮為人知的功能。 我在去年的Java Advent Calendar中發(fā)布了一個(gè)類似的博客 。 請(qǐng)參閱博客末尾的資源以獲取有關(guān)該框架的更多信息。
1. countBy() :當(dāng)您要查找特定對(duì)象的計(jì)數(shù)時(shí),可以使用countBy() API來(lái)獲取Bag。 Bag的目的是維護(hù)對(duì)象到計(jì)數(shù)的映射。 袋可用于查詢O(1)時(shí)間中的項(xiàng)目計(jì)數(shù)。 Bag還提供了其他有用的API,有助于計(jì)數(shù)。 在此博客中了解有關(guān)Bag數(shù)據(jù)結(jié)構(gòu)的更多信息。
@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() :當(dāng)您要選擇不滿足謂詞的元素時(shí),可以使用reject() API。 提供此API是為了增強(qiáng)可讀性并使開發(fā)人員直觀。 您可以使用reject()代替使用select()和取反布爾條件。 本質(zhì)上,當(dāng)使用reject()時(shí),將選擇所有對(duì)于布爾條件不返回true的元素。 reject(BooleanCondition)的輸出與通過(guò)執(zhí)行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() :當(dāng)您想要RichIterable可配置字符串表示形式時(shí),可以使用makeString() 。 如果使用不帶分隔符的makeString() ,則使用默認(rèn)的分隔符"comma space" ( ", " ) 。 如果需要特定的定界符,可以將其傳遞給makeString() ,輸出字符串將具有字符串表示形式,其中每個(gè)元素都由定界符分隔。 如果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() :要將兩個(gè)OrderedIterable縫合在一起時(shí),可以使用zip() 。 zip() API在兩個(gè)OrderedIterable上進(jìn)行操作并將它們縫合在一起,以便您獲得一Pair OrderedIterable 。 在Pair ,第一Pair是從第一元件OrderedIterable和第二Pair從第二元件OrderedIterable 。 如果OrderedIterable的大小不同, OrderedIterable忽略較長(zhǎng)OrderedIterable中多余的元素。 zip()的輸出是一個(gè)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() :當(dāng)您要根據(jù)Predicate查找兩個(gè)OrderedIterable的所有元素是否相等時(shí),可以使用corresponds() API。 的corresponds() API由第一檢查操作如果兩個(gè)OrderedIterable ■找相同的尺寸,如果它們具有那么相同的大小對(duì)應(yīng)的兩個(gè)元素OrderedIterable S使用的被評(píng)估Predicate傳遞給corresponds() 如果OrderedIterable s的大小相等,并且Predicate對(duì)所有元素返回true ,則corresponds() elements corresponds()返回true 。 如果OrderedIterable s的大小不相等,或者Predicate對(duì)任何元素返回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自己的實(shí)現(xiàn)。 它還具有其他數(shù)據(jù)結(jié)構(gòu),例如Multimap , Bag和整個(gè)Primitive Collections層次結(jié)構(gòu)。 我們的每個(gè)集合都有一個(gè)流利且豐富的API,可用于通常需要的迭代模式。
- 網(wǎng)站
- GitHub上的源代碼 ( 確保對(duì)存儲(chǔ)庫(kù) 加注 星標(biāo) )
- 貢獻(xiàn)指南
- 參考指南
- Eclipse Collections 2018版的隱藏寶藏
翻譯自: https://www.javacodegeeks.com/2019/12/hidden-treasures-of-eclipse-collections-2019-edition.html
總結(jié)
以上是生活随笔為你收集整理的Eclipse系列的隐藏宝藏– 2019年版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 匮怎么读什么意思 匮字简单介绍
- 下一篇: cassandra 备份_使用sstab