Java 8 工作中常用到的 对list的基本应用 group by 分组 max 工作中的你绝对能用到,直接复制下来到项目中就能用。希望能点个赞
package com.bangbangke.custom.task;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
/**
* @description:Java 8 list 初始化及获取相关集合操作
*/
public class Test {
@Data
@NoArgsConstructor
public class Student {
public Date updateDate;
public Integer id;
public String name;
public Student(Date updateDate, Integer id, String name) {
this.updateDate = updateDate;
this.id = id;
this.name = name;
}
}
public void test() {
List<Student> inquiryFlowSheetDistributor = new ArrayList<Student>();
//初始化方式1
inquiryFlowSheetDistributor = new ArrayList<Student>(
Arrays.asList(new Student(new Date(),1,"sss"), new Student(new Date(),2,"sss"), new Student(new Date(),3,"sss")));
//初始化方式2
inquiryFlowSheetDistributor.addAll(Arrays.asList(new Student(new Date(),5,"sss"))) ;
//初始化方式3
List<String> strings = Stream.of("Angla", "Bob", "Cris").collect(toList());
//初始化方式4
Stream<String> string1 = Stream.of("Angla", "Bob", "Cris");
//Java 9
// List<String> name = List.of("Angla", "Bob", "Cris");
//ArrayList<String> name = Lists.newArrayList("Angla", "Bob", "Cris");
//Java 10
//var name = List.of("Angla", "Bob", "Cris");
/** 1.根据id分组 **/
Map<Integer, List<Student>> customTagsMap = (toLists(inquiryFlowSheetDistributor)).stream().collect(Collectors.groupingBy(Student::getId));
/** 2.获取list集合中所有的id**/
List<Integer> fromNameIds = inquiryFlowSheetDistributor.stream().map(p -> p.getId()).collect(toList());
//3.获取list中 最新的一条数据
Student sensor = inquiryFlowSheetDistributor.stream().max(Comparator.comparing(Student::getUpdateDate)).get();
/**4.根据memId分组,获得Student对象**/
Map<Integer, Student> rgstNameMemMap = toLists(inquiryFlowSheetDistributor).stream().collect(Collectors.toMap(Student::getId, a -> a, (k1, k2) -> k1));
/** 5.获得 想要的那一条的id集合 比如获取list中 为某个值 的所有集合**/
List<Integer> customLevelId = inquiryFlowSheetDistributor.stream().filter(item -> "A类客户".equals(item.getName())).map(Student::getId).limit(1).collect(toList());
//6.获得list中 的list集合
Map<Integer, List<Student>> customProdtMap = inquiryFlowSheetDistributor.stream().collect(Collectors.groupingBy(Student::getId));
/7./分组获取list中最新的一条数据 的合集
Map<String, List<InquiryFlowSheetMo>> mapGroup = list.stream().collect(Collectors.groupingBy(v ->
v.getInquiryId()));
List<InquiryFlowSheetMo> resultList = mapGroup.values().stream().map(listv -> listv.get(listv.size() - 1)).collect(Collectors.toList());
//8. 获取list中为某个值的所有数据的合集
list对象= list.stream().filter(s->s.getInquiryOpera().equals(InquiryTypeEnum.INQUIRY_TAKE_BACK.getCode())||s.getInquiryOpera().equals(InquiryTypeEnum.INQUIRY_AUTOMATIC_TAKE_BACK.getCode())).collect(Collectors.toList());
//##### 将list 转为map对象 并且 转成另一个对象 list<A> 转为 Map<Key,B>
List<EbayEventMappingRule> eventMappingByChannelId = xxxxx;
//根据activity 分组 并去重
mappingRuleDtoMap = eventMappingByChannelId.stream().collect(Collectors.toMap(EbayEventMappingRule::getPlatformActivity, account -> {
//此处可以自由发挥 改为自己想要的对象。
EbayEventMappingRuleDto ebayEventMappingRule = new EbayEventMappingRuleDto();
BeanUtils.copyProperties(account, ebayEventMappingRule);
return ebayEventMappingRule;
},(oldValue, newVue) -> newVue));
//输入 重复姓名的对象
Map<Object, Long> collect2 = eventList.stream().collect(
Collectors.groupingBy(Student::getName, Collectors.counting() ) );
}
// 根据某个字段遍历数据,判断重复个数大于n的对象
List<Map.Entry<String, List<Student>>> collect1 = eventList.stream().collect(groupingBy(Student::getId)).entrySet().stream().filter(e -> e.getValue().size() >= 1).collect(Collectors.toList());
/**
* 返回一个list对象 ,避免nullPoint异常
**/
public static <T> List<T> toLists(List<T> jobList) {
if (null == jobList) {
return new ArrayList<T>();
} else {
return jobList;
}
}
}