jdk8 收集器

/**
	 * 收集器
	 */
	@Test
	public void test4(){
		List<String> collect = Stream.of("年後", "看懂", "看到").collect(Collectors.toList());
		Set<String> collect2 = Stream.of("年後", "看懂", "看到").collect(Collectors.toSet());
		//返回不可修改的集合 若是修改將拋出異常UnsupportedOperationException
		Set<String> collect3 = Stream.of("年後", "看懂", "看到").collect(Collectors.toUnmodifiableSet());
		List<String> collect4 = Stream.of("年後", "看懂", "看到").collect(Collectors.toUnmodifiableList());

		//使用非接口List,Set類型
		TreeSet<String> collect1 = Stream.of("年後", "看懂", "看到").collect(Collectors.toCollection(TreeSet::new));
		LinkedList<String> collect5 = Stream.of("年後", "看懂", "看到").collect(Collectors.toCollection(LinkedList::new));
		//比較大小獲取最大值
		String s = Stream.of("年後", "看懂", "看到").collect(Collectors.maxBy((x, y) -> x.compareTo(y))).get();
		System.out.println(s);
		String s1 = Stream.of("年後", "看懂", "看到").collect(Collectors.minBy((x, y) -> x.compareTo(y))).get();
		System.out.println(s1);

		//數據統計
		IntSummaryStatistics collect6 = Stream.of("1", "2", "3").map(x -> Integer.valueOf(x)).collect(Collectors.summarizingInt(x -> x.intValue()));
		System.out.println(String.format("sum:%d,avg:%f,max:%d,min:%d,count:%d", collect6.getSum(),collect6.getAverage(),collect6.getMax(),collect6.getMin(),collect6.getCount()));
		//數據分塊
		Map<Boolean, List<Person>> collect7 =  Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).collect(Collectors.partitioningBy(x -> "女人".equals(x.getName())));
		System.out.println(collect7);
		//數據分組
		Map<String, List<Person>> collect8 =  Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).collect(Collectors.groupingBy(Person::getName));
		System.out.println(collect8);
		//拼接字符串
		String collect9 = Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).map(x -> x.getName()).collect(Collectors.joining());
		System.out.println(collect9);
		//添加分割符
		String collect10= Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).map(x -> x.getName()).collect(Collectors.joining(","));
		System.out.println(collect10);
		//添加先後綴
		String collect11= Stream.of(new Man(), new Man(), new Girl(), new Girl(), new Girl()).map(x -> x.getName()).collect(Collectors.joining(",","{","}"));
		System.out.println(collect11);

		List<Chicken> list = new ArrayList<>(5);
		for (int i = 0; i < 5; i++) {
			Chicken chicken = new Chicken();
			chicken.setName(String.valueOf(ThreadLocalRandom.current().nextInt(3)));
			List<Chicken> lt = new LinkedList<>();
			for (int k = 0; k < ThreadLocalRandom.current().nextInt(); k++) {
				lt.add(new Chicken());
			}
			chicken.setDescendants(lt);
			list.add(chicken);
		}
		System.out.println(list);
		//分組並獲取該組一共有多少個相同名稱的雞
		Map<String, Long> collect12 = list.stream().collect(Collectors.groupingBy(Chicken::getName, Collectors.counting()));
		System.out.println(collect12);
		//分組獲取每組子節點最多的對象的option
		Map<String, Optional<Chicken>> collect13 = list.stream().collect(Collectors.groupingBy(Chicken::getName, Collectors.maxBy((x, y) -> Integer.valueOf(x.getDescendants().size()).compareTo(y.getDescendants().size()))));
		System.out.println(collect13);
		//根據上面的option獲取每組子節點最多的對象
		List<Chicken> collect15 = collect13.entrySet().stream().map(x -> x.getValue().get()).collect(Collectors.toList());
		System.out.println(collect15);
		//映射 根據名稱分組並統計全部相同名稱下的全部字節點總數
		Map<String, Integer> collect14 = list.stream().collect(Collectors.groupingBy(Chicken::getName, Collectors.mapping(Chicken::getDescendants, Collectors.summingInt(x -> x.size()))));
		System.out.println(collect14);
	}

看懂
年後
sum:6,avg:2.000000,max:3,min:1,count:3
{false=[com.demo.Man@18a70f16, com.demo.Man@62e136d3], true=[com.demo.Girl@c8e4bb0, com.demo.Girl@6279cee3, com.demo.Girl@4206a205]}
{人=[com.demo.Man@4d826d77, com.demo.Man@61009542], 女人=[com.demo.Girl@77e9807f, com.demo.Girl@448ff1a8, com.demo.Girl@1a38c59b]}
人人女人女人女人
人,人,女人,女人,女人
{人,人,女人,女人,女人}
[Chicken{name='0', descendants=[Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}]}, Chicken{name='2', descendants=[]}, Chicken{name='0', descendants=[Chicken{name='null', descendants=null}]}, Chicken{name='0', descendants=[]}, Chicken{name='0', descendants=[]}]
{0=4, 2=1}
{0=Optional[Chicken{name='0', descendants=[Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}]}], 2=Optional[Chicken{name='2', descendants=[]}]}
[Chicken{name='0', descendants=[Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}, Chicken{name='null', descendants=null}]}, Chicken{name='2', descendants=[]}]
{0=4, 2=0}java

相關文章
相關標籤/搜索