Skip to content

Instantly share code, notes, and snippets.

@bobbyjam99-zz
Created July 4, 2014 05:48
Show Gist options
  • Save bobbyjam99-zz/6ddfdc10756c15137552 to your computer and use it in GitHub Desktop.
Save bobbyjam99-zz/6ddfdc10756c15137552 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.Reader;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
public class GrepUtil {
public static Stream<String> grep(Reader reader, String... words) {
AtomicInteger index = new AtomicInteger();
return new BufferedReader(reader).lines()
.map(s -> new GrepFile(index.incrementAndGet(), s))
.filter(o -> Arrays.asList(words).stream().anyMatch(s -> o.getWord().contains(s)))
.map(o -> o.toString());
}
static class GrepFile {
private int index;
private String word;
public GrepFile(int index, String word) {
this.index = index;
this.word = word;
}
public String getWord() {
return word;
}
public String toString() {
return index + ": " + word;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment