I want to ForEach an array with a Lambda expression in Java

When using the for statement, I wondered what would happen if I wrote it with a lambda expression, so I will put it as a memorandum.

The behavior in the for statement

  1. Function call
  2. Conditionally call a function
  3. Process the value and call the function is.

For statement test 1



	public static void main(String[] args){
		int[] foos = {1, 2, 3, 4, 5};
		
		//Ordinary For statement
		for(int i = 0; i < foos.length; i++){
			function(foos[i]);
		}
		
		//Extended For statement
		for(int foo : foos){
			function(foo);
		}
		
		//lambda expression
		Arrays.stream(foos).forEach(foo -> function(foo));

		//With lambda expression type inference * Test.function(int)Is calling
		Arrays.stream(foos).forEach(Test::function);
	}

For statement test 2



	public static void main(String[] args){
		int[] foos = {1, 2, 3, 4, 5};
		
		//Ordinary For statement
		for(int i = 0; i < foos.length; i++){
			if(condition(foos[i])){
				function(foos[i]);
			}
		}
		
		//Extended For statement
		for(int foo : foos){
			if(condition(foo)){
				function(foo);
			}
		}
		
		//lambda expression naive way of writing
		Arrays.stream(foos).forEach(foo -> {
			if(condition(foo)){
				function(foo);
			}
		});
		
		//lambda expression more functional
		Arrays.stream(foos)
			.filter(foo -> condition(foo))
			.forEach(foo -> function(foo));

		//With lambda expression type inference
		Arrays.stream(foos)
			.filter(Test::condition)
			.forEach(Test::function);
	}

For statement test 3



	//Ordinary For statement
	for(int i = 0; i < foos.length; i++){
		int tmp = offset(foos[i]);
		function(tmp);
	}

	//Extended For statement
	for(int foo : foos){
		int tmp = offset(foo);
		function(tmp);
	}

	//lambda expression
	Arrays.stream(foos)
		.map(Test::offset)
		.forEach(Test::function);

Recommended Posts

I want to ForEach an array with a Lambda expression in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to send an email in Java.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
[Java] I want to convert a byte array to a hexadecimal number
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I want to return an object in CSV format with multi-line header & filter in Java
I want to convert an array to Active Record Relation with Rails
Cast an array of Strings to a List of Integers in Java
[Java] I want to perform distinct with the key in the object
I want to extract between character strings with a regular expression
I want to write a loop that references an index with Java 8's Stream API
[Java] How to turn a two-dimensional array with an extended for statement
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
rsync4j --I want to touch rsync in Java.
How to use Java API with lambda expression
I want to display a PDF in Chinese (Korean) with thin reports
Java8 to start now ~ forEach and lambda expression ~
I wrote a Lambda function in Java and deployed it with SAM
I tried to break a block with java (1)
I want to do something like "cls" in Java
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to use a little icon in Rails
I tried to create a Clova skill in Java
I want to monitor a specific file with WatchService
I tried to make a login function in Java
How to output standard from an array with forEach
Quickly implement a singleton with an enum in Java
I want to define a function in Rails Console
I want to transition screens with kotlin and java!
How to convert a file to a byte array in Java
I want to click a GoogleMap pin in RSpec
I want to get along with Map [Java beginner]
[Ruby] I want to make an array from a character string with the split method. And vice versa.
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
Pass the condition to be used in the Java8 lambda expression filter () as a parameter
Assign a Java8 lambda expression to a variable and reuse it
I tried to modernize a Java EE application with OpenShift.
I want to find a relative path in a situation using Path
I want to build Java Applet without using an IDE
I want to push an app made with Rails 6 to GitHub
Convert a Java byte array to a string in hexadecimal notation
How to change a string in an array to a number in Ruby
I just wanted to make a Reactive Property in Java
Create a SlackBot with AWS lambda & API Gateway in Java
I tried to convert a string to a LocalDate type in Java
I want to create a Parquet file even in Ruby
I want to manually send an authorization email with Devise
I tried to make a client of RESAS-API in Java
I want to implement various functions with kotlin and java!
[Java] I want to test standard input & standard output with JUnit
I want to simplify the conditional if-else statement in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (gray magic that is not so much as black magic)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Royal road edition that is neither magic nor anything)
I sent an email in Java
I created a PDF in Java.
After posting an article with Rails Simple Calendar, I want to reflect it in the calendar.