Skip to content

Instantly share code, notes, and snippets.

@knjname
Created May 6, 2013 06:31
Show Gist options
  • Select an option

  • Save knjname/5523635 to your computer and use it in GitHub Desktop.

Select an option

Save knjname/5523635 to your computer and use it in GitHub Desktop.
Add Debugging Pipe
import java.util.Arrays;
import java.util.function.Function;
/**
* Created with IntelliJ IDEA.
* User: owner
* Date: 13/05/06
* Time: 14:41
* To change this template use File | Settings | File Templates.
*/
public class StreamExample {
public static void main(String... args){
Arrays.asList("1", "Foo", "2", "Bar", "3")
.stream()
.map(debug("Before filter: "))
.filter(str -> str.matches("\\d"))
.map(debug("After filter: "))
.forEach(System.out::println);
}
public static <T> Function<T, T> debug(String debugStr){
return (T t) -> {
System.out.println(debugStr + t);
return t;
};
}
}
@knjname
Copy link
Copy Markdown
Author

knjname commented May 6, 2013

Output:

Before filter: 1
After filter: 1
1
Before filter: Foo
Before filter: 2
After filter: 2
2
Before filter: Bar
Before filter: 3
After filter: 3
3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment