lambda表达式

lambda表达式有个限制,那就是只能引用 final 或 final 局部变量,这就是说不能在lambda内部修改定义在域外的变量。

Compile time error : local variables referenced from a lambda expression must be final or effectively final

1,List转Map//针对重复key的 覆盖之前的value  

List<Person> list = new ArrayList<>();
Map<String, Person> map2 = list.stream().filter(e->StringUtil.isNotBlank(e.getName())).collect(Collectors.toMap(Person::getName , (p) -> p,(k,v)->v));

//value为空,直接存放  不调用map.merge
Map<String, String> map3 = list.stream().collect(Collector.of(HashMap::new, (m,per)->m.put(per.getName(),per.getSex()), (k,v)->v, Characteristics.IDENTITY_FINISH));

  

Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));

2,List转List

List<String> userIdList = Lists.newArrayList();
List<SubUserVO> subUserVOList = new ArrayList<>();
subUserVOList.forEach(e -> userIdList.add(e.getId()));
List<CluePoolDO> search = new ArrayList<>();
List<Long> clueIds = search.stream().map(CluePoolDO::getId).collect(Collectors.toList());

3,条件过滤

List<String> strList = new ArrayList<>();
List<String> filtered = strList.stream().filter(x -> x.length()> 2).collect(Collectors.toList());
ArrayList<String> httpStrsTmp=new ArrayList<>();
if(httpStrsTmp.stream().filter(f->f.equals("x")).count()<=0){
	//xxxxxx
}

  

4,List转拼接字符串

List<String> G7 = Arrays.asList("USA", "Japan", "France", "Germany", "Italy", "U.K.","Canada");
String G7Countries = G7.stream().map(x -> x.toUpperCase()).collect(Collectors.joining(", "));

5,List的最大,小,求和,平均值计算

List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
IntSummaryStatistics stats = primes.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("最大值 : " + stats.getMax());
System.out.println("最小值 : " + stats.getMin());
System.out.println("总   和 : " + stats.getSum());
System.out.println("平均值 : " + stats.getAverage());

6,List迭代

List<OrderItem> itemList = new ArrayList<>();
itemList.stream().forEach(item -> {
    System.out.println(item.getId());
});
List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
features.forEach(n -> System.out.println(n));
或者
features.forEach(System.out::println);

7,List去重

List<String> originList = new ArrayList<>();
originList  = originList.stream().distinct().collect(Collectors.toList());

  

// 将评论时间按照倒序排列后再放入
List<FbCommentVO> comments = fbPosts.getComments();
if (!CollectionUtils.isEmpty(comments)) {
    comments.sort(Comparator.comparing(FbCommentVO::getCommentTime).reversed());
}

 8,排序(reversed反转)

List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge).reversed());


List<ShopSellerMenuVO> collect = list.stream().sorted(Comparator.comparing(ShopSellerMenuVO::getId)).collect(Collectors.toList());
 

 9,获取list最大、最小的一条

List<Date> list = new ArrayList();
Date maxDate = list.stream().max((o1,o2) -> o1.compareTo(o2)).get();
Date minDate = list.stream().min((o1,o2) -> o1.compareTo(o2)).get();

List<Employee> userList = new ArrayList();
Employee max = userList.stream().max(Comparator.comparingInt(Employee ::getAge)).get();
Employee min= userList.stream().min(Comparator.comparingInt(Employee ::getAge)).get();
//注意空指针,对年龄判空

  

10,移动集合中元素的位置

Collections.swap(list,fromIndex,toIndex);

  

11,分组

productSpecNumsList.stream().collect(groupingBy(ProductSpecNums::getSpecId));

  

  

  

  

原文地址:https://www.cnblogs.com/wanhua-wu/p/9035980.html