- 引入列子用stream 和不用区别
题目: 取出姓张的名字三个字的
普通代码:
public static void main(String[] args) {
List<String> names = new ArrayList<>();
Collections.addAll(names,"张三丰","张无忌","周芷若","赵敏","张强");
System.out.println(names);
List<String> list = new ArrayList<>();
for(String name:names){
if (name.startsWith("张")){
list.add(name);
}
}
System.out.println(list);
List<String> list1 = new ArrayList<>();
for (String name :list){
if (name.length() == 3){
list1.add(name);
}
}
System.out.println(list1);
}
Stream流代码: 这边带了stream流的收集 但是流只能收集一次!!!!
System.out.println(names.stream().filter(s->s.startsWith("张")).filter(s -> s.length()==3).collect(Collectors.toList()));
//遍历stream
//.forEach(s -> System.out.println(s))
2.Collection, map, array 获取流的方法
(其中 map 又分健流 值流 以及 键值对流)
/**
* Collection 集合获取数据流
*
*
*/
Collection<String> list = new ArrayList<>();
Stream<String> s =list.stream();
//map 获取 stream流
Map<String,Integer> maps = new HashMap<>();
//健流
Stream<String> keyStream = maps.keySet().stream();
//值流
Stream<Integer> valueStream = maps.values().stream();
//键值对流
Stream<Map.Entry<String,Integer>> keyAndValue = maps.entrySet().stream();
// 数组获取流
String[] name = {"1","2","3"};
Stream<String> nameStream = Arrays.stream(name);
Stream<String> nameStream2 = Stream.of(name);
3.使用api 来给stream 加工
常见api limit ,skip,startwith, map 的使用
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("张无忌");
list.add("赵敏");
list.add("黄秋");
list.add("张三丰");
list.add("肇庆");
list.add("张三丰");
list.stream().filter(s -> s.startsWith("张")).forEach(s -> System.out.println(s));
long size = list.stream().filter(s -> s.length()==3).count();
System.out.println(size);
list.stream().filter(s -> s.startsWith("张")).limit(2).forEach(System.out::println);
System.out.println("==============");
list.stream().filter(s -> s.startsWith("张")).skip(1).forEach(System.out::println);
//map加工方法
//要求:给集合元素前面加上一个黑马的
// 第一个s 是原材料 ->第二个参数是加工后的结果
list.stream().map(s -> "黑马的:" + s).forEach(s -> System.out.println(s));
//需求 : 把所有的名称加工成一个学生对象
list.stream().map(s -> new Student(s)).forEach(s->System.out.println(s));
//简化写法 构造器应用两个参数一样
// list.stream().map(Student::new).forEach(System.out::println);
//合并流
Stream<String> s1 = list.stream().filter(s -> s.startsWith("张"));
Stream<String> s2 = Stream.of("JAVA1","JAVA2");
Stream<String> s3 = Stream.concat(s1,s2);
}
4.stream 小案列 (工资排序)
(1).Employee类
public class Employee {
private String name;
private char sex;
private double salary;
private double bonus;
private String punish; //处罚信息
public Employee() {
}
public Employee(String name, char sex, double salary, double bonus, String punish) {
this.name = name;
this.sex = sex;
this.salary = salary;
this.bonus = bonus;
this.punish = punish;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public String getPunish() {
return punish;
}
public void setPunish(String punish) {
this.punish = punish;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sex=" + sex +
", salary=" + salary +
", bonus=" + bonus +
", punish='" + punish + '\'' +
'}';
}
}
(2).案列
public class StreamTest4 {
public static double allmoney = 0;
public static double allmoney2 = 0;
public static double allmoney3 = 0;
public static void main(String[] args) {
List<Employee> one = new ArrayList<>();
one.add(new Employee("猪八戒",'男',30000,2500,null));
one.add(new Employee("孙悟空",'男',25000,1000,"顶撞上司"));
one.add(new Employee("白龙马",'男',20000,2300,null));
one.add(new Employee("小白脸",'男',20000,250,null));
List<Employee> two = new ArrayList<>();
two.add(new Employee("武松",'男',30000,2500,null));
two.add(new Employee("咯亏",'女',25000,1000,"顶撞上司"));
two.add(new Employee("李逵",'男',10000,2300,"被打"));
two.add(new Employee("宋江",'女',2000,250,null));
two.add(new Employee("和黄秋",'男',1000,250,null));
//找出最高工资员工 封装成 Topperformer对象
//指定大小规则
// Employee e = one.stream().max((e1,e2)-> Double.compare(e1.getSalary()+e1.getBonus(),e2.getSalary()+e2.getBonus()))
// .get();
// System.out.println(e);
Toperformer t = one.stream().max((e1,e2)-> Double.compare(e1.getSalary()+e1.getBonus(),e2.getSalary()+e2.getBonus()))
.map(e -> new Toperformer(e.getName(),e.getSalary()+e.getBonus())).get();
System.out.println(t);
//两个部门的平均工资去掉最高工资和最低工资
one.stream().sorted((e1,e2)->Double.compare(e1.getSalary()+e1.getBonus(),e2.getSalary()+e2.getBonus()))
.skip(1).limit(one.size()-2).forEach(e->{
//求工资共和
allmoney += (e.getBonus()+e.getSalary());
});
System.out.println("开发一部的平均工资:"+allmoney/(one.size()-2));
two.stream().sorted((e1,e2)->Double.compare(e1.getSalary()+e1.getBonus(),e2.getSalary()+e2.getBonus())).skip(1).limit(two.size()-2).forEach(e->{
allmoney2 += (e.getBonus()+e.getSalary());
});
System.out.println("开发二部的平均工资:"+allmoney2/(two.size()-2));
Stream<Employee> s1 = one.stream();
Stream<Employee> s2 = two.stream();
Stream<Employee> s3 = Stream.concat(s1,s2);
s3.sorted((e1,e2)->Double.compare(e1.getSalary()+e1.getBonus(),e2.getSalary()+e2.getBonus())).skip(1).limit(one.size()+two.size()-2)
.forEach(e->{
allmoney3+=(e.getBonus()+e.getSalary());
});
//BigDecimal
BigDecimal bigDecimal1 = BigDecimal.valueOf(allmoney3);
BigDecimal bigDecimal = BigDecimal.valueOf((one.size()+two.size()-2));
System.out.println("开发部的平均工资:"+bigDecimal1.divide(bigDecimal,2, RoundingMode.HALF_UP));
}