eclipse collections入门

Function

配合一下方法使用:

  • collect

  • flatCollect

  • groupBy

  • minBy

  • maxBy

  • toSortedListBy

  • sortThisBy

  • toMap

collect

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

Function<Customer, String> nameFunction = Customer::getName;
MutableList<String> customerNames = customers.collect(nameFunction);

当然,也可以简写为

MutableList<String> customerNames = customers.collect(Customer::getName);

flatCollect

MutableList<LineItem> allOrderedLineItems = company.getOrders().flatCollect(Order::getLineItems);

MutableSet<String> actualItemNames = allOrderedLineItems.collect(LineItem::getName).toSet();

Predicate

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

  • select

  • reject

  • detect

  • count

  • anySatisfy

  • allSatisfy

select

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

selectWith

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

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

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

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

rejectWith

与selectWith功能相反

MutableList<Customer> customersNotFromLondon = company.getCustomers().rejectWith(Customer::livesIn, "London");

partitionWith(集合分区)

PartitionMutableList<Customer> partitionedList = this.company.getCustomers().partitionWith(Customer::livesIn, "London");
        MutableList<Customer> customersFromLondon = partitionedList.getSelected();
        MutableList<Customer> 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<Double> 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> 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<String> 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<PetType,    Integer> petTypeCounts = UnifiedMap.newMap();

MutableSet

  • MutableList转MutableSet

MutableList<LineItem> allOrderedLineItems = company.getOrders().flatCollect(Order::getLineItems);
MutableSet<String> actualItemNames = allOrderedLineItems.collect(LineItem::getName).toSet();
  • UnifiedSet.newSetWith

MutableSet<Person> people = UnifiedSet.newSetWith(mrSmith,mrsSmith,mrJones);    
int numAddresses = people.collect(addressFunction).size();    
System.out.println(numAddresses);

MutableMultiMap

MutableListMultimap<String, Customer> multimap = company.getCustomers().groupBy(Customer::getCity);
Assert.assertEquals(FastList.newListWith(this.company.getCustomerNamed("Mary")),multimap.get("Liphook"));
MutableList<Customer> Liphooks = multimap.get("Liphook");

ArrayIterate

public boolean hasSupply(String itemName){
    return ArrayIterate.contains(itemNames,itemName);
}

ListIterate

List<Order> orders = this.company.getMostRecentCustomer().getOrders();
MutableList<Double> orderValues = ListIterate.collect(orders, Order::getValue);

makeString

String tildeSeparatedNames = company.getSuppliers().collect(Supplier::getName).makeString("~");

参考

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值