It should be noted that Java 8 Stream is very convenient, but it only provides a "flow". Among them, ʻall Match` is very dangerous! On the contrary, side effects occur if not used with caution. This is because allMatch performs short-circuit evaluation (if there is something that does not meet the judgment conditions, the subsequent processing is not continued and the processing ends there).
Create a class that holds a character string, determine whether the string length is all 5 characters or less for the List that holds it, and if data of 6 characters or more is included, that character A program that substitutes the string sorry
instead of a column. Tested with the following four processing methods.
--A. Execute allMatch directly on List --B. Map and then execute allMatch in Stream processing --C: Map and save to List once, then open a new Stream and execute allMatch --D: Filter the processing result and compare the number with the list length of the target that is streaming Stream
Sample code. I think it's easy.
MyProject.java
public class MyProject {
public static void main(String[] args) {
List<String> baseStrList = Arrays.asList("test", "tetetete", "tetetete", "tetetete", "tetetete");
List<TestData> listA = baseStrList.stream().map(TestData::new).collect(Collectors.toList());
List<TestData> listB = baseStrList.stream().map(TestData::new).collect(Collectors.toList());
List<TestData> listC = baseStrList.stream().map(TestData::new).collect(Collectors.toList());
List<TestData> listD = baseStrList.stream().map(TestData::new).collect(Collectors.toList());
boolean a = listA.stream().allMatch(TestData::checkLength);
boolean b = listB.stream().map(TestData::checkLength).allMatch(e -> e);
List<Boolean> list3Temp = listC.stream().map(TestData::checkLength).collect(Collectors.toList());
boolean c = list3Temp.stream().allMatch(e -> e);
boolean d = listD.size() == listD.stream().filter(TestData::checkLength).count();
System.out.println("a:" + a + ", list:" + listA.toString());
System.out.println("b:" + b + ", list:" + listB.toString());
System.out.println("c:" + c + ", list:" + listC.toString());
System.out.println("d:" + d + ", list:" + listD.toString());
}
public static class TestData {
public String content = "fefe";
@Override
public String toString() {
return content;
}
public TestData(String a) {
this.content = a;
}
public boolean checkLength() {
if (getContent().length() > 5) {
setContent("sorry");
return false;
}
return true;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}
result
a:false, list:[test, sorry, tetetete, tetetete, tetetete]
b:false, list:[test, sorry, tetetete, tetetete, tetetete]
c:false, list:[test, sorry, sorry, sorry, sorry]
d:false, list:[test, sorry, sorry, sorry, sorry]
--A: Due to the short-circuit evaluation of allMatch, sorry is assigned only to the second element, and the elements after that are left unprocessed. --B: Even if you put map () of Stream API, it is in the hands of allMatch (), so unfortunately the result is the same as A. --C: Since a new List is created once, it is inefficient, but all elements are processed correctly. --D: This should be more efficient than c because it doesn't generate a new List. .. ..
From the above, we can see that we need to understand how to use allMatch correctly.
Personally, when using allMatch, I think that it is only when you want to check something against List. If the data is destroyed like this time, or if you perform some special processing and aggregate the results, it seems better to use the C or D method.
[2017/10/23 09:40 postscript] Also, of course, allMatch for empty list is true, so be careful. The same result can be obtained with the D method.
If you use lombok, it will be easier, so let's use it.
MyProject.java
import lombok.Data;
import lombok.val;
import java.util.Arrays;
import java.util.stream.Collectors;
public class MyProject {
public static void main(String[] args) {
val baseStrList = Arrays.asList("test", "tetetete", "tetetete", "tetetete", "tetetete");
val listA = baseStrList.stream().map(TestData::new).collect(Collectors.toList());
val listB = baseStrList.stream().map(TestData::new).collect(Collectors.toList());
val listC = baseStrList.stream().map(TestData::new).collect(Collectors.toList());
val listD = baseStrList.stream().map(TestData::new).collect(Collectors.toList());
val a = listA.stream().allMatch(TestData::checkLength);
val b = listB.stream().map(TestData::checkLength).allMatch(e -> e);
val list3Temp = listC.stream().map(TestData::checkLength).collect(Collectors.toList());
val c = list3Temp.stream().allMatch(e -> e);
val d = listD.size() == listD.stream().filter(TestData::checkLength).count();
System.out.println("a:" + a + ", list:" + listA.toString());
System.out.println("b:" + b + ", list:" + listB.toString());
System.out.println("c:" + c + ", list:" + listC.toString());
System.out.println("d:" + d + ", list:" + listD.toString());
}
@Data
public static class TestData {
public String content = "fefe";
@Override
public String toString() {
return content;
}
public TestData(String a) {
this.content = a;
}
public boolean checkLength() {
if (getContent().length() > 5) {
setContent("sorry");
return false;
}
return true;
}
}
}
Recommended Posts