Java8lambda排序筛选分组求和最⼤值平均值
在⼯作中发现Java8使⽤很普遍,整理⼯作中常⽤的Java8集合语法。
package cc.pigg.ba.study.stream;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.*;
import java.util.stream.Collectors;
public class JavaStreamTest {
private static List<Person> personList =new ArrayList<>();
public static void main(String[] args){
personList.add(new Person(1L ,"猪爸爸",30,"男"));
安全生产月总结
personList.add(new Person(2L ,"猪妈妈",28,"⼥"));
personList.add(new Person(3L ,"佩奇",5,"⼥"));
personList.add(new Person(4L ,"乔治",2,"男"));
System.out.println("hello");
System.out.println("===foreach循环===");
for(Person person : personList){
System.out.String());
}
System.out.println("===Java8的foreach()===");
爱国人物事迹personList.forEach(person -> System.out.String()));
System.out.println("===foreach()年龄都加1岁===");
personList.forEach(p -> p.Age()+1));
print();
音乐教学反思System.out.println("===map toList===");
藕丝怎么炒好吃//获取id的List集合
List<Long> ids = personList.stream().map(Person::getId).List());
ids.forEach(id -> System.out.println(id));
System.out.println("===map toSet===");
//获取性别的Set集合
Set<String> genders = personList.stream().map(Person::getGender).Set());
genders.forEach(gender -> System.out.println(gender));
System.out.println("===获取名称以逗号分隔===");
唯有孤独永恒
String names = personList.stream().map(Person::getName).collect(Collectors.joining(","));
System.out.println(names);
System.out.println("===List 转 Map<Long, Person> 根据ID分组===");
//如果ID重复就⽤第⼀个
Map<Long, Person> mapById = personList.stream().Map(Person::getId, a -> a,(k1, k2)-> k1));
白水岩瀑布
mapById.forEach((key, value)-> System.out.println(key +" -> "+ String()));
System.out.println("===List 转 Map<String, Person> 根据gender分组===");
//如果gender重复就⽤第⼀个
Map<String, Person> mapByGender = personList.stream().Map(Person::getGender, a -> a,(k1, k2)-> k1)); mapByGender.forEach((key, value)-> System.out.println(key +" -> "+ String()));
System.out.println("===List 转 Map<String, List<Person>> 根据gender分组===");
Map<String, List<Person>> groupByGender = personList.stream().upingBy(Person::getGender));
groupByGender.forEach((key, value)-> System.out.println(key +" -> "+ String()));
System.out.println("===统计值===");
//最⼤年龄
Integer maxAge = personList.stream().mapToInt(Person::getAge).max().getAsInt();
//最⼩年龄
Integer minAge = personList.stream().mapToInt(Person::getAge).min().getAsInt();
//年龄求和
Integer sumAge = personList.stream().mapToInt(Person::getAge).sum();
//平均年龄
Double avgAge = personList.stream().mapToInt(Person::getAge).average().getAsDouble();
System.out.println("maxAge = "+ maxAge);
阴天歌词
System.out.println("minAge = "+ minAge);
System.out.println("sumAge = "+ sumAge);
System.out.println("avgAge = "+ avgAge);
System.out.println("===List<Integer> 的最⼤值===");
System.out.println("最⼤的ID="+ Collections.max(ids));
System.out.println("===SummaryStatistics统计===");
IntSummaryStatistics iss = personList.stream().mapToInt(Person::getAge).summaryStatistics();
System.out.println("max = "+ Max());
System.out.println("min = "+ Min());
System.out.println("sum = "+ Sum());
System.out.println("avg = "+ Average());
System.out.println("===分组统计利⽤SummaryStatistics===");
//求不同性别的平均年龄
Map<String, IntSummaryStatistics> genderSummary =
personList.stream().upingBy(Person::getGender, Collectors.summarizingInt(Person::getAge)));
genderSummary.forEach((k, v)-> System.out.println(k +" -> 最⼤年龄="+ v.getMax()+", 平均年龄="+ v.getAverage()+", ⼈数="+ v.getCount()));
System.out.println("===分组统计数量===");
//求不同性别的⼈数
Map<String, Long> genderSummary2 =
personList.stream().upingBy(Person::getGender, unting()));
genderSummary2.forEach((k, v)-> System.out.println(k +" -> ⼈数="+ v));
System.out.println("===filter 筛选出成⼈男===");
List<Person> mans = personList.stream().filter(person ->"男".Gender())&& Age()>=18).List()); mans.forEach(person -> System.out.String()));
System.out.println("===Comparator 根据年龄升序排===");
personList.paring(Person::getAge));证券商
print();
System.out.println("===Comparator 根据ID降序排===");
personList.paring(Person::getId).reverd());
print();
System.out.println("===Comparator 组合排序===");
personList.paring(Person::getGender).thenComparing(Person::getAge));
print();
System.out.println("goodbye");
}
private static void print(){
personList.forEach(person -> System.out.String()));
}
}
@Data
@AllArgsConstructor
class Person {
private Long id;
private String name;
private Integer age;
private String gender;
如果本⽂对您有帮助,就点个赞 吧