- map泛型方法 R对应的是返回值? Function也是
- map收集?
package com.stream;
//stream体验
//把张的 三个字的留下
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StreamTest01 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰","张无忌","周芷诺","赵敏","张三");
System.out.println(list);
for (int i = list.size()-1; i >=0 ; i--) {
if (!list.get(i).startsWith("张")){
list.remove(list.get(i));
}
}
System.out.println(list);
for (int i = list.size()-1; i >=0 ; i--) {
if (list.get(i).length() != 3){
list.remove(list.get(i));
}
}
System.out.println(list);
System.out.println("====================================");
list.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() == 3)
.forEach(s -> System.out.println(s));
}
}
package com.stream;
//获取Stream流的方式
import java.util.*;
import java.util.stream.Stream;
public class SteamDemo02 {
public static void main(String[] args) {
Collection<String> list = new ArrayList<>();
Stream<String> stream1 = list.stream();
System.out.println("=====================================================");
Map<String,Integer> map = new HashMap<>();
Set<String> set = map.keySet();
Stream<String> stream2 = set.stream();
Collection<Integer> c = map.values();
Stream<Integer> stream = c.stream();
Set<Map.Entry<String,Integer>> set1 = map.entrySet();
Stream<Map.Entry<String, Integer>> stream3 = set1.stream();
System.out.println("==================================================");
String[] names = {"张三丰","张无忌","周芷诺","赵敏","张三"};
Stream<String> stream4 = Arrays.stream(names);
Stream<String> names1 = Stream.of(names);
Stream<String> names2 = Stream.of("张三丰","张无忌","周芷诺","赵敏","张三");
}
}
package com.stream;
//Stream相关API
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class StreamDemo03 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list,"张无忌","周芷诺","赵敏","张强","张三丰","张三丰");
System.out.println(list);
/*list.stream().filter(new Predicate<String>() {
@Override
public boolean test(String s) {
return s.startsWith("张");
}
}).forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});*/
list.stream().filter(s -> s.startsWith("张")).forEach(s -> System.out.println(s));
System.out.println("=============================================");
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);
list.stream().filter(s -> s.startsWith("张") ).skip(2).forEach(System.out::println);
System.out.println("================================================");
list.stream().map(new Function<String, String>() {
@Override
public String apply(String s) {
return "black"+ s;
}
}).forEach(System.out::println);
/*list.stream().map(new Function<String, Student>() {
@Override
public Student apply(String s) {
return new Student(s);
}
}).forEach(System.out::println);*/
// list.stream().map(s-> new Student(s)).forEach(System.out::println);
list.stream().map(Student::new).forEach(System.out::println);
System.out.println("========================================================");
Stream<String> s1 = list.stream();
Stream<String> s2 = Stream.of("java1", "java2");
Stream<String> s4 = Stream.concat(s1, s2);
s4.forEach(System.out::println);
Stream<String> s3 = list.stream();
Stream<Integer> s5 = Stream.of(21, 22);
Stream<Object> concat = Stream.concat(s3, s5);
}
}
package com.stream;
//Stream相关API
public class Student {
private String name;
public Student() {
}
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}
package com.stream;
//需求:某个公司的开发部门,分为开发一部和二部,现在需要进行年中数据结算。
//员工信息至少包含了(名称、性别、工资、奖金、处罚记录)
//1.开发一部有4个员工、开发二部有5名员工,分别筛选出2个部门的最高工资的员工信息,封装成优秀员工对象Topperformer
//2.分别统计出2个部门的平均月收入,要求去掉最高和最低工资。
//3.统计2个开发部门整体的平均工资,去掉最低和最高工资的平均值。
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
public class StreamTest04 {
public static double allMoney1 ;
public static double allMoney ;
public static void main(String[] args) {
List<Employee> one = new ArrayList<>();
one.add(new Employee( "猪八戒",'男',30000 , 25000, null));
one.add(new Employee( "孙悟空",'男',25000 , 1000, "顶撞上司"));
one.add(new Employee( "沙僧",'男',20000 , 20000, null));
one.add(new Employee( "小白龙",'男',20000 , 25000, null));
List<Employee> two = new ArrayList<>();
two.add(new Employee( "武松",'男',15000 , 9000, null));
two.add(new Employee( "李逵",'男',20000 , 10000, null));
two.add(new Employee( "张三",'男',50000 , 100000, null));
two.add(new Employee( "王亮",'女',3500 , 1000, null));
two.add(new Employee( "张帅",'女',20000 , 0, null));
//1
/*Topperformer t1 = one.stream()
.max(( o1, o2)->Double.compare(o1.getSalary() + o1.getBonus(), o2.getSalary() + o2.getBonus()))
.map(new Function<Employee, Topperformer>() {
@Override
public Topperformer apply(Employee e) {
return new Topperformer(e.getName(),e.getSalary()+e.getBonus());
}
}).get();*/
Topperformer t1 = one.stream()
.max(( o1, o2)->Double.compare(o1.getSalary() + o1.getBonus(), o2.getSalary() + o2.getBonus()))
.map(e-> new Topperformer(e.getName(),e.getSalary()+e.getBonus())).get();
System.out.println(t1);
//2
one.stream().sorted((o1, o2)->Double.compare(o1.getSalary() + o1.getBonus(), o2.getSalary() + o2.getBonus()))
.skip(1).limit(one.size()-2).forEach( e->
allMoney1 += (e.getSalary()+e.getBonus())
);
System.out.println("平均工资1:"+allMoney1/(one.size()-2));
//3
Stream<Employee> s1 = one.stream();
Stream<Employee> s2 = two.stream();
Stream<Employee> s3 = Stream.concat(s1,s2);
s3.sorted((o1, o2)->Double.compare(o1.getSalary() + o1.getBonus(), o2.getSalary() + o2.getBonus()))
.skip(1).limit(one.size()+two.size()-2).forEach(e->
allMoney += (e.getSalary()+e.getBonus())
);
BigDecimal b1 = BigDecimal.valueOf(allMoney);
BigDecimal b2 = BigDecimal.valueOf(one.size()+ two.size()-2);
BigDecimal b3 = b1.divide(b2,2, RoundingMode.HALF_UP);
System.out.println("总平均工资:"+b3);
}
}
package com.stream;
//需求:某个公司的开发部门,分为开发一部和二部,现在需要进行年中数据结算。
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 + '\'' +
'}';
}
}
package com.stream;
//需求:某个公司的开发部门,分为开发一部和二部,现在需要进行年中数据结算。
public class Topperformer {
private String name;
private double money;
public Topperformer() {
}
public Topperformer(String name, double money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Topperformer{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
}
package com.stream;
//Stream流收集的方法
import java.util.*;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamDemo05 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list,"张无忌","周芷诺","赵敏","张强","张三丰","张三丰");
System.out.println(list);
Stream<String> s1 = list.stream().filter(s->s.startsWith("张"));
List<String> list1 = s1.collect(Collectors.toList());
System.out.println(list1);
Stream<String> s2 = list.stream().filter(s->s.startsWith("张"));
Set<String> set1 = s2.collect(Collectors.toSet());
System.out.println(set1);
//array
Stream<String> s3 = list.stream().filter(s->s.startsWith("张"));
Object[] objects = s3.toArray();
System.out.println(Arrays.toString(objects));
Stream<String> s4 = list.stream().filter(s->s.startsWith("张"));
/*String[] strings = s3.toArray(new IntFunction<String[]>() {
@Override
public String[] apply(int value) {
return new String[value];
}
});*/
// String[] strings = s3.toArray( value-> new String[value]);
String[] strings = s4.toArray(String[]::new);
System.out.println(Arrays.toString(strings));
}
}
标签:Stream,stream,list,new,public,String
From: https://www.cnblogs.com/799rijiyuelei/p/16929561.html