不可变集合
不可变就是不可被修改。
集合的数据在创建的时候提供,并且在整个生命周期中都不可改变。
List<Integer> list1=List.of(33,222,11);
Set<String> names=Set.of("Damon","Stefan","ELena");
Set<String> names=Set.of("Damon","Stefan","ELena","Damon");//如果有重复元素会报错。
Map<String ,Integer> maps=Map.of("fxw",1,"sw",3);
Stream流
用于简化集合和数组操作的API。
public class stream_test {
public static void main(String[] args) {
List<String > names=new ArrayList<>();
Collections.addAll(names,"Damon","Stefan","ELena","Dell");;
//找到D开头的名字
//1。for遍历
List<String > Dname=new ArrayList<>();
for (String name : names) {
if (name.startsWith("D") && name.length()==4){
Dname.add(name);
}
}
System.out.println(Dname);
//2。使用Stream实现
names.stream().filter(s -> s.startsWith("D")).filter(s -> s.length()==4).forEach(s -> System.out.println(s));
}
}
Stream流的三类方法
获取Stream流
创建一条流水线,并把数据放到流水线上准备进行操作
public class Demo1 {
public static void main(String[] args) {
/**
* Collection集合获取流
*/
List<String> list=new ArrayList<>();
Stream<String> stream=list.stream();
/**
* Map集合获取流
*/
Map<String ,Integer> map=new HashMap<>();
//键流
Stream<String> keystream=map.keySet().stream();
//值流
Stream<Integer> valuestream=map.values().stream();
//键值对流(拿整体)
Stream<Map.Entry<String ,Integer>> kvstream =map.entrySet().stream();
/**
* 数组获取流
*/
String[] names={
"Damon","Stefan","ELena","Dell"};
//方法1
Stream<String> namestream1= Arrays.stream(names);
//方法2
Stream<String> namestream2=Stream.of(names);
}
}
中间方法
public class Demo2 {
public static void main(String[] args) {
List<String > names=new ArrayList<>();
Collections.addAll(names,"Damon","Stefan","ELena","Dell");
//filter过滤
names.stream().filter(s -> s.startsWith("D")).filter(s -> s.length()==4).forEach(s -> System.out.println(s));
//map加工方法:第一个参数原材料 -> 第二个参数是加工后的结果
//给集合元素的前面都加上 2020
names.stream().map(s -> 2020+s).forEach(s -> System.out.println(s));
//吧所有的名称都加工成一个学生对象
names.stream().map(s -> new Student(s)).forEach(s -> System.out.println(s));
//合并流
Stream<String> stream1=names.stream();
Stream<Integer> stream2=Stream.of(11,22);
Stream<Object> stream3=Stream.concat(stream1,stream2);
stream3.forEach(s-> System.out.println(s));
}
}
中间方法也称为非中间方法,调用完成后返回新的Stream流可以继续使用,支持链式编程。
在Stream流中无法直接修改集合、数组中的数据。
Demo
//注意:流只能使用一次
public class Demo3 {
public static double allmoney1;
public static double allmoney2;
public static double allmoney;
public static void main(String[] args) {
List<emp> one=new ArrayList<>();
String[] p={
"Damon","Stefan","ELena","Dell"};
one.add(new emp("fxw1",'m',3000,2000,null));
one.add(new emp("fxw2",'f',400,20,"摸鱼"));
one.add(new emp("fxw3",'m',3000,1000,"韭菜"));
one.add(new emp("fxw4",'m',1000,500,"韭菜"));
List<emp> two=new ArrayList<>();
two.add(new emp("wyx1",'m',323,21,"迟到"));
two.add(new emp("wyx2",'f',222,223,"摸鱼"));
two.add(new emp("wyx3",'m',500,100,null));
two.add(new emp("wyx4",'f',555,30,"吃东西"));
two.add(new emp("wyx5",'f',888,90,"摸鱼"));
//一部的最高工资员工
topemp t1=one.stream().max((emp o1, emp o2)-> Double.compare(o1.getSalary()+o1.getBouns(),o2.getSalary()+ o2.getBouns()) ).map(s-> new topemp(s.getName(),s.getSalary()+s.getBouns())).get();
//一部的最高工资员工
topemp t2=two.stream().max((emp o1, emp o2)-> Double.compare(o1.getSalary()+o1.getBouns(),o2.getSalary()+ o2.getBouns()) ).map(s-> new topemp(s.getName(),s.getSalary()+s.getBouns())).get();
//统计平均工资,去除最高工资和最低工资
one.stream().sorted((emp o1, emp o2)-> Double.compare(o1.getSalary()+o1.getBouns(),o2.getSalary()+o2.getBouns())).skip(1).limit(one.size()-2).forEach(s->allmoney1+=(s.getSalary()+s.getBouns()));
System.out.println(allmoney1/(one.size()-2));
two.stream().sorted((emp o1, emp o2)-> Double.compare(o1.getSalary()+o1.getBouns(),o2.getSalary()+o2.getBouns())).skip(1).limit(two.size()-2).forEach(s->allmoney2+=(s.getSalary()+s.getBouns()));
System.out.println(allmoney2/(one.size()-2));
//统计全部门的平均工资
Stream<emp> s1=one.stream();
Stream<emp> s2=two.stream();
Stream<emp> s3=Stream.concat(s1,s2);
s3.sorted((emp o1, emp o2)-> Double.compare(o1.getSalary()+o1.getBouns(),o2.getSalary()+o2.getBouns())).skip(1).limit(one.size()+two.size()-2).forEach(s->allmoney=(s.getSalary()+s.getBouns()));
System.out.println(allmoney/(one.size()+two.size()-2));//可能会有精度问题
//BigDecimal解决精度问题
BigDecimal b1=BigDecimal.valueOf(allmoney);
BigDecimal b2=BigDecimal.valueOf(one.size()+two.size()-2);
System.out.println(b1.divide(b2,2, RoundingMode.HALF_UP));
}
}
终结方法
Stream流的收集操作
收集Stream流的含义:就是把Stream流操作后的结果数据转回到集合或者数组中去。
Stream流:方便操作集合/数组的手段。
List<String > names=new ArrayList<>();
Set
Collections.addAll(names,"Damon","Stefan","ELena","Dell");
List<String> res1=names.stream().collect(Collectors.toList());
Set<String> res2=names.stream().collect(Collectors.toSet());
Object[] arr=names.stream().toArray();//转为数组