java8一些例子

1、islegal函数返回false,则list中过滤掉。

List result = list.stream().filter(w -> islegal(w.getName(),illegal)).collect(Collectors.toList());

 

2、

Set ids = list.stream().map(Student::getId).collect(Collectors.toSet());

List ids = list.stream().map(Student::getId).collect(Collectors.toList());


List mappings = ....;
Map map = mappings.stream().collect(Collectors.toMap(HxUserMapping::getUserId, HxUserMapping::getHxUserId));

 

如果list里面有重复的数据,to map 会报错,所以可以使用下面的方式:

Map map3 = list.stream().collect(Collectors.toMap(HxUserMapping::getUserId, HxUserMapping::getHxUserId, (key1, key2) -> key2));

 

3.

List result = hxLoans.stream().filter(w -> isNotIn(w.getLoanId(),wdLoanIds)).collect(Collectors.toList());
private boolean isNotIn(Long loanId, List loanIds) {
    if(loanIds.contains(loanId)) return false;
    return true;
}

//返回false则被过滤掉

你可能感兴趣的:(java)