来自于日常开发~~
1、提取集合中的前20个数据
```
LinkedList<UserInfo> list1 = new LinkedList<UserInfo>();
if (list.size() > 20) {
list1 = (LinkedList<UserInfo>) list.subList(0, 19);
list = list1;
}
```
2、集合中按照时间进行排序
```
private static void ListSort(List<AccountsBillsDto> list) {
Collections.sort(list, new Comparator<AccountsBillsDto>() {
@Override
public int compare(AccountsBillsDto o1, AccountsBillsDto o2) {
try {
Date dt1 = o1.getBillTime();
Date dt2 = o2.getBillTime();
if (dt1.getTime() < dt2.getTime()) {
return 1;
} else if (dt1.getTime()>dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
}
```
3、对List数据进行规律,实现模糊查询
```
List<Person> allPerson = getPerson();
System.out.println("男性人数为"+ allPerson.stream().filter(person -> person.getGen().equals("男")).count());
```
4、list循环遍历
```
原方式:
for(String name : friends) {
System.out.println(name);
}
lamada如下:
A、friends.forEach((name) -> System.out.println(name));
B、friends.forEach(name -> System.out.println(name));
如果是List<String>,则使用下方的语法进行打印
List<String> lists;
lists.forEach(a->System.out.println(a));
```
5、对集合中的元素某一个属性进行去重
```
List<PayChannelInfo> payChannelInfos = payChannelInfoMapper.selectBatchIds(strings);
//拉姆达表达式 进行去重
payChannelInfos.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(o ->o.getChannelName()))),ArrayList::new));
```
6、对集合中的元素的某一属性进行求和
```
语法:
list.stream().map(类名::属性的get方法).reduce(BigDecimal.ZERO, BigDecimal::add))
如:
list.stream().map(MyMagicMiGangVO::getCumulativeRepayPrincipal).reduce(BigDecimal.ZERO, BigDecimal::add)
```
PS: 第一次写文章 吼吼
网友评论