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<ABC> list = Arrays.asList(types);
		list.sort((type1, type2) -> {
			return Integer.compare(type1.getSort(), type2.getSort());
		});
		list.forEach(s -> System.out.println(s.getTypeName()));

		Map<String, List<ABC>> 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(","))));
	}

猜你喜欢

转载自tristan-s.iteye.com/blog/2268731