Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to use map, filter and reduce in Java

import java.util.stream.IntStream;

public class StreamOperations {
	public static void main(String[] args) {
		int[] values = {3, 10, 6, 1};

		// Sum of values via reduce method
		System.out.printf("Sum: %d%n",
			IntStream.of(values)
					 .reduce(0, (x, y) -> x + y)); // Sum: 20
		// Product via reduce
		System.out.printf("Product: %d%n",
			IntStream.of(values)
					 .reduce(1, (x, y) -> x*y)); // Product: 180
		// Even values multiplied by 10 
		// and displayed in sorted order
		// Printing: 60 100
		IntStream.of(values)
				 .filter(value -> value % 2 == 0)
				 .map(value -> value * 10)
				 .sorted()
				 .forEach(value -> System.out.printf("%d ", value));	
	}
}
Comment

java stream filter map reduce example

String[] myArray = { "this", "is", "a", "sentence" };
String result = Arrays.stream(myArray)
                .reduce("", (a,b) -> a + b);
Comment

PREVIOUS NEXT
Code Example
Java :: arraylist vs vector in java 
Java :: write message in file java 
Java :: print anything in java 
Java :: java stream sort 
Java :: priority queue java comparator 
Java :: java parse json to class 
Java :: spring convert page to list 
Java :: java home 
Java :: java how to know if there is something on arguments 
Java :: how to change toolbar name in android studio 
Java :: java creating a stack 
Java :: can i call another function from main hava 
Java :: float to int in java 
Java :: Java How to use Queue? 
Java :: java map tostring 
Java :: jframe maximized at startup 
Java :: java return new instance of generic type 
Java :: hide background of button java swing 
Java :: could not find java in a java_home at /opt/java/openjdk/bin/java docker sonarqube 
Java :: spring boot put invalid cors request 
Java :: Exception in thread "main" java.lang.NoClassDefFoundError sdkmanager 
Java :: java stream code to ignore null 
Java :: what are variables in java 
Java :: flutter webview plugin background transparent 
Java :: java run class file 
Java :: . Java Program to Delete the Specified Integer from an Array 
Java :: 7877777777777777777777777 
Java :: HashMap to store key and value pair. 
Java :: marshalling in java 
Java :: java create array with values 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =