SQL-like operations with Java using Streams
1. Overview
Nearly every Java application makes and processes collections to group and process the data. To process the data from collections, we write lot of boiler plate code. Typical processing patterns on collections are similar to SQL-like operations such as “finding”, “grouping” etc. Most databases lets to specify such operations declaratively like the below SQL query
SELECT dept_no, MAX(salary) FROM employee GROUP BY DeptID
As you can see, we don’t need to implement how to calculate the maximum salary(for example, using loops and a variable to track the highest value). We only express what we expect. In Java 8 we can do the same way using streams.
2. What is Stream and What is not?
- A Stream is a pipeline of functions that can be evaluated
- Streams can transform data
- A Stream is not a data structure
- Streams cannot mutate data
Most importantly, a stream isn’t a data structure. We can often create a stream from collections to apply a number of functions on a data structure, but a stream itself is not a data structure.
3. Working with Streams
3.1 Obtaining a Stream From a Collection
We can obtain a stream from collection like below
1 2 3 4 5 6 7 |
List<String> items = new ArrayList<String>(); items.add("one"); items.add("two"); items.add("three"); Stream<String> stream = items.stream(); |
First list of items created using Collection API. Then a Stream
of strings is obtained by calling the items.stream()
method.
3.2 Streams filter() and collect()
Before Java 8, filter a List
like this
1 2 3 4 5 6 7 8 9 10 |
List<String> items = Arrays.asList("one", "two", "three"); List<String> result = new ArrayList<>(); for (String item : items) { if (!"three".equals(item)) { // don't want three result.add(item); } } for (String temp : result) { System.out.println(temp); //output : one, two } |
Using Java 8 streams, stream.filter()
to filter a List
, and collect()
to convert a stream into a List
.
1 2 3 4 5 6 7 |
List<String> items = Arrays.asList("one", "two", "three"); List<String> result = items.stream() // obtain a stream from the list .filter(item -> !"three".equals(item)) // don't want three .collect(Collectors.toList()); // collect the output and convert streams to a List result.forEach(System.out::println); //output : one, two |
3.3 Streams map()
Before Java 8
1 2 3 4 5 6 7 8 9 |
List<String> alphaNumbers = Arrays.asList("one", "two", "three", "four"); List<String> alphaNumbersUpperCase = new ArrayList<>(); for (String s : alphaNumbers) { alphaNumbersUpperCase.add(s.toUpperCase()); } System.out.println(alphaNumbers); //[one, two, three, four] System.out.println(alphaNumbersUpperCase); //[ONE, TWO, THREE, FOUR] |
Using Java 8′ stream.map()
1 2 3 4 5 6 7 |
List<String> alphaNumbers = Arrays.asList("one", "two", "three", "four"); List<String> alphaNumbersUpperCase = alphaNumbers.stream() .map(String::toUpperCase) .collect(Collectors.toList()); System.out.println(alphaNumbersUpperCase); //[ONE, TWO, THREE, FOUR] |
3.4 Streams sorted()
Using the same example above, we will sort the alphaNumbers
1 2 3 4 5 6 7 |
List<String> alphaNumbers = Arrays.asList("one", "two", "three", "four"); List<String> alphaNumbersUpperCase = alphaNumbers.stream() .map(String::toUpperCase) .sorted() .collect(Collectors.toList()); System.out.println(alphaNumbersUpperCase); // [FOUR, ONE, THREE, TWO] |
to sort in reverse order
1 2 3 4 5 6 7 |
List<String> alphaNumbers = Arrays.asList("one", "two", "three", "four"); List<String> alphaNumbersUpperCase = alphaNumbers.stream() .map(String::toUpperCase) .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); System.out.println(alphaNumbersUpperCase); // [TWO, THREE, ONE, FOUR] |
4. Conclusion
The Streams API is powerful and simple to understand. It allows us to reduce a huge amount of boilerplate code, create more readable programs and improve productivity when used properly. We should not leave an instantiated streams unconsumed as that will lead to memory leaks. Apply the close()
method or a terminal operation to close the stream.