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年版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 佳明推出泰铁时 tactix 7 AMO
- 下一篇: 印度网友怒了!iPhone 15太贵 还