Last active
May 3, 2017 04:11
-
-
Save solaris33/0ebd2d0203f9c4347489e40c69d76ef7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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