资讯专栏INFORMATION COLUMN

eclipse collections入门

LeexMuller / 1933人阅读

摘要:配合一下方法使用类似的方法,用于提取一个里头的一个属性出来。当然,也可以简写为主要用来过滤集合。配合使用功能与相同,只是把该筛选条件内置在中然后这里直接使用,传入参数与功能相反集合分区只取出一个符合条件的计数与条件满足判断其他转参考

Function

配合一下方法使用:

collect

flatCollect

groupBy

minBy

maxBy

toSortedListBy

sortThisBy

toMap

collect

类似guava的transform方法,用于提取一个object里头的一个属性出来。

Function nameFunction = Customer::getName;
MutableList customerNames = customers.collect(nameFunction);

当然,也可以简写为

MutableList customerNames = customers.collect(Customer::getName);
flatCollect
MutableList allOrderedLineItems = company.getOrders().flatCollect(Order::getLineItems);

MutableSet actualItemNames = allOrderedLineItems.collect(LineItem::getName).toSet();
Predicate

主要用来过滤集合。配合使用:

select

reject

detect

count

anySatisfy

allSatisfy

select
MutableList customersFromLondon = customers.select(c -> "London".equals(c.getCity()));
selectWith

功能与select相同,只是把该筛选条件内置在domain中
Customer:

public boolean livesIn(String city){
        return this.city.equals(city);
}

然后这里直接使用,传入参数:

MutableList customersFromLondon = customers.selectWith(Customer::livesIn,"London");
rejectWith

与selectWith功能相反

MutableList customersNotFromLondon = company.getCustomers().rejectWith(Customer::livesIn, "London");
partitionWith(集合分区)
PartitionMutableList partitionedList = this.company.getCustomers().partitionWith(Customer::livesIn, "London");
        MutableList customersFromLondon = partitionedList.getSelected();
        MutableList customersNotFromLondon = partitionedList.getRejected();
detect(只取出一个符合条件的)
public Customer getCustomerNamed(String name) {
        return customers.detect(c -> name.equals(c.getName()));
    }
countWith(计数)
int numberOfCustomerFromLondon = company.getCustomers().countWith(Customer::livesIn,"London");
sort
MutableList sortedTotalValues = company.getCustomers().collect(Customer::getTotalOrderValue).sortThis();
Assert.assertEquals("Highest total order value", Double.valueOf(857.0), sortedTotalValues.getLast());
Assert.assertEquals("Lowest total order value", Double.valueOf(71.0), sortedTotalValues.getFirst());
max与maxBy
Double maximumTotalOrderValue = company.getCustomers().collect(Customer::getTotalOrderValue).max();
Customer customerWithMaxTotalOrderValue = company.getCustomers().maxBy(Customer::getTotalOrderValue);
any/allSatisfy(条件满足判断)
Predicate CUSTOMER_FROM_LONDON = customer -> customer.getCity().equals("London");
boolean anyCustomersFromLondon = company.getCustomers().anySatisfy(CUSTOMER_FROM_LONDON);
boolean allCustomersFromLondon = company.getCustomers().allSatisfy(CUSTOMER_FROM_LONDON);
其他 MutableBag
MutableBag bag    = HashBag.newBagWith("one","two","two","three","three","three");    
Assert.assertEquals(3,bag.occurrencesOf("three"));    
bag.add("one");    
Assert.assertEquals(2,bag.occurrencesOf("one"));
MutableMap
MutableMap petTypeCounts = UnifiedMap.newMap();
MutableSet

MutableList转MutableSet

MutableList allOrderedLineItems = company.getOrders().flatCollect(Order::getLineItems);
MutableSet actualItemNames = allOrderedLineItems.collect(LineItem::getName).toSet();

UnifiedSet.newSetWith

MutableSet people = UnifiedSet.newSetWith(mrSmith,mrsSmith,mrJones);    
int numAddresses = people.collect(addressFunction).size();    
System.out.println(numAddresses);
MutableMultiMap
MutableListMultimap multimap = company.getCustomers().groupBy(Customer::getCity);
Assert.assertEquals(FastList.newListWith(this.company.getCustomerNamed("Mary")),multimap.get("Liphook"));
MutableList Liphooks = multimap.get("Liphook");
ArrayIterate
public boolean hasSupply(String itemName){
    return ArrayIterate.contains(itemNames,itemName);
}
ListIterate
List orders = this.company.getMostRecentCustomer().getOrders();
MutableList orderValues = ListIterate.collect(orders, Order::getValue);
makeString
String tildeSeparatedNames = company.getSuppliers().collect(Supplier::getName).makeString("~");
参考

eclipse-collections-kata

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65832.html

相关文章

  • 入门教程 | 5分钟从零构建第一个 Flink 应用

    摘要:接着我们将数据流按照单词字段即号索引字段做分组,这里可以简单地使用方法,得到一个以单词为的数据流。得到的结果数据流,将每秒输出一次这秒内每个单词出现的次数。最后一件事就是将数据流打印到控制台,并开始执行最后的调用是启动实际作业所必需的。 本文转载自 Jark’s Blog ,作者伍翀(云邪),Apache Flink Committer,阿里巴巴高级开发工程师。 本文将从开发环境准备、创建 ...

    Mike617 评论0 收藏0
  • 分析JVM GC及内存情况的方法

    摘要:如果你下的没有,可以自己添加一个相关资料几个关键淘测试使用进行堆转储文件分析 当JVM响应变慢或者停滞的时候,我们往往需要对GC和其内存情况是进行分析,下面列举一些常用的分析方法和工具: 获得GC信息的方法 -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps 详细解释可以看JAVA SE 6 GC调优笔记 -XX:+PrintG...

    mudiyouyou 评论0 收藏0
  • Java代码分析器(一): JDT入门

    摘要:另载于这是一个关于抽象语法树的故事。抽象语法树是对程序代码的结构化表示,是对代码进行词法分析语法分析后得到的产物。 另载于 http://www.qingjingjie.com/blogs/2 这是一个关于抽象语法树(Abstract Syntax Tree, AST)的故事。 抽象语法树是对程序代码的结构化表示,是对代码进行词法分析、语法分析后得到的产物。编译器要用到它,很多生产力工...

    binaryTree 评论0 收藏0

发表评论

0条评论

LeexMuller

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<