Returning to the beginning, getting started with Java ② Control statements, loop statements

Introduction

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 that can be written in various ways

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.

if statement

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.

Ternary operator

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();
    }

This is also a various loop statement

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.

for statement

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.

Extended for statement

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);
        
    }

At the end

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

Returning to the beginning, getting started with Java ② Control statements, loop statements
Going back to the beginning and getting started with Java ① Data types and access modifiers
Getting started with Kotlin to send to Java developers
Getting Started with Doma-Introduction to the Criteria API
Getting Started with Java Collection
Getting Started with Java Basics
Getting started with Java lambda expressions
Getting Started with Ruby for Java Engineers
[Processing × Java] How to use the loop
Getting Started with Java Starting from 0 Part 1
Getting started with the JVM's GC mechanism
Links & memos for getting started with Java (for myself)
[LeJOS] Let's control the EV3 motor with Java
Getting Started with Doma-Using Projection with the Criteira API
Getting Started with Doma-Using Subqueries with the Criteria API
Getting Started with Java 1 Putting together similar things
Returning to the beginning, Java-Kisama mocked Hello World-
Getting Started with Doma-Using Joins with the Criteira API
Getting started with Java programs using Visual Studio Code
Getting Started with DBUnit
Getting Started with Ruby
HTTPS connection with Java to the self-signed certificate server
[LeJOS] Let's remotely control the EV3 motor with Java
Getting Started with Swift
Getting Started with Docker
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Getting Started with Doma-Transactions
Get started with serverless Java with the lightweight framework Micronaut!
[Java] Change the process according to the situation with the Strategy pattern
Getting started with Java and creating an AsciiDoc editor with JavaFX
Getting Started with Doma-Dynamicly construct WHERE clauses with the Criteria API
Getting Started with Reactive Streams and the JDK 9 Flow API
[Java] [Play Framework] Until the project is started with Gradle
Now is the time to get started with the Stream API
Be sure to compare the result of Java compareTo with 0
Getting Started with Doma-Annotation Processing
Java to play with Function
Version control Java with SDKMAN
Java version control with jenv
Connect to DB with Java
Connect to MySQL 8 with Java
Getting Started with JSP & Servlet
Getting Started with Spring Boot
Getting Started with Ruby Modules
Input to the Java console
I want to return to the previous screen with kotlin and java!
Summarize the main points of getting started with JPA learned with Hibernate
Connecting to a database with Java (Part 1) Maybe the basic method
Replace with a value according to the match with a Java regular expression
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
[Java] I want to perform distinct with the key in the object
Java to learn with ramen [Part 1]
How to get started with slim
[java8] To understand the Stream API
Getting Started with Java_Chapter 5_Practice Exercises 5_4
[Java] Points to note with Arrays.asList ()
Dare to challenge Kaggle with Java (1)
I tried to interact with Java
[Google Cloud] Getting Started with Docker
Follow the link with Selenium (Java)
Welcome to the Java Library Swamp! !!