[ad_1]
String output = names.stream()
.sorted((name1, name2) -> {
String[] parts1 = name1.cut up(" ");
String lastName1 = parts1[parts1.length - 1];
String[] parts2 = name2.cut up(" ");
String lastName2 = parts2[parts2.length - 1];
return lastName2.compareTo(lastName1);
})
.map(identify -> {
String[] components = identify.cut up(" ");
return components[parts.length - 1];
})
.filter(lastName -> lastName.size() >= 5)
.scale back("", (accumulator, factor) -> accumulator + factor + ", ");
System.out.println("consequence: " + output);
It will concatenate all of the strings collectively, joined by a comma. (We’ll have a trailing comma that we may drop off the tip.)
scale back
offers us an fascinating have a look at a barely extra superior space of streams. Contemplate in the event you needed to rely 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 factor
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 have a look at the way in which it feels to make use of and compose a number of the most necessary Java useful operations. One other necessary side 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 useful interface to share that operation:
[ad_2]