By the way, there is no basic edition
Java is that Use classes and methods If you can use if statement and for statement, it will be somehow.
For people
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,
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 filter
・ Map
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
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
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