java8中的Stream操作
2019-04-13 本文已影响0人
虫儿飞ZLEI
就是操作集合,java代码一样可以实现,只是stream写起来方便些
1.Stream的三个操作步骤
-
创建stream
-
中间操作(并不会操作,只有终止操作了以后,中间操作的代码才会执行)
-
终止操作(惰性求值)
2. 创建Stream的方式
2.1 调用集合的stream方法
ArrayList<String> list = new ArrayList<>();
Stream<String> stream = list.stream();
2.2 调用Arrays的stream方法
People[] perples = new People[10];
Stream<People> stream1 = Arrays.stream(perples);
2.3 Stream的静态of方法
Stream<People> perples1 = Stream.of(perples);
2.4 创建无限流
//迭代
Stream<Integer> iterate = Stream.iterate(0, (x) -> x + 2);
iterate.limit(19).forEach(System.out::println);
//生成
Stream<Double> generate = Stream.generate(Math::random);
generate.limit(5).forEach(System.out::println);
3. 中间操作
3.1 Filter
List<People> peoples = Arrays.asList(
new People("zhang1", 1, 1),
new People("zhang2", 2, 2),
new People("zhang3", 3, 3),
new People("zhang4", 4, 4),
new People("zhang5", 5, 5)
);
Stream<People> peopleStream = peoples.stream()
.filter((p) -> p.getI()>2);
peopleStream.forEach((p)->System.out.println(p.getS()));
}
3.2 limit
List<People> peoples = Arrays.asList(
new People("zhang1", 1, 1),
new People("zhang2", 2, 2),
new People("zhang3", 3, 3),
new People("zhang4", 4, 4),
new People("zhang5", 5, 5)
);
Stream<People> peopleStream = peoples.stream()
.limit(2);
peopleStream.forEach((p)->System.out.println(p.getS()));
3.3 skip 跳过前几个
List<People> peoples = Arrays.asList(
new People("zhang1", 1, 1),
new People("zhang2", 2, 2),
new People("zhang3", 3, 3),
new People("zhang4", 4, 4),
new People("zhang5", 5, 5)
);
Stream<People> peopleStream = peoples.stream()
.skip(2);
peopleStream.forEach((p)->System.out.println(p.getS()));
3.4 distinct去重(People类必须实现hashcode和equals方法才可以去重)
List<People> peoples = Arrays.asList(
new People("zhang1", 1, 1),
new People("zhang2", 2, 2),
new People("zhang3", 3, 3),
new People("zhang4", 4, 4),
new People("zhang5", 5, 5),
new People("zhang5", 5, 5),
new People("zhang5", 5, 5)
);
Stream<People> peopleStream = peoples.stream()
.distinct();
peopleStream.forEach((p)->System.out.println(p.getS()));
3.4 map 在map中可以通过代码实现上述Filter、distinct、skip、limit等等的操作
List<People> peoples = Arrays.asList(
new People("zhang1", 1, 1),
new People("zhang2", 2, 2),
new People("zhang3", 3, 3),
new People("zhang4", 4, 4),
new People("zhang5", 5, 5)
);
Stream<People> peopleStream = peoples.stream()
.map(people -> {
//接收一个任意参数,返回一个任意参数,基本上可以做任何操作了
people.setS(people.getS().toUpperCase());
return people;
});
peopleStream.forEach((p)->System.out.println(p.getS()));
3.5 sort 排序
- 自然排序
List<Integer> ints = Arrays.asList(
1,2,3,4,5,6,7
);
Stream<Integer> intstream = ints.stream()
.sorted();
intstream.forEach((s)->System.out.println(s));
- 自己写排序
List<People> peoples = Arrays.asList(
new People("zhang1", 1, 1),
new People("zhang3", 3, 3),
new People("zhang2", 2, 2),
new People("zhang5", 5, 5),
new People("zhang4", 4, 4)
);
peoples.stream().sorted((p1,p2)->{
return p1.getI()-p2.getI();
}).forEach(people -> System.out.println(people.getS()));
4. 终止操作
只有执行了终止操作,中间操作的代码才会执行
比如可以把所有people的某个int字段加起来
返回过一个处理过的集合
List<People> peoples = Arrays.asList(
new People("zhang1", 1, 1),
new People("zhang3", 3, 3),
new People("zhang2", 2, 2),
new People("zhang5", 5, 5),
new People("zhang4", 4, 4)
);
List<People> list = peoples.stream().limit(2).collect(Collectors.toList());