Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

11.5. peek 打印调试信息

		
package cn.netkiller.test;

import lombok.Data;

import java.io.IOException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
public class Test {
    public static void main(String[] args) throws IOException {
        Stream.of("one", "two", "three", "four").filter(e -> e.length() > 3)
                .peek(e -> System.out.println("Before value: " + e))
                .map(String::toUpperCase)
                .peek(e -> System.out.println("After value: " + e))
                .collect(Collectors.toList());
    }
}
	
		
		
		
	List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
	numbers.stream()
       .peek(n -> System.out.println("原始元素:" + n))
       .filter(n -> n % 2 == 0)
       .peek(n -> System.out.println("过滤后元素:" + n))
       .collect(Collectors.toList());