import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DemoUtl {
public static int index = 0;
public static void main(String[] args) throws InterruptedException {
List<String> list = new ArrayList<>();
list.add("张无忌");
list.add("周芷若");
list.add("赵敏");
list.add("小昭");
list.add("殷离");
list.add("张三");
list.add("张三丰");
//流进行过滤并输出
list.stream()
.filter(name->name.startsWith("张"))
.filter(name->name.length()==3)
.forEach(name-> System.out.println(name));
//-----------------------------------------------------------------------------------
List<Integer> numList = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
//遍历输出
numList.stream().
forEach(v-> System.out.println(v));
// 匹配第一个
Optional<Integer> first = numList.stream().filter(v -> v > 3).findFirst();
System.out.println("获取第一个大于3的数为"+first.get());
// 匹配任意(适用于并行流)
Optional<Integer> any = numList.parallelStream().filter(v -> v > 3).findAny();
System.out.println("获取并行流大于3的数为:"+any.get());
// 是否包含符合特定条件的元素
boolean b = numList.stream().anyMatch(x -> x > 6);
System.out.println("判断是否存在复合条件的数据:"+b);
//-------------------------------------------------------------------------------------------
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
personList.add(new Person("Anni", 8200, 24, "female", "New York"));
personList.add(new Person("Owen", 9500, 25, "male", "New York"));
personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
//筛选员工中工资高于8000的人,并形成新的集合。 形成新集合依赖collect
List<String> collect = personList.stream().filter(v -> v.getSalary() >= 8000).map(Person::getName)
.collect(Collectors.toList());
System.out.println(collect);
//------------------------------------------------------------------------------------------、
//获取String集合中最长的元素。
List<String> listA = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
Optional<String> max = listA.stream().max(Comparator.comparing(String::length));
System.out.println("最长的字符串:" + max.get());
//获取Integer集合中的最大值。
List<Integer> listB = Arrays.asList(7, 6, 9, 4, 11, 6);
// 自然排序
Optional<Integer> max1 = listB.stream().max(Integer::compareTo);
Optional<Integer> max2 = listB.stream().max(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
System.out.println("自然排序的最大值:" + max1.get());
System.out.println("自定义排序的最大值:" + max2.get());
//获取员工工资最高的人。
List<Person> personListA = new ArrayList<Person>();
personListA.add(new Person("Tom", 8900, 23, "male", "New York"));
personListA.add(new Person("Jack", 7000, 25, "male", "Washington"));
personListA.add(new Person("Lily", 7800, 21, "female", "Washington"));
personListA.add(new Person("Anni", 8200, 24, "female", "New York"));
personListA.add(new Person("Owen", 9500, 25, "male", "New York"));
personListA.add(new Person("Alisa", 7900, 26, "female", "New York"));
Optional<Person> max3 = personListA.stream().max(Comparator.comparingInt(Person::getSalary));
System.out.println("员工工资最大值:" + max3.get().getSalary());
// 计算Integer集合中大于6的元素的个数。
List<Integer> listC = Arrays.asList(7, 6, 4, 8, 2, 11, 9);
long count = listC.stream().filter(v -> v > 6).count();
System.out.println("list中大于6的元素个数:" + 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(x -> x + 3).collect(Collectors.toList());
System.out.println("每个元素+3:" + collect2);
// 将员工的薪资全部增加1000。
List<Person> personListB = new ArrayList<Person>();
personListB.add(new Person("Tom", 8900, 23, "male", "New York"));
personListB.add(new Person("Jack", 7000, 25, "male", "Washington"));
personListB.add(new Person("Lily", 7800, 21, "female", "Washington"));
personListB.add(new Person("Anni", 8200, 24, "female", "New York"));
personListB.add(new Person("Owen", 9500, 25, "male", "New York"));
personListB.add(new Person("Alisa", 7900, 26, "female", "New York"));
List<Person> collect3 = personListB.stream().map(person -> {
Person personNew = new Person(person.getName(), 0, 0, null, null);
personNew.setSalary(person.getSalary() + 10000);
return personNew;
}).collect(Collectors.toList());
System.out.println("一次改动前:" + personListB.get(0).getName() + "-->" + personListB.get(0).getSalary());
System.out.println("一次改动后:" + collect3.get(0).getName() + "-->" + collect3.get(0).getSalary());
// 改变原来员工集合的方式
List<Person> personListNew2 = personListB.stream().map(person -> {
person.setSalary(person.getSalary() + 10000);
return person;
}).collect(Collectors.toList());
System.out.println("二次改动前:" + personListB.get(0).getName() + "-->" + personListNew2.get(0).getSalary());
System.out.println("二次改动后:" + personListNew2.get(0).getName() + "-->" + personListNew2.get(0).getSalary());
//将两个字符数组合并成一个新的字符数组。
List<String> listD = Arrays.asList("m,k,l,a", "1,3,5,7");
List<String> collect4 = listD.stream().flatMap(v -> {
String[] split = v.split(",");
Stream<String> stream = Arrays.stream(split);
return stream;
}).collect(Collectors.toList());
System.out.println("处理前的集合:" + listD);
System.out.println("处理后的集合:" + collect4);
// ---------------------------------------------------------------------------------------
List<Integer> listE = Arrays.asList(1, 3, 2, 8, 11, 4);
Optional<Integer> sum = listE.stream().reduce((x, y) -> x + y);
// 求和方式2
Optional<Integer> sum2 = listE.stream().reduce(Integer::sum);
// 求和方式3
Integer sum3 = listE.stream().reduce(0, Integer::sum);
System.out.println(sum2.get());
// 求乘积
Optional<Integer> product = listE.stream().reduce((x, y) -> x * y);
// 求最大值方式1
Optional<Integer> maxA = listE.stream().reduce((x, y) -> x > y ? x : y);
// 求最大值写法2
Integer maxB = listE.stream().reduce(1, Integer::max);
System.out.println("list求积:" + product.get());
System.out.println("list求和:" + maxA.get() + "," + maxB);
//归集(toList、toSet、toMap)
Map<?, Person> map = personList.stream().filter(p -> p.getSalary() > 8000)
.collect(Collectors.toMap(Person::getName, p -> p));
System.out.println("toMap:" + map);
}
static class Person {
private String name; // 姓名
private int salary; // 薪资
private int age; // 年龄
private String sex; //性别
private String area; // 地区
public void setSalary(int salary) {
this.salary = salary;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setArea(String area) {
this.area = area;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getSalary() {
return salary;
}
public int getAge() {
return age;
}
public String getSex() {
return sex;
}
public String getArea() {
return area;
}
// 构造方法
public Person(String name, int salary, int age, String sex, String area) {
this.name = name;
this.salary = salary;
this.age = age;
this.sex = sex;
this.area = area;
}
}
}
jdk8 stream流基本操作
最新推荐文章于 2024-06-11 17:16:17 发布