This article is intended as an output to review the basics of Java. I hope you can read it in your spare time.
Control statements in Java are roughly divided ・ If statement ・ Ternary operator ・ Stream (although it's a little different ...) ・ Switch statement -Method that returns boolean I will do it at. This time, I will explain the above three.
It is a grammar that generally exists in well-known languages. Of course it also exists in Java.
Control.java
public long secondCheck(List<String> checkTarget) {
long count = 0L;
for(String target : checkTarget) {
if(target.length() > 3) {
count++;
} else if(target.length() > 5) {
count++;
} else {
return count;
}
}
return count;
}
The processing order is as follows. (1) The conditional expression of if is judged first. (2) If the if conditional expression returns false, the else if conditional expression is determined. ③ If 1 and 2 do not match, else is executed.
It is an image that judges in order from the top and performs processing that matches the conditions, and if they do not match, processing of the else block is executed.
It has the same control as the if statement. However, if you use it incorrectly, the code will be very difficult to read. The advantage is that you can write with one liner.
Control.java
public long thridCheck(List<String> checkTarget) {
long count = 0L;
int i = 0 ;
while(i < checkTarget.size()) {
count = checkTarget.get(i).length() > 5 ? count++ : count;
i++;
}
return count;
}
The process is as follows. ?? If the previous conditional expression returns true, the processing on the left is executed, and if false is returned, the processing on the right is executed. In my opinion, it is a description that was contraindicated before the appearance of Stream, I feel like I've gained some citizenship since the introduction of Stream.
It is also possible to further nest conditional expressions when false.
Control.java
public long thridCheck(List<String> checkTarget) {
long count = 0L;
int i = 0 ;
while(i < checkTarget.size()) {
count = checkTarget.get(i).length() > 5 ? count++ : checkTarget.get(i).length() > 3 ? count++ : count;
i++;
}
return count;
}
It's hard to read at this point, so you can complete it with if else, and the code doesn't stretch too much sideways. Recommended to use in some cases.
Stream It is an interface that can be used when performing collection operations added from Java SE 8. The detailed explanation will be in a separate article, but the above code can be written as follows.
Control.java
public long firstCheck(List<String> checkTarget) {
long count = checkTarget.stream().filter(str -> str.length() > 5).count();
return count;
}
In this example, it would be good to borrow the idea of JavaScript and return the result directly.
Control.java
public long firstCheck(List<String> checkTarget) {
return checkTarget.stream().filter(str -> str.length() > 5).count();
}
The loop statement is as follows. ・ For statement ・ Extended for statement ・ Stream ・ While statement ・ Do while statement This also explains the third from the top.
A loop that exists in many languages. In Java ** (Variable declaration, conditional expression, count processing) ** Describe in, and loop the process until the conditional expression returns false.
Control.java
public long secondCheck(List<String> checkTarget) {
long count = 0L;
for(int i = 0 ; i < checkTarget.size() ; i++) {
if(checkTarget.get(i).length() > 3) {
count++;
}
}
return count;
}
The above code loops until the declared variable i is greater than or equal to the number in the checkTarget list.
In other languages, it is called a foreach statement. It processes the elements of the collection in order and loops through all the elements until processing is complete.
Control.java
public long secondCheck(List<String> checkTarget) {
long count = 0L;
for(String target : checkTarget) {
if(target.length() > 3) {
count++;
}
}
return count;
}
The above code will handle all the elements of checkTargetList.
Stream A forEach () method is provided. This also performs the same processing as the extended for statement.
Control.java
public void firstCheck(List<String> checkTarget) {
checkTarget.stream().filter(str -> str.length() > 5).forEach(System.out::print);
}
When I became an engineer, Java 7 was still in its heyday, and there were few people who used Stream, but Stream, which has a short description and is easy to understand, has become mainstream, and I feel the flow of the times.
~~ Still, 8 is still mainstream and old, though ... ~~
Source code https://github.com/mamoru12150927/JavaQitta.git
Recommended Posts