jdk 新特性 stream流
复制就能用
import java.util.*;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
//1.使用流中的 disctinct去重
// List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
// List<String> collect = strings.stream().distinct().collect(Collectors.toList());
// System.out.println(collect);
// 2.使用limit获取 skip去除 前n个元素
List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
// List<String> collect = strings.stream().limit(4).collect(Collectors.toList());
// List<String> collect2 = strings.stream().skip(4).collect(Collectors.toList());
// System.out.println(collect2);
// 3.使用sorted对流中元素做排序处理 按中文拼音
// List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
// List<String> collect = strings.stream().sorted(Collections.reverseOrder(Collator.getInstance(Locale.CHINA))).collect(Collectors.toList());
// System.out.println(collect);
// 4.使用allMath对集合中的数据做处理 如判断不等于空
// List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
// boolean b = strings.stream().allMatch(s -> s != "");
// System.out.println(b);
5.使用forEach遍历流 比 for 慢1倍
// List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
// strings.stream().forEach(s -> System.out.println(s));
// 6.使用tomap做转换 设计主键重复的问题
// List<String> strings = Arrays.asList("abd","bc","efg","abcd","efg","jkl");
// strings.stream().collect(Collectors.toMap());
// 11111111111111111111111111111111111111111111111111111111111111111111111
//
List<Persion> persionList = new ArrayList<>();
persionList.add(new Persion("p1",8000,23,"meal","北京"));
persionList.add(new Persion("p2",9000,22,"meal","北京"));
persionList.add(new Persion("p3",7000,23,"femeal","上海"));
persionList.add(new Persion("p4",8000,25,"femeal","青岛"));
persionList.add(new Persion("p5",10000,10,"femeal","内蒙古"));
// 筛选工资大于8000的
List<String> collet = persionList.stream().filter(x -> x.salary>8000).map(Persion :: getName).collect(Collectors.toList());
System.out.println(collet);
// 综合实力2 赛选员工工资最高的
Optional<Persion> max = persionList.stream().max(Comparator.comparingInt(Persion::getSalary));
System.out.println(max.get().getName());
// 综合实力3 将员工工资增加1000
List<Persion> collect = persionList.stream().map(persion -> {
Persion pn = new Persion();
pn.setAge(persion.getAge());
pn.setName(persion.getName());
pn.setSalary(persion.getSalary() + 1000);
return pn;
}).collect(Collectors.toList());
// 综合实力4 求工资和
Optional<Integer> reduce = persionList.stream().map(Persion::getSalary).reduce(Integer::sum);
// 综合实力5 工资分组
Map<Integer, List<Persion>> collect1 = persionList.stream().collect(Collectors.groupingBy(x -> x.getSalary()));
System.out.println(collect);
// 排序
List<Persion> collect2 = persionList.stream().sorted(Comparator.comparing(Persion::getSalary)).collect(Collectors.toList());
System.out.println(collect2);
}
static class Persion{
private String name;
private int salary;
private int age;
private String sex;
private String area;
public Persion() {
}
public Persion(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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
}