Bootstrap

Java lambda 方法使用(1)

Java lambda 方法使用(1)

		
		ArrayList<String> list = Lists.newArrayList("1","2","3","4","5");
		//打印元素
		list.stream().forEach(x ->{
			System.out.println(x);
		});
		
		//打印元素
		list.stream().forEach(System.out::println);
		
		//过滤元素
		List<String> collect = list.stream()
				.filter(x -> Integer.valueOf(x) % 2 == 0)
				.collect(Collectors.toList());
		
		//判断元素是否满足预判条件,全部满足
		boolean allMatch = list.stream().allMatch(x -> Integer.valueOf(x) % 2 == 0);
		
		//判断元素是否满足预判条件,只要有一个满足
		boolean anyMatch = list.stream().anyMatch(x -> Integer.valueOf(x) % 2 == 0);
		
		//分组
		Map<Integer, List<String>> collect2 = list.stream()
				.collect(Collectors.groupingBy(x -> Integer.valueOf(x) % 4));
		
		//特殊的,分成两组
		Map<Boolean, List<String>> collect3 = list.stream()
				.collect(Collectors.partitioni
;