Java 9 will add three features to Stream.
・ TakeWhile Targets data while satisfying the conditions ・ Data that meets the dropWhile condition is excluded. ・ OfNullable Allow null
But I don't think I see anyone (or source) who uses Stream at work before the new feature. (I don't have many opportunities to use new things at work)
If you don't touch new things, they will be left behind, so I took a quick look at what they were like.
I think I will probably use it at such times.
・ Basically, you will be able to write with less description than before, so the source will be refreshed. -Since the return value of various APIs is Stream, it cannot be used without knowing it. -I want to run parallel processing using parallel (). (Added on 2017/06/01) -Create a different list using the contents of the list. (Added on 2017/06/02)
Example 1: Loop the list and pass it to the method
List<String> list = Arrays.asList("Part 1", "Part 2", "Part 3", "Part 4", "Part 5");
//Passing the string in the list to the fuga method of the Hoge class
list.stream().forEach(Hoge::fuga);
Example 2: Create an integer list from a string list
List<String> sl= Arrays.asList("11", "12", "13", "14", "15");
List<Integer> il = sl.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());
Example 3: After getting all the csv paths under "C: \ temp", get the number of files starting with "A"
Path target = Paths.get("C:\temp");
try(
Stream<Path> paths = Files.find(target , Integer.MAX_VALUE, (p, a) -> p.getFileName().endsWith(".csv"));
){
//Try counting only CSV that starts with "A"
long c = paths.filter(p -> p.getFileName().startsWith("A")).count();
}
The "->" that appears here and there is a "lambda expression", isn't it?
The streamAPI usually has a Consumer argument that allows you to pass method references and lambda expressions.
Well, when "->" appears, the left side is the argument, and the right side is the content of the method.
The method reference is written like "Hoge :: fuga". If you just read it, you can think that the parentheses of "Hoge.fuga (str)" are omitted.
Let's write how much the source shrinks by using Stream.
■ When using Stream
paths.filter(p -> p.getFileName().startsWith("A"))
■ When not using Stream
/**Filter part*/
private List<Path> filter(List<Path> paths) {
List result = new ArrayList<>();
for (Path p : paths) {
if (isStartA(p)) {
result.add(p);
}
}
return result;
}
/**Lambda type part*/
private boolean isStartA(Path p) {
return p.getFileName().startsWith("A");
}
You can see how the description is reduced by using Stream and lambda expressions!
Looking at the above results, it seems that it is better to use Stream or lambda expression more bang bang, When it comes to actually using it at work, if you do not say "for / while prohibited", the source will be messed up. It's a source that doesn't make sense to those who don't know. (People who don't understand this feel bad)
Since it has been updated conveniently, I would like to update humans as well!
Recommended Posts