import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test_s {
public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, "male", "New York"));
personList.add(new Person("Jack", 7000, "male", "Washington"));
personList.add(new Person("Lily", 7800, "female", "Washington"));
personList.add(new Person("Anni", 8200, "female", "New York"));
personList.add(new Person("Owen", 9500, "male", "New York"));
personList.add(new Person("Alisa", 7900, "female", "New York"));
List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
//遍历输出符合条件的元素
list.stream().filter(n->n>6).forEach(System.out::println);
//匹配第一个
Optional<Integer> first = list.stream().filter(n -> n <= 6).findFirst();
// System.out.println(first.get());
//匹配任意,适用于并行流
Optional<Integer> any = list.parallelStream().filter(n -> n > 6).findAny();
// System.out.println(any.get());
//是否包含符合特定条件的元素
boolean match = list.stream().allMatch(n -> n < 6);
System.out.println("匹配第一个值:" + first.get());
System.out.println("匹配任意一个值:" + any.get());
System.out.println("是否存在大于6的值:" + match);
//筛选员工中工资高于8000的人,并形成新的集合
List<String> collect = personList.stream().filter(n -> n.getSalary() > 8000).map(Person::getName).collect(Collectors.toList());
// System.out.println(collect);
List<String> stringlist = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
Optional<String> max = stringlist.stream().max(Comparator.comparing(String::length));
// System.out.println(max.get());
//获取Integer集合中的最大值。
List<Integer> integerList = Arrays.asList(7, 6, 9, 4, 11, 6);
Optional<Integer> integer = integerList.stream().max(Integer::compareTo);
// System.out.println(integer.get());
//获取员工工资最高的人
Optional<Person> max1 = personList.stream().max(Comparator.comparing(Person::getSalary));
// System.out.println(max1.get());
//计算integer集合中大于6的元素的个数
List<Integer> list2 = Arrays.asList(7, 6, 4, 8, 2, 11, 9);
long count = list2.stream().filter(n -> n > 6).count();
// System.out.println(count);
//英文字符串数组的元素全部改为大写,整数数组每个元素+3
String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
List<String> collect1 = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());
// System.out.println(collect1);
List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);
List<Integer> collect2 = intList.stream().map(n -> n + 3).collect(Collectors.toList());
// System.out.println(collect2);
//将员工的薪资全部增加1000
List<Person> collect3 = personList.stream().map(person -> {
Person personNew = new Person(person.getName(), 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("一次改动后:" + collect3.get(0).getName() + "-->" + collect3.get(0).getSalary());
// 改变原来员工集合的方式
List<Person> personListNew2 = personList.stream().map(person -> {
person.setSalary(person.getSalary() + 1000);
return person;
}).collect(Collectors.toList());
// System.out.println("二次改动前:" + personList.get(0).getName() + "-->" + personList.get(0).getSalary());
// System.out.println("二次改动后:" + personListNew2.get(0).getName() + "-->" + personListNew2.get(0).getSalary());
//将两个字符组合成一个新的字符数组
List<String> list4 = Arrays.asList("m,k,l,a", "1,3,5,7");
List<String> collect4 = list4.stream().flatMap(s -> {
String[] strings = s.split(",");
Stream<String> stream = Arrays.stream(strings);
return stream;
}).collect(Collectors.toList());
// System.out.println("处理前的集合:" + list4);
// System.out.println("处理后的集合:" + collect4);
//求integer集合的元素之和、乘积和最大值
List<Integer> list5 = Arrays.asList(1, 3, 2, 8, 11, 4);
//求和方式1
Optional<Integer> sum = list5.stream().reduce((x, y) -> x + y);
//求和方式2
Optional<Integer> sum2 = list5.stream().reduce(Integer::sum);
//求和方式3
Integer sum3 = list5.stream().reduce(0, Integer::sum);
//求乘积
Optional<Integer> reduce = list5.stream().reduce((x, y) -> x * y);
//求最大值方式1
Integer max2 = list5.stream().reduce(1, Integer::max);
//求最大值方式2
Optional<Integer> reduce1 = list5.stream().reduce((x, y) -> x > y ? x : y);
//求员工人数
long count1 = personList.stream().count();
// System.out.println("员工人数:"+count1);
//求平均工资
Double collect5 = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
// System.out.println(collect5);
//求最高工资
Optional<Integer> collect7 = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare));
//求工资之和
Long collect6 = personList.stream().collect(Collectors.summingLong(Person::getSalary));
// 将员工按薪资是否高于8000分组
Map<Boolean, List<Person>> part = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
//将员工按性别分组
Map<String, List<Person>> group = personList.stream().collect(Collectors.groupingBy(Person::getSex));
// 将员工先按性别分组,再按地区分组
Map<String, Map<String, List<Person>>> group2 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
// System.out.println("员工按薪资是否大于8000分组情况:" + part);
// System.out.println("员工按性别分组情况:" + group);
// System.out.println("员工按性别、地区:" + group2);
//将员工按工资由高到低(工资一样则按年龄由大到小)排序
List<String> collect8 = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName).collect(Collectors.toList());
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> collect9 = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
System.out.println("流合并:"+collect9);
//limit:限制从流中获得前n个数据
List<Integer> collect10 = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
System.out.println("limit:" + collect10);
//skip:跳过前n个数据
List<Integer> collect11 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());
System.out.println(collect11);
}
}
jdk8中Stream的使用
最新推荐文章于 2025-05-14 22:27:35 发布