JDK8 stream groupBY

阅读更多
JDK8的stream中的groupBy 还是有点作用的, 其他的只是简洁代码


	
@Test
	public void group() {
		ABC[] types = new ABC[] { ABC.KEY, ABC.AUCTION,
		        ABC.FASTSELLER, ABC.INFORMATION,
		        ABC.FASTSELLER };
		List list = Arrays.asList(types);
		list.sort((type1, type2) -> {
			return Integer.compare(type1.getSort(), type2.getSort());
		});
		list.forEach(s -> System.out.println(s.getTypeName()));

		Map> group = list.stream().collect(
		        Collectors.groupingBy(ABC::getTypeName));

		group.forEach((name, g) -> System.out.format("name %s has %s\n", name, g.stream().map(t -> t.getTypeName())
		        .collect(Collectors.joining(","))));
	}

你可能感兴趣的:(JDK8 stream groupBY)