对集合进行数据统计,进行计数、平均值、最值、求和
计数:count
平均值:averagingInt、averagingLong、averagingDouble
最值:maxBy、minBy
求和:summingInt、summingLong、summingDouble
统计以上所有:summarizingInt、summarizingLong、summarizingDouble
//案例:统计员工人数、平均工资、工资总额、最高工资。
public static void main(String[] args) {
List<Person> person = Person.getPerson();
求总数
Long count = person.stream().collect(Collectors.counting());
//求平均工资
Double averageSalary = person.stream().collect(Collectors.averagingDouble(Person::getSalary));
//求最高工资
Optional<Integer> maxSalary = person.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare));
// 求工资之和
Integer sum = person.stream().collect(Collectors.summingInt(Person::getSalary));
// 一次性统计所有信息
DoubleSummaryStatistics summarizingSalary = person.stream().collect(Collectors.summarizingDouble(Person::getSalary));
System.out.println("员工总数:"+count);
System.out.println("平均工资:"+averageSalary);
System.out.println("最高工资:"+maxSalary.get());
System.out.println("员工工资之和:"+sum);
System.out.println("一次性统计员工工资之和:"+summarizingSalary);
}
public static void main(String[] args) {
List<Person> person = Person.getPerson();
//使用,链接所有员工姓名
String mans = person.stream().map(Person::getName).collect(Collectors.joining(","));
System.out.println("公司所有员工:"+mans);
//拼接字符串
List<String> strings = Arrays.asList("A", "B", "C", "D");
String collect = strings.stream().collect(Collectors.joining("-"));
System.out.println(collect);
}
分区:按条件将stream分为两个Map,
分组:将集合分为多个map
public static void main(String[] args) {
List<Person> person = Person.getPerson();
// 将员工按薪资是否高于8000分组
Map<Boolean, List<Person>> collect = person.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
// 将员工按性别分组
Map<String, List<Person>> collect1 = person.stream().collect(Collectors.groupingBy(Person::getSex));
// 将员工先按性别分组,再按地区分组
Map<String, Map<String, List<Person>>> collect2 = person.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
System.out.println("员工薪资是否高于八千分组:" + collect);
System.out.println("员工性别分组:" + collect1);
System.out.println("员工性别、地区分组:" + collect2);
}
public static void main(String[] args) {
/**
* 计算员工扣税后薪资总和
* */
List<Person> person = Person.getPerson();
// 每个员工减去起征点后的薪资之和---Collectors.reducing()方式
Integer sumWithout = person.stream().collect(Collectors.reducing(0,Person::getSalary, (x, y) -> (x + y - 5000)));
System.out.println("员工扣税后薪资总和"+sumWithout);
//使用stream的reduce方式
Optional<Integer> sumSalary = person.stream().map(Person::getSalary).reduce(Integer::sum);
System.out.println("员工薪资总和" + sumSalary.get());
}
public static void main(String[] args) {
/**
* 收集流处理后元素
* 对集合数据对2取余为0的数值分别收集为list和set--set不可重复,6对应的只会存储一份
* */
List<Integer> list = Arrays.asList(1, 6, 3, 4, 6, 7, 9, 6, 20);
List<Integer> collect = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
Set<Integer> set = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet());
collect.stream().forEach(System.out::println);
set.stream().forEach(System.out::print);
//员工工资大于8000的姓名和对应属性map
Map<String, Person> map = Person.getPerson().stream().filter(x -> x.getSalary() > 8000).collect(Collectors.toMap(Person::getName, p -> p));
System.out.println(map);
}
筛选工资高于8000的人,并形成新的集合。形成新集合依赖collect
public static void main(String[] args) {
List<Person> persons = Person.getPerson();
List<String> collect = persons.stream().filter(x -> x.getSalary() > 7500).map(Person::getName).collect(Collectors.toList());
System.out.print("高于8000的员工姓名:" + collect);
}
Stream支持类似集合的遍历和匹配元素,Stream中的元素以Optional类型存在
public static void main(String[] args) {
List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
// 遍历输出符合条件的元素
list.stream().filter(x -> x > 6).forEach(System.out::println);
// 匹配第一个
Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();
// 匹配任意(适用于并行流)
Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();
// 是否包含符合特定条件的元素
boolean anyMatch = list.stream().anyMatch(x -> x < 6);
System.out.println("匹配第一个值:" + findFirst.get());
System.out.println("匹配任意一个值:" + findAny.get());
System.out.println("是否存在大于6的值:" + anyMatch);
}
接收一个函数作为参数,将流中的每个值都换成另外一个流,然后把所有流连接成一个流
public static void main(String[] args) {
merge2StrList();
}
/**
* 将两个字符数组合并成一个新的字符数组
* */
public static void merge2StrList(){
List<String> srcList = Arrays.asList("o,o,p,s", "21,11,51,42");
List<String> desList = srcList.stream().flatMap(s -> {
//将集合的每个元素转化成一个stream流
String[] split = s.split(",");
Stream<String> stream = Arrays.stream(split);
return stream;
}).collect(Collectors.toList());
System.out.println("转换前的集合:" + srcList);
System.out.println("转换后的集合:" + desList);
}
接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素
public static void main(String[] args) {
demo1();
increaseSalary();
}
/**
* 英文字符串数组的元素全部改为大写。整数数组每个元素+3
* */
public static void demo1(){
String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
final List<String> strToupper = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());
List<Integer> integers = Arrays.asList(77, 8, 45, 12, 11);
final List<Integer> addIntegers = integers.stream().map(x -> x + 3).collect(Collectors.toList());
System.out.println("都转为大写的字符串是:" + strToupper);
System.out.println("都加3的数值是:" + addIntegers);
}
/**
*将员工的薪资全部增加1000
* */
public static void increaseSalary(){
List<Person> personList = Person.getPerson();
/**
* 改变原来员工集合的方式--新旧集合中的工资都改动
* */
List<Person> personNewList = personList.stream().map(personT -> {
personT.setSalary(personT.getSalary() + 1000);
return personT;
}).collect(Collectors.toList());
System.out.println("改变原集合改动前:" + personList.get(0).getName() + "--" + personList.get(0).getSalary());
System.out.println("改变原集合改动后:" + personNewList.get(0).getName() + "--" + personNewList.get(0).getSalary());
/**
* 不改变原来员工集合的方式--旧集合中的工资未改动
* */
List<Person> personNewList2 = personList.stream().map(person -> {
Person personNew = new Person(person.getName(), 0, 0, null, null);
personNew.setSalary(person.getSalary() + 1000);
return personNew;
}).collect(Collectors.toList());
System.out.println("不改变原集合改动前:" + personList.get(0).getName() + "--" + personList.get(0).getSalary());
System.out.println("不改变原集合改动后:" + personNewList2.get(0).getName() + "--" + personNewList2.get(0).getSalary());
}
public static void main(String[] args) {
String[] arr1 = {"a", "b", "c", "d"};
String[] arr2 = {"d", "e", "f", "g"};
Stream<String> stream1 = Stream.of(arr1);
Stream<String> stream2 = Stream.of(arr2);
// concat:合并两个流 distinct:去重
List<String> collect = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
System.out.println("合并去重后:" + collect);
// limit:限制从流中获得前n个数据
List<Integer> limit = Stream.iterate(1, x -> x + 6).limit(3).collect(Collectors.toList());
System.out.println("限制从流中获取前三个数据" + limit);
// skip:跳过前n个数据
List<Integer> skip = Stream.iterate(1, x -> x + 3).limit(6).skip(2).collect(Collectors.toList());
System.out.println("跳过前两个数后:" + skip);
}
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(11, 89, 110, 154, 47, 44);
/**
* 自然排序
* */
Optional<Integer> max = integers.stream().max(Integer::compareTo);
/**
* 自定义排序
* */
Optional<Integer> max1 = integers.stream().max(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
System.out.println("自然排序的最大值:" + max.get());
System.out.println("自定义排序的最大值:" + max1.get());
/**
* 获取员工工资最高的人
* */
List<Person> person = Person.getPerson();
Optional<Person> maxSalary = person.stream().min(Comparator.comparingInt(Person::getSalary));
System.out.println("员工工资最低的人:" + maxSalary.get().getSalary() + "--职员:" + maxSalary.get().getName());
}
public static void main(String [] args){
// 将员工按工资由高到低(工资一样则按年龄由大到小)排序
List<Person> person = Person.getPerson();
//按员工工资升序排序
List<String> sort = person.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName).collect(Collectors.toList());
//按员工工资降序排序
List<String> reversedSort = person.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).map(Person::getName).collect(Collectors.toList());
System.out.println("升序排序"+sort);
System.out.println("降序排序"+reversedSort);
//先按工资再按年龄升序排序
List<String> salaryThenAge = person.stream().sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName).collect(Collectors.toList());
System.out.println("先按工资再按年龄升序排序" + salaryThenAge);
//先按薪资再按年龄自定义降序排序
List<String> collect = person.stream().sorted((a, b) -> {
if (a.getSalary() == b.getSalary()) {
return b.getAge() - a.getAge();
} else {
return b.getSalary() - a.getSalary();
}
}).map(Person::getName).collect(Collectors.toList());
System.out.println("自定义先按薪资后按年龄排序" + collect);
}