0
点赞
收藏
分享

微信扫一扫

Java Lambda Optional partitioningBy groupingBy joining counting

ITWYY 2022-02-16 阅读 78
java

Java Lambda Optional partitioningBy groupingBy joining counting

		Optional<Apple> oa = appleList.stream().collect(Collectors.maxBy(Comparator.comparing(Apple::getWeight)));
		Apple apple = oa.get();
		System.out.println(apple.getName());
		
		double d = appleList.stream().collect(Collectors.averagingInt(Apple::getWeight));
		System.out.println(d);
		
		Map<Boolean, List<Apple>> sweetMap = appleList.stream().collect(Collectors.partitioningBy(a -> a.isSweet(a.getTaste())));
		List<Apple> sweetList = sweetMap.get(true);
		sweetList.forEach(sweetApple -> System.out.println("sweet apple " + sweetApple.getName()));
		List<Apple> unSweetList = sweetMap.get(false);
		unSweetList.forEach(sweetApple -> System.out.println("unsweet apple " + sweetApple.getName()));
		
		Map<String, List<Apple>> tasteMap = appleList.stream().collect(Collectors.groupingBy(Apple::getTaste));
		
		tasteMap.forEach((key, value) -> {
			System.out.println(key);
			
		});
		
		List<Apple> sList = tasteMap.get("sweet");
		sList.forEach(sl -> System.out.println("sweet apple " + sl.getName()));
		
		List<Apple> sourList = tasteMap.get("sour");
		sourList.forEach(sl -> System.out.println("sour apple " + sl.getName()));
		
		String appleNames = appleList.stream().map(Apple::getName).collect(Collectors.joining(" "));
		System.out.println(appleNames);
		
		Map<String, Long> sMap = appleList.stream().collect(Collectors.groupingBy(Apple::getTaste, Collectors.counting()));
		sMap.forEach((key, value) -> {
			System.out.println(key + " apple " + value);
		});
		
		Map<String, List<Apple>> slMap = appleList.stream().collect(Collectors.groupingBy(Apple::getTaste, Collectors.toList()));
		slMap.forEach((key, value) ->{
			System.out.println(key + " apples " + value.stream().map(Apple::getName).collect(Collectors.joining(" ")));
		});
举报

相关推荐

0 条评论