Skip to content

Instantly share code, notes, and snippets.

@solaris33
Last active May 3, 2017 04:11
Show Gist options
  • Save solaris33/0ebd2d0203f9c4347489e40c69d76ef7 to your computer and use it in GitHub Desktop.
Save solaris33/0ebd2d0203f9c4347489e40c69d76ef7 to your computer and use it in GitHub Desktop.
// Traditional Method
public static void main(String[] args) {
List<String> lines = Arrays.asList("apple", "banana", "grape");
List<String> result = getFilterOutput(lines, "grape");
for (String temp : result) {
System.out.println(temp); //output : apple, banana
}
}
private static List<String> getFilterOutput(List<String> lines, String filter) {
List<String> result = new ArrayList<>();
for (String line : lines) {
if (!filter.equals(line)) { // except grape
result.add(line);
}
}
return result;
}
// Using Stream API
public static void main(String[] args) {
List<String> lines = Arrays.asList("apple", "banana", "grape");
List<String> result = lines.stream() // convert list to stream
.filter(line -> !"grape".equals(line)) // except grape
.collect(Collectors.toList()); // collect the output and convert streams to a List
result.forEach(System.out::println); //output : apple, banana
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment