Java application for beginners: stream

By the way, there is no basic edition

Premise

Java is that Use classes and methods If you can use if statement and for statement, it will be somehow.

For people

so?

I haven't changed the writing style so much that the performance will improve (although there is). If you inflate {} like a fox, you see. It's hard to see

Hard to see.java


package dummy;

import java.util.ArrayList;
import java.util.List;

/**
 *Hard-to-see sample
 * 
 * @author me
 *
 */
public class Dummy {

	/**
	 *main function
	 * 
	 * @param args argument
	 */
	public static void main(String[] args) {
		run();
	}

	/**
	 *Hard-to-see processing
	 */
	public static void run() {
		//Declare a List of strings
		List<String> strList = new ArrayList<String>();
		//Put a in a mess
		for (int i = 0; i < 100; i++) {
			strList.add("a");
		}
		//About character strings
		for (String str : strList) {
			if (str.isEmpty()) {
				//Empty string
				System.out.println("I'm not saying yes");
			} else if (str.equals("a")) {
				// a
				System.out.println("It's a");
			} else {
				//Other
				for (int l = 0; l < 100; l++) {
					System.out.println(str);
				}
			}
		}
	}
}

Hmm, it's hard to see Let's clean this up

Smart.java


package dummy;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 *Hard-to-see sample<br>
 * ->Modified
 * <p>
 * 
 * @author me
 * @author
 *
 */
public class Dummy {

	/**
	 *main function
	 * 
	 * @param args argument
	 */
	public static void main(String[] args) {
		run();
	}

	/**
	 *Hard-to-see processing
	 * ->Modification
	 */
	public static void run() {
		//Declare a List of strings
		List<String> strList = new ArrayList<String>();
		//Put a in a mess
		IntStream.range(0, 100).forEach(i -> strList.add("a"));
		//About character strings
		strList.forEach(Dummy::conditions);
	}

	/**
	 *Output changes depending on the input value<br>
	 *Branch condition
	 * <p>
	 * <ul>
	 * <li>Empty string-> "I'm not saying yes"
	 * <li>"a" -> "It's a"
	 * <li>Other->Output the input value as it is
	 * </ul>
	 * @param str Input string
	 */
	public static void conditions(String str) {
		if (str.isEmpty())
			System.out.println("I'm not saying yes");
		else if (str.equals("a"))
			System.out.println("It's a");
		else
			IntStream.range(0, 100).forEach(i -> {System.out.println(str);});
	}
}

What changed ・ For statement that specifies the number of times is put on one line ・ Since it is difficult to see if it is wrapped in a for statement ・ Conditional branching in another method

Well, it doesn't make sense because it's a branch with dead code. If you don't have to connect if statements like this, you only need one condition Is it like this?

Wow!.java


package dummy;

import java.util.Arrays;
import java.util.List;

public class Dummy {

	public static void main(String[] args) {
		run("a");
	}

	/**
	 *For the entered string<br>
	 * <p>
	 * a,b,c,d,e,f,If it matches the letter g<br>
	 *Output with a wahoi added to the rear
	 * 
	 * @param str search condition
	 */
	public static void run(String str) {
		List<String> list = Arrays.asList("a", "b", "c", "d", "e", "f", "g");
		list.stream().filter(v -> v.equals(str)).map(v -> v + "Wow!").forEach(Dummy::echo);
	}

	/**
	 *Processing to output the input value to the console
	 * 
	 * @param obj input value
	 */
	public static void echo(Object obj) {
		System.out.println(obj);
	}
}

The upper one is omitted because it is an obstacle. You don't have to separate the echo function,

What do you want to say

It ’s a deadly stream.

stream for the target source 0 or more intermediate operations + 1 termination operation

It ’s a great guy to do

kwsk

This is it.java


package mk42;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class Stream {

	public static void main(String[] args) {
		run();
	}

	public static void run() {
		// instance Initializer
		List<String> list = new ArrayList<String>() {
			{
				//It's easy to see if you don't dare to break the line
				IntStream.range(0, 100).forEach(i -> {add("a");});
			}
		};

		/**Declaration to use stream for source called list*/
		list.stream();

		/**Intermediate operation(You can do it) */
		//For the element"a"Add(Abbreviation for value, v or variable is attached, anything k)
		list.stream().map(v -> v + "a");
		//Filter the elements and collect only those that meet the conditions
		list.stream().filter(v -> v.equals("a"));
		//Double intermediate operation is also possible
		list.stream().filter(v -> v.equals("a")).map(v -> v + "a");

		/**Termination operation*/
		list.stream().forEach(v -> {
			//Processing in the for statement
		});

		/**Final form*/
		list.stream().filter(v -> v.equals("a")).map(v -> v + "a").forEach(v -> {
			System.out.println(v);
		});
	}
}

Hoisa

In other words.java


list.stream().filter(v -> v.equals("a")).map(v -> v + "a").forEach(v -> {
			System.out.println(v);
		});

Use stream for arrays → collect those whose elements are equals with" a "→ add" a "to the collected elements → sysout them That is. This is one line.

Please go through the types of intermediate operation and termination operation stream official reference

Most of the intermediate operations are basic and convenient ・ If there is only one condition, it will be a substitute for the if statement filterMap of manipulating elements (addition)

For Each is convenient for termination operations.

The instance initializer is completely cool. Please take a good look.

By the way, if you use only forEach without intermediate operation, you can go without stream

Appropriate ★.java


list.forEach( v -> {sysout(v);};

Like

What is v-> v + 1!

I used to call it a lambda expression, I like it because it's easy to understand. Since v is a variable that is attached appropriately, it can be anything. It's an abbreviation for value, but I'm using it because it's cool. stream fun

Finally

Not only is it fun, but it's usually more readable, so This is something other than what you think the for statement is easier to read Let's use stream!

Overall, if the conditional branching is complicated, it is difficult to handle with one line in stream, or it is hard to see. Is it better to go out with the branch condition as a separate method or use the for statement obediently?

I like the name stream Beautiful

that's all

Recommended Posts

Java application for beginners: stream
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Java for beginners, data hiding
[For beginners] How to operate Stream API after Java 8
[For beginners] Summary of java constructor
Rock-paper-scissors game for beginners in Java
Java for beginners, expressions and operators 1
[For beginners] Run Selenium in Java
Java for beginners, expressions and operators 2
Notes for Android application development beginners
[For Java beginners] About exception handling
Classes and instances Java for beginners
A memorandum for android application development beginners
[JAVA] Stream type
For JAVA learning (2018-03-16-01)
Try Java 8 Stream
Learn Java with "So What" [For beginners]
Java 9+ application status
[For beginners] Difference between Java and Kotlin
Java Stream API
2017 IDE for Java
Studying Java 8 (Stream)
Java Stream termination
[Java] Stream processing
Java 9 Optional :: stream
Java No.3 for, stream memory usage useful for business
Kantai Collection Java # 1 Classes and Objects [For Beginners]
A collection of simple questions for Java beginners
[For beginners] About lambda expressions and Stream API
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
[Java] for statement, while statement
[Java] Stream Collectors notes
[Java] Package for management
[Java] for statement / extended for statement
[Java] Stream API-Stream generation
Java8 Stream API practice
NLP for Java (NLP4J) (2)
(Memo) Java for statement
NLP for Java (NLP4J) (1)
Java8 Stream reduction operation
Java8 Stream Rough Summary
Scraping for beginners (Ruby)
[For beginners] Quickly understand the basics of Java 8 Lambda
[For beginners] Minimum sample to display RecyclerView in Java
Java development for beginners to start from 1-Vol.1-eclipse setup
Introduction to Java for beginners Basic knowledge of Java language ①
List of frequently used Java instructions (for beginners and beginners)
[For beginners] Until building a Web application development environment using Java on Mac OS
Java (WebSphere Application Server) memo [1]
[For beginners] Explanation of classes, instances, and statics in Java
Java update for Scala users
[Java] Beginner's understanding of Servlet-②
Web application development environment construction in Java (for inexperienced people)
[Java11] Stream Summary -Advantages of Stream-
Java Stream API cheat sheet
[Java] Precautions for type conversion
Books used for learning Java
[Java] Beginner's understanding of Servlet-①
[For super beginners] DBUnit super introduction