1 . countBy():当您要查找特定对象的计数时,可以使用countBy()API来获取Bag。袋的目的是维护对象到计数的映射。袋可以用来查询物品的数量O(1)。Bag还提供了其他有用的API,可帮助您计数。在此博客中了解有关Bag数据结构的更多信息。
countBy()
countBy()A
@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()布尔条件的和来使用。本质上,true使用时将选择所有不返回布尔条件的元素reject()。的输出与reject(BooleanCondition)通过执行操作将获得的输出相同select(!someBooleanCondition)。
reject()
reject()API
select()
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():当您想要a的可配置字符串表示形式时RichIterable,可以使用makeString()。如果makeString()不带定界符使用,则使用默认定界符"comma space" ( ", " )。如果需要特定的定界符,则可以将其传递给makeString()输出字符串,并且输出字符串将具有字符串表示形式,其中每个元素都由定界符分隔。如果大小Iterable为1,则不使用定界符。
makeString()
RichIterable
"comma space" ( ", " )
Iterable
@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(";")); }
zip()
OrderedIterable
OrderedIterableS
Pair
OrderedIterables
@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 . corresponds():如果要OrderedIterable根据a来查找两个s的所有元素是否相等Predicate,则可以使用corresponds()API。所述corresponds()API通过第一检查操作如果两个OrderedIterable■找相同的尺寸,如果它们具有那么相同的大小对应的两个要素OrderedIterable是使用进行评价的SPredicate传递给corresponds()。如果大小OrderedIterables等于和Predicate返回true所有元素,然后corresponds()返回true。如果OrderedIterables的大小不相等或任何元素的Predicate返回false值,则corresponds()返回false。
corresponds()
corresponds()A
OrderedIterable■
Predicate
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,可用于通常需要的迭代模式。
希望你喜欢!
原文链接:http://codingdict.com