1

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("~");

参考


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...