TestStream1

package com.imooc.controller;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author jiangli
 * @date 2018/11/3 20:54
 */
public class TestStream1 {
	List<Book> books= Arrays.asList(
			new Book("三国演义","罗贯中",40.00),
			new Book("水浒传","施耐庵",20.00),
			new Book("红楼梦","曹雪芹",10.00),
			new Book("西游记","吴承恩",30.00),
			new Book("西游记","吴承恩",30.00),
			new Book("西游记","吴承恩",30.00)
	);

	/*
		筛选与切片
		filter--接收lambda,从流中排除某些元素
		limit--截断流,使其元素不超过给定数量
		skip(n)--跳过元素,返回一个去掉了前n个元素的流.若流中元素不足n个,则返回一个空流,与limit(n)互补
		distinct--筛选,通过流所生成元素的hashCode()和equals()去重
	 */
	@Test
	public void test1() {
		Stream<Book> bookStream = books.stream().filter(e -> e.getPrice() > 10).limit(2);
		bookStream.forEach(System.out::println);
		System.out.println("----------------");
		books.stream().map(Book::getName).forEach(System.out::println);
	}

	@Test
	public void test2() {
		Stream<Book> bookStream = books.stream().filter(e -> e.getPrice() > 10).distinct();
		Stream<Book> bookStream1 = books.stream().filter(e -> e.getPrice() > 10).skip(2);
	}

	/*
	映射
	map--接收Lambda,将元素转换成其他形式或提取信息.接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素
	flatMap--接收一个函数作为参数,将流中的每个元素都换成另一个流,然后把所有流连接成一个流
	 */
	@Test
	public void test3() {
		List<String> list = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
		list.stream().map(s->s.toUpperCase()).forEach(System.out::println);

		list.stream().flatMap(TestStream1::filterCharacter).forEach(System.out::println);
	}

	private static Stream<Character> filterCharacter(String str) {
		List<Character> list=new ArrayList<>();
		for (Character c : str.toCharArray()) {
			list.add(c);
		}
		return list.stream();
	}

	/*
	排序
	sorted()--自然排序(Comparable)
	sorted(Comparator com)--定制排序(Comparator)
	 */
	@Test
	public void test4() {
		List<String> list = Arrays.asList("ccc", "eee", "aaa", "ddd", "bbb");
		list.stream().sorted().forEach(System.out::println);

		books.stream().sorted((b1,b2)->{
			if (b1.getPrice().equals(b2.getPrice())) {
				return b1.getWriter().compareTo(b2.getWriter());
			}else {
				return b1.getPrice().compareTo(b2.getPrice());
			}
		}).forEach(System.out::println);
	}

	@Test
	public void test5() {
		Optional<Book> first = books.stream().sorted((b1, b2) -> -Double.compare(b1.getPrice(), b2.getPrice())).findFirst();
		System.out.println(first.get());

		long count = books.stream().count();
		Optional<Book> max = books.stream().max(Comparator.comparing(Book::getPrice));
		System.out.println(max.get());
		//找出最低价格是多少
		Optional<Double> min = books.stream().map(Book::getPrice).min(Double::compare);
		System.out.println(min.get());
	}

	/*
	归约
	reduce(T identity,BinaryOperator bo)/reduce(BinaryOperator bo)--将流中的元素结合起来得到一个值
	 */
	@Test
	public void test6() {
		Double sum = books.stream().map(Book::getPrice).reduce(0.0, Double::sum);
		System.out.println(sum);

	}

	/*
	收集
	collect--将流转换为其他形式.接收一个Collector的实现,用于给Stream中元素汇总的方法
	 */
	@Test
	public void test7() {
		List<String> list = books.stream().map(Book::getName).collect(Collectors.toList());
		list.forEach(System.out::println);
		System.out.println("-----------------");
		Set<String> set = books.stream().map(Book::getName).collect(Collectors.toSet());
		System.out.println("-----------------");
		HashSet<String> hashSet = books.stream().map(Book::getName).collect(Collectors.toCollection(HashSet::new));

		//总数量
		Long count = books.stream().collect(Collectors.counting());
		System.out.println(count);
		//平均值
		Double avg = books.stream().collect(Collectors.averagingDouble(Book::getPrice));
		System.out.println(avg);
		//总和
		Double sum = books.stream().collect(Collectors.summingDouble(Book::getPrice));
		System.out.println(sum);
		//最大值
		Optional<Book> max = books.stream().collect(Collectors.maxBy(Comparator.comparing(Book::getPrice)));
		System.out.println(max.get());
		//最小值
		Optional<Double> min = books.stream().map(Book::getPrice).collect(Collectors.minBy(Double::compare));
		//分组
		Map<Double, List<Book>> map = books.stream().collect(Collectors.groupingBy(Book::getPrice));
		//分区
		Map<Boolean, List<Book>> collect = books.stream().collect(Collectors.partitioningBy(b -> b.getPrice() > 2500));
		//
		DoubleSummaryStatistics summaryStatistics = books.stream().collect(Collectors.summarizingDouble(Book::getPrice));
		System.out.println(summaryStatistics.getAverage()+summaryStatistics.getCount()+summaryStatistics.getMax());
		//连接
		String name = books.stream().map(Book::getName).collect(Collectors.joining());
		 name = books.stream().map(Book::getName).collect(Collectors.joining(",","[","]"));
		System.out.println(name);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值