In the previous journal entry “Understanding Java 8 Streams with Examples” we have looked at the core concepts of Java 8 Streams API with some examples. Now, in this journal we will take a look at some more examples of Java 8 Stream operations: Intermediate.
Java 8 Stream Intermediate Operation Example
Let’s have a look at some of the basic use cases of intermediate operation methods provided by Stream API.
- filter()
- map()
- sorted()
- flatMap()
filter(): We can use filter() method to test stream elements for a condition and generate filtered list.
Java Stream filter() Example for a List
public class Java8StreamFilter {
public static void main(String[] args) {
List<String> lines = Arrays.asList("java", "stream", "oldjava");
List<String> result = lines.stream() // convert list to stream
.filter(line -> !"oldjava".equals(line)) // filter and remove the oldjava from the list
.collect(Collectors.toList()); // collect the output and convert streams to a List
result.forEach(System.out::println);
}
}
//OUTPUT : java, stream
Java Stream filter() Example with Objects
class Person {
private String name;
private int age;
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String value) {
this.name = value;
}
public int getAge() {
return this.age;
}
public void setAge(int value) {
this.age = value;
}
}
public class Java8StreamFilter {
public static void main(String[] args) {
List<Person> personList = Arrays.asList(
new Person("Smith", 37),
new Person("John", 24),
new Person("Kirsten", 33)
);
Person result = personList.stream() // Convert to steam
.filter(x -> "John".equals(x.getName())) // we want "John" only
.findAny() // If 'findAny' then return found
.orElse(null); // If not found, return null
System.out.println(result);
}
}
// OUTPUT : Person{name="John", age=24}
map(): We can use map() to apply functions to an stream. In the below example we will try to apply lower case function to a list of Strings.
Java Stream map() Example
Stream<String> names = Stream.of("Kirsten", "John", "Philips");
System.out.println(names.map(s -> {
return s.toLowerCase();
}).collect(Collectors.toList()));
//OUTPUT: [kirsten, john, philips]
sorted(): We can use sorted() to sort the stream elements by passing Comparator argument.
Java Stream sorted() Example
Stream<String> reverseSortedName = Stream.of("Kirsten", "John", "Philips");
List<String> reverseSorted = reverseSortedName
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
System.out.println("Reverse Sort: " + reverseSorted);
Stream<String> sortedName = Stream.of("Kirsten", "John", "Philips");
List<String> naturalSorted = sortedName.sorted().collect(Collectors.toList());
System.out.println("Default Natural Sort: " + naturalSorted);
// OUTPUT
Reverse Sort: [Philips, Kirsten, John]
Default Natural Sort: [John, Kirsten, Philips]
flatMap(): Create a stream from the stream of list. Let’s see a simple example to clear this doubt.
Java Stream flatMap() Example
Stream<List<String>> namesList = Stream.of(
Arrays.asList("Smith"),
Arrays.asList("John","Kirsten"),
Arrays.asList("Philips")
);
Stream<String> flatMapStream = namesList
.flatMap(stringList -> stringList.stream());
flatMapStream.forEach(System.out::println);
//OUTPUT
Smith
John
Kirsten
Philips
