stream
对容器进行筛选,过滤,查找,去重,映射,排序等操作
配合 lam表达式 操作简介明了
demo:
G:\project\demo\basic\stream
创建Stream流
获取list对象并进行 逻辑操作,list里是啥不关心
1.通过Collection 系列集合提供的 stream() , parallelStream() 获取
List a = new ArrayList();
a.stream();
a.parallelStream();
2.通过Arrays 的静态方法 stream() 获取 数组流
String[] a = new String[10];
Stream<String> stream = Arrays.stream(a);
3.通过Stream的静态方法
Stream<String> stream = Stream.of("a","b","c");
stream()_筛选
- 根据规则进行数据过滤 ,减少对象,多种筛选是串行执行的,执行完一种筛选返回新流,next筛选才会基于新流筛选
- filter :接受lambda,从流中排出某些元素,返回list数组
@Test public void test() throws Exception { List<HeroPojo> list = Arrays.asList( new HeroPojo("1","10","11"), new HeroPojo("2","11","15"), new HeroPojo("3","11","11") ); List<HeroPojo> newList = list.stream().filter(e -> e.getPower().equals("11")).collect(Collectors.toList()); for (HeroPojo m:newList) { System.out.println(m.getName()); } } 输出: 2
- limit: 截取满指定数元素的流
@Test public void test1() throws Exception { List<HeroPojo> list = Arrays.asList( new HeroPojo("1","10","11"), new HeroPojo("2","11","15"), new HeroPojo("3","11","11") ); List<HeroPojo> newList = list.stream().limit(1).collect(Collectors.toList()); for (HeroPojo m:newList) { System.out.println(m.getName()); } } 输出: 1
- skip:跳过指定数的元素,返回删除了前n个元素的新流,如果元素不足N返回null
@Test public void test2() throws Exception { List<HeroPojo> list = Arrays.asList( new HeroPojo("1","10","11"), new HeroPojo("2","11","15"), new HeroPojo("3","11","11") ); List<HeroPojo> newList = list.stream().skip(2).collect(Collectors.toList()); for (HeroPojo m:newList) { System.out.println(m.getName()); } } 输出: 3
- distinct:去重,根据对象的hashcode()和equals()方法去除重复元素
@Test public void test3() throws Exception { List<HeroPojo> list = Arrays.asList( new HeroPojo("1","10","10"), new HeroPojo("1","10","10") ); List<HeroPojo> newList = list.stream().distinct().collect(Collectors.toList()); for (HeroPojo m:newList) { System.out.println(m.getName()); } } 输出:
stream()_映射
- map:接受一个函数,函数会将每个元素作为入参执行,并映射成新的元素,或者提取元素的属性。
@Test public void test4() throws Exception { List<HeroPojo> list = Arrays.asList( new HeroPojo("a","10","10"), new HeroPojo("b","10","10") ); List<String> newList = list.stream().map(e -> e.getName().toUpperCase()).collect(Collectors.toList()); for (String m:newList) { System.out.println(m); } } 输出: A B
- flatMap: 接受一个函数作为参数,将流中每个值转换为另一个流,并把所有的流连接成一个流
stream()_排序
sorted():自然排序(comparable):根据类重写的compareTo() 排序
@Test public void test6() throws Exception { List<String> list = Arrays.asList("c","a","b"); List<String> newList = list.stream().sorted().collect(Collectors.toList()); for (String m:newList) { System.out.println(m); } } 输出 a b c
sorted(Comparator comparator):自定义排序 有问题
@Test public void test5() throws Exception { List<HeroPojo> list = Arrays.asList( new HeroPojo("法师","10","11"), new HeroPojo("刺客","11","24"), new HeroPojo("法师","12","10"), new HeroPojo("刺客","13","25"), new HeroPojo("战士","14","11"), new HeroPojo("法师","15","10") ); List<HeroPojo> newList = list.stream().sorted((e1,e2) -> { if(!(e1.getName().equals(e2.getName()))){ return e1.getPower().compareTo(e2.getPower()); }else{ return e1.getName().compareTo(e2.getName()); } }).collect(Collectors.toList()); for (HeroPojo m:newList) { System.out.println(m.getName() + ":" + m.getPower()); } } 输出
查找与匹配(终止操作)
allMatch-检查是否匹配所有元素,返回boolean
anyMatch-是否至少匹配一个元素,返回boolean
noneMatch-是否没有匹配所有元素
findFirst-返回第一个元素
findAny-返回任何元素
count-返回流中元素总数
max-返回流中最大值
min-返回流中最小值
归约