Java8 stream

  • 创建
/**
	 * 1:一个 Stream 只可以使用一次
	 */
	@Test
	public void testCreate() {

		System.out.println("1::create");
		//数组创建
		Stream<Integer> stream = Stream.of(1, 2, 34, 5, 65);
		stream.forEach(System.out::println);

		//集合创建
		List<Integer> list = new ArrayList<Integer>(){{add(3);add(4);}};
		list.forEach(System.out::println);

}
  • 合并
	@Test
	public void testConcat() {
		Stream<Integer> stream = Stream.of(1, 2, 34, 5, 65);

		//集合创建
		List<Integer> list = new ArrayList<Integer>(){{add(3);add(4);}};

		System.out.println("2::concat");
		Stream<Integer> concat = Stream.concat(stream, list.stream());
		concat.forEach(System.out::println);
	}
  • 拆解
@Test
	/**
	 * 1: static <T> Stream<T> of(T t)
	 * 参数:传递单个元素。
	 * 返回:该方法返回一个包含一个元素的流。
	 *
	 * 2: static <T> Stream<T> of(T... values)
	 * 参数:传递多个元素。
	 * 返回:该方法返回包含给定元素的流。
	 */
	public void testFlapMap() {

		Stream<Integer> stream = Stream.of(1, 2, 34, 5, 65);
		List<Integer> list = new ArrayList<Integer>(){{add(3);add(4);}};
		System.out.println("3::flapMap");
		//flapMap:拆解流,将流中每一个元素拆解成一个流
		Stream.of(stream, list.stream()).flatMap(value -> value).forEach(System.out::println);
	}
  • student
@Data
	@Accessors(chain = true)
	static class Student{
		private String name;
		private Integer age;
	}

	static List<Student> studentList = new ArrayList<>();

	static {
		Student zs = new Student().setAge(10).setName("zs");
		Student a2 = new Student().setAge(12).setName("a2");
		Student a1 = new Student().setAge(12).setName("a1");
		Student ls = new Student().setAge(10).setName("ls");

		studentList.add(zs);
		studentList.add(ls);
		studentList.add(a1);
		studentList.add(a2);
	}
  • key 值重复解决
@Test
	public void testStudent() {

		//第三个参数表示key值重复后如何处理-->s 为原来的值,a为现在的值
		Map<Integer, Student> studentMap = studentList.stream()
												   .collect(Collectors.toMap(Student::getAge, Function.identity(), (s, a) -> s));

		studentMap.forEach((k,v) -> {
			System.out.println(k + "-" + v.toString());
		});
	}
  • 连续排序
@Test
	//连续排序
	public void testStudentSort() {

		//默认升序
		//.reversed() 降序
		List<Student> studentList = StreamTest.studentList.stream()
														  .sorted(Comparator.comparing(Student::getAge).thenComparing(Student::getName))
														  .collect(Collectors.toList());

		studentList.forEach((student) -> {
			System.out.println(student);
		});
	}
  • 统计
public void testTj() {
		IntSummaryStatistics summaryStatistics = studentList.stream().collect(Collectors.summarizingInt(Student::getAge));
		System.out.println("getAverage->"+summaryStatistics.getAverage());
		System.out.println("getMax->"+summaryStatistics.getMax());
		System.out.println("getMin->"+summaryStatistics.getMin());
		System.out.println("getCount->"+summaryStatistics.getCount());
		System.out.println("getSum->"+summaryStatistics.getSum());
	}
  • 规约
@Test
	//规约
	public void testReduce() {
		Integer sum = StreamTest.studentList.stream().map(Student::getAge).reduce((x, y) -> x += y).get();

		System.out.println(sum);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值