
String output = names.stream()
.sorted((name1, name2) -> {
String[] parts1 = name1.break up(" ");
String lastName1 = parts1[parts1.length - 1];
String[] parts2 = name2.break up(" ");
String lastName2 = parts2[parts2.length - 1];
return lastName2.compareTo(lastName1);
})
.map(identify -> {
String[] components = identify.break up(" ");
return components[parts.length - 1];
})
.filter(lastName -> lastName.size() >= 5)
.scale back("", (accumulator, component) -> accumulator + component + ", ");
System.out.println("outcome: " + output);
This may concatenate all of the strings collectively, joined by a comma. (We’ll have a trailing comma that we may drop off the top.)
scale back offers us an fascinating take a look at a barely extra superior space of streams. Contemplate for those who needed to depend the string characters and return an integer worth. How may you try this? The return worth of scale back would need to be an integer, however the accumulator and component args are strings. I’ll go away that for an train, with this Stack Overflow query as one lead, and my introduction to Java stream gatherers as one other.
Reusing operations
We’ve had a reasonably good take a look at the way in which it feels to make use of and compose a few of the most vital Java practical operations. One other vital aspect is code reuse. Say you wanted to make use of that fancy string sorter in a number of locations. In Java, we may create a practical interface to share that operation:


