Its All Binary
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
FRAME | NO FRAME | TREE

Package java.util.stream

Class java.util.stream.Stream

Examples for usage of all methods of 'java.util.stream.Stream' with console output of example code.

Methods

Please click on method from below list to go to code example for usage of that method. Click [↓ Imports] to get import statements used in examples. To read javadoc of methods, click on [⿺ Javadoc] for that method.

Method Examples

public abstract boolean allMatch(java.util.function.Predicate<? super T> predicate)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		/*
		 * All not matching predicate
		 */
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Samy");
		System.out.println("list values = " + list);

		boolean allStartsWithR = list.stream().allMatch(n -> n.startsWith("R"));
		System.out.println("allStartsWithR = " + allStartsWithR);

		/*
		 * All matching predicate
		 */
		ArrayList<String> list_1 = new ArrayList<>();
		list_1.add("Ravi");
		list_1.add("Robert");
		list_1.add("Richard");
		System.out.println("list_1 values = " + list_1);

		boolean list_1_allStartsWithR = list_1.stream().allMatch(n -> n.startsWith("R"));
		System.out.println("list_1_allStartsWithR = " + list_1_allStartsWithR);

		/*
		 * Empty stream. Since stream empty, will return true without executing
		 * predicate.
		 */
		ArrayList<String> emptyList = new ArrayList<>();

		boolean emptyList_allStartsWithR = emptyList.stream().allMatch(n -> n.startsWith("R"));
		System.out.println("emptyList_allStartsWithR = " + emptyList_allStartsWithR);
	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Samy]
allStartsWithR = false
list_1 values = [Ravi, Robert, Richard]
list_1_allStartsWithR = true
emptyList_allStartsWithR = true

Tag: Example for allMatch method of class java.util.stream.Stream., Stream allMatch function example with arguments java.util.function.Predicate<? super T> predicate, How to use allMatch method of Stream?, Usage of Stream.allMatch, Stream.allMatch() examples

public abstract boolean anyMatch(java.util.function.Predicate<? super T> predicate)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Samy");
		System.out.println("list values = " + list);

		// Few match predicate
		boolean anyStartsWithR = list.stream().anyMatch(n -> n.startsWith("R"));
		System.out.println("anyStartsWithR = " + anyStartsWithR);

		// No one match predicate
		boolean anyStartsWithW = list.stream().anyMatch(n -> n.startsWith("W"));
		System.out.println("anyStartsWithW = " + anyStartsWithW);

		// Empty stream. Since stream empty, will return false without executing
		// predicate.
		ArrayList<String> emptyList = new ArrayList<>();
		boolean emptyList_anyStartsWithR = emptyList.stream().anyMatch(n -> n.startsWith("R"));
		System.out.println("emptyList_anyStartsWithR = " + emptyList_anyStartsWithR);
	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Samy]
anyStartsWithR = true
anyStartsWithW = false
emptyList_anyStartsWithR = false

Tag: Example for anyMatch method of class java.util.stream.Stream., Stream anyMatch function example with arguments java.util.function.Predicate<? super T> predicate, How to use anyMatch method of Stream?, Usage of Stream.anyMatch, Stream.anyMatch() examples

public static java.util.stream.Stream$Builder<T> builder()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		// Stream build add with chained method.
		Stream.builder().add("Tom").add("Richard").add("Robert").build()
				.forEach(s -> System.out.println("Added value = " + s));

		// Stream builder accept returns void so can't chain.
		Stream.Builder<String> builder = Stream.builder();
		builder.accept("Tom");
		builder.accept("Ravi");
		builder.build().forEach(s -> System.out.println("Accepted value = " + s));

	
					
Output:
Added value = Tom
Added value = Richard
Added value = Robert
Accepted value = Tom
Accepted value = Ravi

Tag: Example for builder method of class java.util.stream.Stream., Stream builder function example , How to use builder method of Stream?, Usage of Stream.builder, Stream.builder() examples, Java 8 build stream directly, Java create stream without any collection.

public abstract void close()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Samy");
		System.out.println("list values = " + list);

		list.stream().onClose(() -> System.out.println("Performing close handler"))
				.onClose(() -> System.out.println("Performing another close handler"))
				.map(s -> "Doing some stream tasks here " + s).close();

	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Samy]
Performing close handler
Performing another close handler

Tag: Example for close method of class java.util.stream.BaseStream., BaseStream close function example , How to use close method of BaseStream?, Usage of BaseStream.close, BaseStream.close() examples

public abstract R collect(java.util.stream.Collector<? super T, A, R> collector)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...

See these examples also: java.util.stream.Collectors

Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Samy");
		System.out.println("list values = " + list);

		// Collectors filtered list.
		List<String> namesStartingWithR = list.stream().filter(s -> s.startsWith("R")).collect(Collectors.toList());
		System.out.println("namesStartingWithR = " + namesStartingWithR);

		// Collectors filter & join comma separated.
		String namesStartingWithRJoint = list.stream().filter(s -> s.startsWith("R")).collect(Collectors.joining(","));
		System.out.println("namesStartingWithRJoint = " + namesStartingWithRJoint);

		// Collectors group by first character
		Map<Object, List<String>> namesGroupByFirstChar = list.stream()
				.collect(Collectors.groupingBy(k -> k.charAt(0)));
		System.out.println("namesGroupByFirstChar = " + namesGroupByFirstChar);

	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Samy]
namesStartingWithR = [Ravi, Richard]
namesStartingWithRJoint = Ravi,Richard
namesGroupByFirstChar = {A=[Amy], R=[Ravi, Richard], S=[Samy], T=[Tom]}

Tag: Example for collect method of class java.util.stream.Stream., Stream collect function example with arguments java.util.stream.Collector<? super T, A, R> collector, How to use collect method of Stream?, Usage of Stream.collect, Stream.collect() examples

public abstract R collect(java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R, ? super T> accumulator, java.util.function.BiConsumer<R, R> combiner)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		ArrayList<Integer> list = new ArrayList<>();
		list.add(2);
		list.add(3);
		list.add(5);
		list.add(4);
		list.add(8);
		System.out.println("list values = " + list);

		/*
		 * Get integers from above list & do i1^2 + i2^2 + i3^2 ...... + in^2
		 * 
		 * 2^2 + 3^2 + 5^2 + 4^2 + 8^2 = 118
		 */
		class EquationResult {
			public long result;
		}
		// This will first time initialize holder of result i.e. EquationResult
		Supplier<EquationResult> resultInitializer = EquationResult::new;
		// For each entry in list, this consumer will be called. So do the square & add
		// to result.
		BiConsumer<EquationResult, Integer> consumeOneIntegerInResult = (er, i) -> er.result += (i * i);
		// In case of parallel execution of stream, this helps merging two results into
		// one.
		BiConsumer<EquationResult, EquationResult> resultMerger = (er1, er2) -> er1.result = er1.result + er2.result;
		EquationResult result = list.stream().collect(resultInitializer, consumeOneIntegerInResult, resultMerger);
		System.out.println("Result = " + result.result);
		// All lambdas in single line
		EquationResult singleLineResult = list.stream().collect(EquationResult::new, (er, i) -> er.result += (i * i),
				(er1, er2) -> er1.result = er1.result + er2.result);
		System.out.println("singleLineResult = " + singleLineResult.result);

		/*
		 * Average of all numbers in list using stream lambda and collect. Refer this
		 * for formula for incremental average
		 * https://ubuntuincident.wordpress.com/2012/04/25/calculating-the-average-
		 * incrementally/
		 */
		class AverageResult {
			public double average;
			public int countOfElements;
		}
		AverageResult average = list.stream().collect(AverageResult::new,
				(er, n) -> er.average += ((n - er.average) / ++er.countOfElements),
				(er1, er2) -> er1.average = (er1.average + er2.average) / 2);
		System.out.println("average = " + average.average);

		/*
		 * Average from stream of Strings.
		 */
		ArrayList<String> listOfNums = new ArrayList<>();
		listOfNums.add("2");
		listOfNums.add("3");
		listOfNums.add("5");
		listOfNums.add("4");
		listOfNums.add("8");

		AverageResult averageOfNums = listOfNums.stream().mapToDouble(s -> Double.parseDouble(s)).collect(
				AverageResult::new, (er, n) -> er.average += ((n - er.average) / ++er.countOfElements),
				(er1, er2) -> er1.average = (er1.average + er2.average) / 2);
		System.out.println("averageOfNums = " + averageOfNums.average);

	
					
Output:
list values = [2, 3, 5, 4, 8]
Result = 118
singleLineResult = 118
average = 4.4
averageOfNums = 4.4

Tag: Example for collect method of class java.util.stream.Stream., Stream collect function example with arguments java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R, ? super T> accumulator, java.util.function.BiConsumer<R, R> combiner, How to use collect method of Stream?, Usage of Stream.collect, Stream.collect() examples

public static java.util.stream.Stream<T> concat(java.util.stream.Stream<? extends T> a, java.util.stream.Stream<? extends T> b)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list_1 = new ArrayList<>();
		list_1.add("Tom");
		list_1.add("Ravi");
		list_1.add("Amy");
		System.out.println("list_1 values = " + list_1);

		ArrayList<String> list_2 = new ArrayList<>();
		list_2.add("Richard");
		list_2.add("Samy");
		System.out.println("list_2 values = " + list_2);

		// Since list is ordered, order of both elements are maintained.
		String allValuesFrom1And2 = Stream.concat(list_1.stream(), list_2.stream()).collect(Collectors.joining(","));
		System.out.println("allValuesFrom1And2 = " + allValuesFrom1And2);

		String allValuesFrom2And1 = Stream.concat(list_2.stream(), list_1.stream()).collect(Collectors.joining(","));
		System.out.println("allValuesFrom2And1 = " + allValuesFrom2And1);
	
					
Output:
list_1 values = [Tom, Ravi, Amy]
list_2 values = [Richard, Samy]
allValuesFrom1And2 = Tom,Ravi,Amy,Richard,Samy
allValuesFrom2And1 = Richard,Samy,Tom,Ravi,Amy

Tag: Example for concat method of class java.util.stream.Stream., Stream concat function example with arguments java.util.stream.Stream<? extends T> a, java.util.stream.Stream<? extends T> b, How to use concat method of Stream?, Usage of Stream.concat, Stream.concat() examples, How to merge two streams., How to merge java8 streams., How to concat to lists or sets using stream concat.

public abstract long count()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Don");
		System.out.println("list values = " + list);

		long countOfNamesWithR = list.stream().filter(s -> s.startsWith("R")).count();
		System.out.println("countOfNamesWithR = " + countOfNamesWithR);

		long countOf3CharNames = list.stream().filter(s -> s.length() == 3).count();
		System.out.println("countOf3CharNames = " + countOf3CharNames);
	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Don]
countOfNamesWithR = 2
countOf3CharNames = 3

Tag: Example for count method of class java.util.stream.Stream., Stream count function example , How to use count method of Stream?, Usage of Stream.count, Stream.count() examples

public abstract java.util.stream.Stream<T> distinct()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Tom");
		list.add("Richard");
		list.add("Ravi");
		System.out.println("list values = " + list);

		List<String> distinctList = list.stream().distinct().collect(Collectors.toList());
		System.out.println("distinctList value = " + distinctList);
	
					
Output:
list values = [Tom, Ravi, Amy, Tom, Richard, Ravi]
distinctList value = [Tom, Ravi, Amy, Richard]

Tag: Example for distinct method of class java.util.stream.Stream., Stream distinct function example , How to use distinct method of Stream?, Usage of Stream.distinct, Stream.distinct() examples, How to get distinct unique values from ArrayList or list.

public java.util.stream.Stream<T> dropWhile(java.util.function.Predicate<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		String[] sortedNames = { "Amy", "Bob", "Chris", "Don", "Ravi", "Robert", "Zion" };

		String namesDtoZ = Arrays.stream(sortedNames).dropWhile(n -> n.charAt(0) <= 'C')
				.collect(Collectors.joining(","));

		System.out.println("Names D to Z = " + namesDtoZ);
	
					
Output:
Names D to Z = Don,Ravi,Robert,Zion

Tag: Example for dropWhile method of class java.util.stream.Stream., Stream dropWhile function example with arguments java.util.function.Predicate<? super T> arg0, How to use dropWhile method of Stream?, Usage of Stream.dropWhile, Stream.dropWhile() examples

public static java.util.stream.Stream<T> empty()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		List<Object> emptyList = Stream.empty().collect(Collectors.toList());

		System.out.println("emptyList value = " + emptyList + " Size " + emptyList.size());

	
					
Output:
emptyList value = [] Size 0

Tag: Example for empty method of class java.util.stream.Stream., Stream empty function example , How to use empty method of Stream?, Usage of Stream.empty, Stream.empty() examples

public abstract java.util.stream.Stream<T> filter(java.util.function.Predicate<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Tom");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		// Filter names starting with R
		List<String> namesWithRList = list.stream().filter(s -> s.startsWith("R")).collect(Collectors.toList());
		System.out.println("Names starting with R = " + namesWithRList);

		// Multiple filters i.e. starting with R & ending with I
		List<String> namesWithRAndIList = list.stream().filter(s -> s.startsWith("R")).filter(s -> s.endsWith("i"))
				.collect(Collectors.toList());
		System.out.println("Names starting with R & ending with I = " + namesWithRAndIList);

		// Filter empty list. Returns empty list in result
		List<String> namesWithRListFromEmpty = Collections.<String>emptyList().stream().filter(s -> s.startsWith("R"))
				.collect(Collectors.toList());
		System.out.println("Names starting with R from empty = " + namesWithRListFromEmpty);
	
					
Output:
list values = [Tom, Ravi, Amy, Tom, Richard, Remi]
Names starting with R = [Ravi, Richard, Remi]
Names starting with R & ending with I = [Ravi, Remi]
Names starting with R from empty = []

Tag: Example for filter method of class java.util.stream.Stream., Stream filter function example with arguments java.util.function.Predicate<? super T> arg0, How to use filter method of Stream?, Usage of Stream.filter, Stream.filter() examples

public abstract java.util.Optional<T> findAny()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		Set<String> list = new HashSet<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Tom");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		// Find any
		Optional<String> any = list.stream().findAny();
		System.out.println("Found any = " + any.get());

		// Again find any. This may or may not return same result as above. Not
		// predictable, non deterministic.
		Optional<String> any1 = list.stream().findAny();
		System.out.println("Found any1 = " + any1.get());

		// Find any from empty list
		Optional<String> anyFromEmpty = Collections.<String>emptyList().stream().findAny();
		System.out.println("Found any? = " + anyFromEmpty.isPresent() + " Is empty? = " + anyFromEmpty.isEmpty());

	
					
Output:
list values = [Tom, Ravi, Richard, Remi, Amy]
Found any = Tom
Found any1 = Tom
Found any? = false Is empty? = true

Tag: Example for findAny method of class java.util.stream.Stream., Stream findAny function example , How to use findAny method of Stream?, Usage of Stream.findAny, Stream.findAny() examples

public abstract java.util.Optional<T> findFirst()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Tom");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		// Find first
		Optional<String> first = list.stream().findFirst();
		System.out.println("Found first = " + first.get());

		// Sort & then find first
		Optional<String> sortedFirst = list.stream().sorted().findFirst();
		System.out.println("Found first after sorting = " + sortedFirst.get());

		// Find first from empty list
		Optional<String> anyFromEmpty = Collections.<String>emptyList().stream().findFirst();
		System.out.println("Found any? = " + anyFromEmpty.isPresent() + " Is empty? = " + anyFromEmpty.isEmpty());

	
					
Output:
list values = [Tom, Ravi, Amy, Tom, Richard, Remi]
Found first = Tom
Found first after sorting = Amy
Found any? = false Is empty? = true

Tag: Example for findFirst method of class java.util.stream.Stream., Stream findFirst function example , How to use findFirst method of Stream?, Usage of Stream.findFirst, Stream.findFirst() examples

public abstract java.util.stream.Stream<R> flatMap(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		// Separate lists for boys & girls names
		List<String> boys = new ArrayList<>();
		boys.add("Tom");
		boys.add("Ravi");
		boys.add("Remi");
		List<String> girls = new ArrayList<>();
		girls.add("Angela");
		girls.add("Rita");
		girls.add("Amy");

		// List of lists
		List<List<String>> all = new ArrayList<>();
		all.add(boys);
		all.add(girls);
		System.out.println("list values boys = " + boys + " girls = " + girls + " all = " + all);

		// Flat map to merge streams of both
		List<String> allList = all.stream().flatMap(l -> l.stream()).collect(Collectors.toList());
		System.out.println("allList = " + allList);

		// Flat map to merge streams of both & then filter names with R
		List<String> allStartingWithR = all.stream().flatMap(l -> l.stream()).filter(s -> s.startsWith("R"))
				.collect(Collectors.toList());
		System.out.println("allStartingWithR = " + allStartingWithR);

	
					
Output:
list values boys = [Tom, Ravi, Remi] girls = [Angela, Rita, Amy] all = [[Tom, Ravi, Remi], [Angela, Rita, Amy]]
allList = [Tom, Ravi, Remi, Angela, Rita, Amy]
allStartingWithR = [Ravi, Remi, Rita]

Tag: Example for flatMap method of class java.util.stream.Stream., Stream flatMap function example with arguments java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>> arg0, How to use flatMap method of Stream?, Usage of Stream.flatMap, Stream.flatMap() examples

public abstract java.util.stream.DoubleStream flatMapToDouble(java.util.function.Function<? super T, ? extends java.util.stream.DoubleStream> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		// Separate lists for different numbers
		List<Double> oneZeros = new ArrayList<>();
		oneZeros.add(100d);
		oneZeros.add(1000000d);
		oneZeros.add(10000d);
		List<Double> nines = new ArrayList<>();
		nines.add(9999d);
		nines.add(9999999d);
		nines.add(99d);

		// List of lists
		List<List<Double>> all = new ArrayList<>();
		all.add(oneZeros);
		all.add(nines);
		System.out.println("list values oneZeros = " + oneZeros + " nines = " + nines + " all = " + all);

		// Flat map to merge streams of both & then average
		Double average = all.stream().flatMapToDouble(l -> l.stream().mapToDouble(d -> d)).average().getAsDouble();
		System.out.println("average = " + average);

	
					
Output:
list values oneZeros = [100.0, 1000000.0, 10000.0] nines = [9999.0, 9999999.0, 99.0] all = [[100.0, 1000000.0, 10000.0], [9999.0, 9999999.0, 99.0]]
average = 1836699.5

Tag: Example for flatMapToDouble method of class java.util.stream.Stream., Stream flatMapToDouble function example with arguments java.util.function.Function<? super T, ? extends java.util.stream.DoubleStream> arg0, How to use flatMapToDouble method of Stream?, Usage of Stream.flatMapToDouble, Stream.flatMapToDouble() examples

public abstract java.util.stream.IntStream flatMapToInt(java.util.function.Function<? super T, ? extends java.util.stream.IntStream> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		// Separate lists for different numbers
		List<Integer> oneZeros = new ArrayList<>();
		oneZeros.add(100);
		oneZeros.add(1000000);
		oneZeros.add(10000);
		List<Integer> nines = new ArrayList<>();
		nines.add(9999);
		nines.add(9999999);
		nines.add(99);

		// List of lists
		List<List<Integer>> all = new ArrayList<>();
		all.add(oneZeros);
		all.add(nines);
		System.out.println("list values oneZeros = " + oneZeros + " nines = " + nines + " all = " + all);

		// Flat map to merge streams of both & then average
		Double average = all.stream().flatMapToInt(l -> l.stream().mapToInt(d -> d)).average().getAsDouble();
		System.out.println("average = " + average);

	
					
Output:
list values oneZeros = [100, 1000000, 10000] nines = [9999, 9999999, 99] all = [[100, 1000000, 10000], [9999, 9999999, 99]]
average = 1836699.5

Tag: Example for flatMapToInt method of class java.util.stream.Stream., Stream flatMapToInt function example with arguments java.util.function.Function<? super T, ? extends java.util.stream.IntStream> arg0, How to use flatMapToInt method of Stream?, Usage of Stream.flatMapToInt, Stream.flatMapToInt() examples

public abstract java.util.stream.LongStream flatMapToLong(java.util.function.Function<? super T, ? extends java.util.stream.LongStream> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		// Separate lists for different numbers
		List<Long> oneZeros = new ArrayList<>();
		oneZeros.add(100L);
		oneZeros.add(1000000L);
		oneZeros.add(10000L);
		List<Long> nines = new ArrayList<>();
		nines.add(9999L);
		nines.add(9999999L);
		nines.add(99L);

		// List of lists
		List<List<Long>> all = new ArrayList<>();
		all.add(oneZeros);
		all.add(nines);
		System.out.println("list values oneZeros = " + oneZeros + " nines = " + nines + " all = " + all);

		// Flat map to merge streams of both & then average
		Double average = all.stream().flatMapToLong(l -> l.stream().mapToLong(d -> d)).average().getAsDouble();
		System.out.println("average = " + average);

	
					
Output:
list values oneZeros = [100, 1000000, 10000] nines = [9999, 9999999, 99] all = [[100, 1000000, 10000], [9999, 9999999, 99]]
average = 1836699.5

Tag: Example for flatMapToLong method of class java.util.stream.Stream., Stream flatMapToLong function example with arguments java.util.function.Function<? super T, ? extends java.util.stream.LongStream> arg0, How to use flatMapToLong method of Stream?, Usage of Stream.flatMapToLong, Stream.flatMapToLong() examples

public abstract void forEach(java.util.function.Consumer<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Tom");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		// Iterate & print all values
		list.stream().forEach(System.out::println);

	
					
Output:
list values = [Tom, Ravi, Amy, Tom, Richard, Remi]
Tom
Ravi
Amy
Tom
Richard
Remi

Tag: Example for forEach method of class java.util.stream.Stream., Stream forEach function example with arguments java.util.function.Consumer<? super T> arg0, How to use forEach method of Stream?, Usage of Stream.forEach, Stream.forEach() examples

public abstract void forEachOrdered(java.util.function.Consumer<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Tom");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		// Iterate & print all values
		list.stream().forEachOrdered(System.out::println);

	
					
Output:
list values = [Tom, Ravi, Amy, Tom, Richard, Remi]
Tom
Ravi
Amy
Tom
Richard
Remi

Tag: Example for forEachOrdered method of class java.util.stream.Stream., Stream forEachOrdered function example with arguments java.util.function.Consumer<? super T> arg0, How to use forEachOrdered method of Stream?, Usage of Stream.forEachOrdered, Stream.forEachOrdered() examples

public static java.util.stream.Stream<T> generate(java.util.function.Supplier<? extends T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		// Generate stream of random numbers. Using limit to avoid infinite numbers
		// stream.
		Stream.generate(() -> Math.random()).limit(5).forEach(System.out::println);

	
					
Output:
0.7830521612515711
0.010343752253523575
0.38950792269534196
0.9268700713460384
0.739512749232883

Tag: Example for generate method of class java.util.stream.Stream., Stream generate function example with arguments java.util.function.Supplier<? extends T> arg0, How to use generate method of Stream?, Usage of Stream.generate, Stream.generate() examples

public abstract boolean isParallel()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");

		System.out.println("Is parallel? = " + list.parallelStream().isParallel());
		System.out.println("Is parallel? = " + list.stream().isParallel());

	
					
Output:
Is parallel? = true
Is parallel? = false

Tag: Example for isParallel method of class java.util.stream.BaseStream., BaseStream isParallel function example , How to use isParallel method of BaseStream?, Usage of BaseStream.isParallel, BaseStream.isParallel() examples

public static java.util.stream.Stream<T> iterate(T arg0, java.util.function.Predicate<? super T> arg1, java.util.function.UnaryOperator<T> arg2)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		// Iterate first 4 elements by index.
		// This is similar to for(int i=0; i<4, i++){}
		// NOTE: Don't use i++ in iterate() because it will iterate infinite. Notice use
		// of ++i which increments & then assigns
		Stream.iterate(0, (i -> i < 4), (i -> ++i))
				.forEach(i -> System.out.println("i = " + i + " name = " + list.get(i)));

	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Remi]
i = 0 name = Tom
i = 1 name = Ravi
i = 2 name = Amy
i = 3 name = Richard

Tag: Example for iterate method of class java.util.stream.Stream., Stream iterate function example with arguments T arg0, java.util.function.Predicate<? super T> arg1, java.util.function.UnaryOperator<T> arg2, How to use iterate method of Stream?, Usage of Stream.iterate, Stream.iterate() examples

public static java.util.stream.Stream<T> iterate(T arg0, java.util.function.UnaryOperator<T> arg1)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		/*
		 * NOTE: Don't use i++ in iterate() because it will iterate infinite. Notice use
		 * of ++i which increments & then assigns This will create infinite stream. For
		 * example purpose we will use limit.
		 */
		Stream.iterate(0, (i -> ++i)).limit(4).forEach(i -> System.out.println("i = " + i));

		// Limit 8
		Stream.iterate(0, (j -> ++j)).limit(8).forEach(j -> System.out.println("j = " + j));

	
					
Output:
i = 0
i = 1
i = 2
i = 3
j = 0
j = 1
j = 2
j = 3
j = 4
j = 5
j = 6
j = 7

Tag: Example for iterate method of class java.util.stream.Stream., Stream iterate function example with arguments T arg0, java.util.function.UnaryOperator<T> arg1, How to use iterate method of Stream?, Usage of Stream.iterate, Stream.iterate() examples

public abstract java.util.Iterator<T> iterator()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		// Getting iterator from stream of list of names.
		Iterator<String> iterator = list.stream().filter(n -> n.startsWith("R")).iterator();
		while (iterator.hasNext()) {
			System.out.println("iterator next = " + iterator.next());
		}
	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Remi]
iterator next = Ravi
iterator next = Richard
iterator next = Remi

Tag: Example for iterator method of class java.util.stream.BaseStream., BaseStream iterator function example , How to use iterator method of BaseStream?, Usage of BaseStream.iterator, BaseStream.iterator() examples

public abstract java.util.stream.Stream<T> limit(long arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		// Generate stream of random numbers. Using limit to avoid infinite numbers
		// stream.
		Stream.generate(() -> Math.random()).limit(5).forEach(r -> System.out.println("Limit 5 item = " + r));
		Stream.generate(() -> Math.random()).limit(8).forEach(r -> System.out.println("Limit 8 item = " + r));

		// Top 5 or highest 5 numbers from stream of list using limit
		List<Double> number = new ArrayList<>();
		number.add(1000000d);
		number.add(1d);
		number.add(100000000d);
		number.add(10000d);
		number.add(10000000d);
		number.add(1000d);
		number.add(10d);
		number.add(100000d);

		List<Double> top5 = number.stream().sorted(Comparator.reverseOrder()).limit(5).collect(Collectors.toList());
		System.out.println("Top/highest 5 = " + top5);

	
					
Output:
Limit 5 item = 0.3692273069658618
Limit 5 item = 0.060950814746752346
Limit 5 item = 0.08688461347308774
Limit 5 item = 0.31418660322060554
Limit 5 item = 0.29989933946384495
Limit 8 item = 0.6195515012563012
Limit 8 item = 0.4251153763376334
Limit 8 item = 0.4097423888054764
Limit 8 item = 0.7870750329346188
Limit 8 item = 0.6879502932218493
Limit 8 item = 0.6926867113548445
Limit 8 item = 0.03840928633570251
Limit 8 item = 0.8001158190086313
Top/highest 5 = [1.0E8, 1.0E7, 1000000.0, 100000.0, 10000.0]

Tag: Example for limit method of class java.util.stream.Stream., Stream limit function example with arguments long arg0, How to use limit method of Stream?, Usage of Stream.limit, Stream.limit() examples

public abstract java.util.stream.Stream<R> map(java.util.function.Function<? super T, ? extends R> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		class Employee {
			public String name;
			public int age;
			public double salary;

			public Employee(String name, int age, double salary) {
				super();
				this.name = name;
				this.age = age;
				this.salary = salary;
			}
		}

		List<Employee> employees = Arrays.asList(new Employee("James", 25, 50000), new Employee("Amy", 32, 60000),
				new Employee("Tom", 35, 70000), new Employee("John", 28, 65000), new Employee("Jimmy", 40, 40000));

		// Average using collect averaging.
		double averageSalary = employees.stream().map(e -> e.salary).collect(Collectors.averagingDouble(s -> s));
		System.out.println("Average salary = " + averageSalary);

		List<String> namesOfEmployees = employees.stream().map(e -> e.name).collect(Collectors.toList());
		System.out.println("Emplyee names = " + namesOfEmployees);
	
					
Output:
Average salary = 57000.0
Emplyee names = [James, Amy, Tom, John, Jimmy]

Tag: Example for map method of class java.util.stream.Stream., Stream map function example with arguments java.util.function.Function<? super T, ? extends R> arg0, How to use map method of Stream?, Usage of Stream.map, Stream.map() examples

public abstract java.util.stream.DoubleStream mapToDouble(java.util.function.ToDoubleFunction<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		class Employee {
			public String name;
			public int age;
			public double salary;

			public Employee(String name, int age, double salary) {
				super();
				this.name = name;
				this.age = age;
				this.salary = salary;
			}
		}

		List<Employee> employees = Arrays.asList(new Employee("James", 25, 50000), new Employee("Amy", 32, 60000),
				new Employee("Tom", 35, 70000), new Employee("John", 28, 65000), new Employee("Jimmy", 40, 40000));

		// Average using average
		OptionalDouble averageSalary = employees.stream().mapToDouble(e -> e.salary).average();
		System.out.println("Average salary = " + averageSalary.getAsDouble());

	
					
Output:
Average salary = 57000.0

Tag: Example for mapToDouble method of class java.util.stream.Stream., Stream mapToDouble function example with arguments java.util.function.ToDoubleFunction<? super T> arg0, How to use mapToDouble method of Stream?, Usage of Stream.mapToDouble, Stream.mapToDouble() examples

public abstract java.util.stream.IntStream mapToInt(java.util.function.ToIntFunction<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		class Employee {
			public String name;
			public int age;
			public double salary;

			public Employee(String name, int age, double salary) {
				super();
				this.name = name;
				this.age = age;
				this.salary = salary;
			}
		}

		List<Employee> employees = Arrays.asList(new Employee("James", 25, 50000), new Employee("Amy", 32, 60000),
				new Employee("Tom", 35, 70000), new Employee("John", 28, 65000), new Employee("Jimmy", 40, 40000));

		// Average using average
		OptionalDouble averageAge = employees.stream().mapToInt(e -> e.age).average();
		System.out.println("Average age of employees = " + averageAge.getAsDouble());

	
					
Output:
Average age of employees = 32.0

Tag: Example for mapToInt method of class java.util.stream.Stream., Stream mapToInt function example with arguments java.util.function.ToIntFunction<? super T> arg0, How to use mapToInt method of Stream?, Usage of Stream.mapToInt, Stream.mapToInt() examples

public abstract java.util.stream.LongStream mapToLong(java.util.function.ToLongFunction<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		class Employee {
			public String name;
			public int age;
			public double salary;

			public Employee(String name, int age, double salary) {
				super();
				this.name = name;
				this.age = age;
				this.salary = salary;
			}
		}

		List<Employee> employees = Arrays.asList(new Employee("James", 25, 50000), new Employee("Amy", 32, 60000),
				new Employee("Tom", 35, 70000), new Employee("John", 28, 65000), new Employee("Jimmy", 40, 40000));

		// Average using average
		OptionalDouble averageAge = employees.stream().mapToLong(e -> e.age).average();
		System.out.println("Average age of employees = " + averageAge.getAsDouble());

	
					
Output:
Average age of employees = 32.0

Tag: Example for mapToLong method of class java.util.stream.Stream., Stream mapToLong function example with arguments java.util.function.ToLongFunction<? super T> arg0, How to use mapToLong method of Stream?, Usage of Stream.mapToLong, Stream.mapToLong() examples

public abstract java.util.Optional<T> max(java.util.Comparator<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		class Employee {
			public String name;
			public int age;
			public double salary;

			public Employee(String name, int age, double salary) {
				super();
				this.name = name;
				this.age = age;
				this.salary = salary;
			}
		}

		List<Employee> employees = Arrays.asList(new Employee("James", 25, 50000), new Employee("Amy", 32, 60000),
				new Employee("Tom", 35, 70000), new Employee("John", 28, 65000), new Employee("Jimmy", 40, 40000));

		Optional<Employee> employeeWithMaxSalary = employees.stream()
				.max((e1, e2) -> Double.compare(e1.salary, e2.salary));
		System.out.println("Employee with max salary = " + employeeWithMaxSalary.get().name + ", Salary = "
				+ employeeWithMaxSalary.get().salary);

	
					
Output:
Employee with max salary = Tom, Salary = 70000.0

Tag: Example for max method of class java.util.stream.Stream., Stream max function example with arguments java.util.Comparator<? super T> arg0, How to use max method of Stream?, Usage of Stream.max, Stream.max() examples

public abstract java.util.Optional<T> min(java.util.Comparator<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		class Employee {
			public String name;
			public int age;
			public double salary;

			public Employee(String name, int age, double salary) {
				super();
				this.name = name;
				this.age = age;
				this.salary = salary;
			}
		}

		List<Employee> employees = Arrays.asList(new Employee("James", 25, 50000), new Employee("Amy", 32, 60000),
				new Employee("Tom", 35, 70000), new Employee("John", 28, 65000), new Employee("Jimmy", 40, 40000));

		Optional<Employee> youngestEmployee = employees.stream().min((e1, e2) -> Integer.compare(e1.age, e2.age));
		System.out.println(
				"Youngest employee = " + youngestEmployee.get().name + ", Age = " + youngestEmployee.get().age);

	
					
Output:
Youngest employee = James, Age = 25

Tag: Example for min method of class java.util.stream.Stream., Stream min function example with arguments java.util.Comparator<? super T> arg0, How to use min method of Stream?, Usage of Stream.min, Stream.min() examples

public abstract boolean noneMatch(java.util.function.Predicate<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		// Check if none of the name starts with 'R'
		boolean noNameStartWithR = list.stream().noneMatch(s -> s.startsWith("R"));
		System.out.println("No name start with 'R' = " + noNameStartWithR);

		// Check if none of the name starts with 'K'
		boolean noNameStartWithK = list.stream().noneMatch(s -> s.startsWith("K"));
		System.out.println("No name start with 'K' = " + noNameStartWithK);

		// With empty list
		boolean noNameStartWithK_emptyInput = new ArrayList<String>().stream().noneMatch(s -> s.startsWith("K"));
		System.out.println("No name start with 'K' (empty input) = " + noNameStartWithK_emptyInput);
	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Remi]
No name start with 'R' = false
No name start with 'K' = true
No name start with 'K' (empty input) = true

Tag: Example for noneMatch method of class java.util.stream.Stream., Stream noneMatch function example with arguments java.util.function.Predicate<? super T> arg0, How to use noneMatch method of Stream?, Usage of Stream.noneMatch, Stream.noneMatch() examples

public static java.util.stream.Stream<T> of(T arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		Stream.of("Ravi").forEach(n -> System.out.println("Newly created stream with single object = " + n));

		// Stream with null value. Causes NullPointerException. Use ofNullable
		Stream.of(null).forEach(n -> System.out.println("Newly created stream with single object = " + n));

	
					
Output:
Newly created stream with single object = Ravi
java.lang.NullPointerException
	at java.base/java.util.Arrays.stream(Arrays.java:5614)
	at java.base/java.util.stream.Stream.of(Stream.java:1188)
	at com.itsallbinary.javadocexamples.examples.java_util_stream.StreamExamples.test_of_Object(StreamExamples.java:809)

Tag: Example for of method of class java.util.stream.Stream., Stream of function example with arguments T arg0, How to use of method of Stream?, Usage of Stream.of, Stream.of() examples, Java 8 create new stream with single object.

public static transient java.util.stream.Stream<T> of(T[] arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		List<String> namesWithR = Stream.of(new String[] { "Tom", "Ravi", "Amy", "Richard", "Remi" })
				.filter(n -> n.startsWith("R")).collect(Collectors.toList());

		System.out.println("Names starting with 'R' = " + namesWithR);

		// Stream from array with null values. Causes NullPointerException
		List<String> namesWithR2 = Stream.of(new String[] { "Tom", null, "Amy", null, "Remi" })
				.filter(n -> n.startsWith("R")).collect(Collectors.toList());

		System.out.println("Names starting with 'R' = " + namesWithR2);
	
					
Output:
Names starting with 'R' = [Ravi, Richard, Remi]
java.lang.NullPointerException
	at com.itsallbinary.javadocexamples.examples.java_util_stream.StreamExamples.lambda$73(StreamExamples.java:825)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:176)
	at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
	at com.itsallbinary.javadocexamples.examples.java_util_stream.StreamExamples.test_of_ObjectArray(StreamExamples.java:825)

Tag: Example for of method of class java.util.stream.Stream., Stream of function example with arguments T[] arg0, How to use of method of Stream?, Usage of Stream.of, Stream.of() examples, Java 8 create new stream without collection or list., Java 8 create new stream from multiple values., Create java 8 stream directly from values.

public static java.util.stream.Stream<T> ofNullable(T arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				

		Stream.ofNullable("Ravi").forEach(n -> System.out.println("Newly created stream with single object = " + n));

		// Stream with null value.
		Stream.ofNullable(null).forEach(n -> System.out.println("Newly created stream with single object = " + n));

	
					
Output:
Newly created stream with single object = Ravi

Tag: Example for ofNullable method of class java.util.stream.Stream., Stream ofNullable function example with arguments T arg0, How to use ofNullable method of Stream?, Usage of Stream.ofNullable, Stream.ofNullable() examples

public abstract S onClose(java.lang.Runnable arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		// Without close()
		list.stream().onClose(() -> System.out.println("Calling close handler"))
				.forEach(n -> System.out.println("(Without close) Name = " + n));

		// With close()
		list.stream().onClose(() -> System.out.println("Calling close handler")).close();

	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Remi]
(Without close) Name = Tom
(Without close) Name = Ravi
(Without close) Name = Amy
(Without close) Name = Richard
(Without close) Name = Remi
Calling close handler

Tag: Example for onClose method of class java.util.stream.BaseStream., BaseStream onClose function example with arguments java.lang.Runnable arg0, How to use onClose method of BaseStream?, Usage of BaseStream.onClose, BaseStream.onClose() examples

public abstract S parallel()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		boolean isStreamParallel = list.stream().parallel().isParallel();

		System.out.println("Is stream parallel? = " + isStreamParallel);

		boolean isStreamParallel_1 = list.stream().isParallel();

		System.out.println("Is stream parallel? = " + isStreamParallel_1);
	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Remi]
Is stream parallel? = true
Is stream parallel? = false

Tag: Example for parallel method of class java.util.stream.BaseStream., BaseStream parallel function example , How to use parallel method of BaseStream?, Usage of BaseStream.parallel, BaseStream.parallel() examples

public abstract java.util.stream.Stream<T> peek(java.util.function.Consumer<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		List<String> namesStartingWithR = list.stream().peek(n -> System.out.println("Peeking name = " + n))
				.filter(n -> n.startsWith("R")).peek(n -> System.out.println("Peeking again name = " + n))
				.collect(Collectors.toList());

		System.out.println("Names starting with 'R' = " + namesStartingWithR);
	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Remi]
Peeking name = Tom
Peeking name = Ravi
Peeking again name = Ravi
Peeking name = Amy
Peeking name = Richard
Peeking again name = Richard
Peeking name = Remi
Peeking again name = Remi
Names starting with 'R' = [Ravi, Richard, Remi]

Tag: Example for peek method of class java.util.stream.Stream., Stream peek function example with arguments java.util.function.Consumer<? super T> arg0, How to use peek method of Stream?, Usage of Stream.peek, Stream.peek() examples

public abstract T reduce(T arg0, java.util.function.BinaryOperator<T> arg1)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		ArrayList<String> list = new ArrayList<>();
		list.add("Tom");
		list.add("Ravi");
		list.add("Amy");
		list.add("Richard");
		list.add("Remi");
		System.out.println("list values = " + list);

		String reduced = list.stream().reduce("Names are", (n1, n2) -> n1 + " " + n2);
		System.out.println("reduced = " + reduced);

		// Null identity & comma separated accumulator
		String reducedWithNull = list.stream().reduce(null, (n1, n2) -> n1 + ", " + n2);
		System.out.println("reducedWithNull = " + reducedWithNull);

		// Empty string identity with brackets
		String reducedWithEmpty = list.stream().reduce("", (n1, n2) -> "(" + n1 + ") " + n2);
		System.out.println("reducedWithEmpty = " + reducedWithEmpty);
	
					
Output:
list values = [Tom, Ravi, Amy, Richard, Remi]
reduced = Names are Tom Ravi Amy Richard Remi
reducedWithNull = null, Tom, Ravi, Amy, Richard, Remi
reducedWithEmpty = ((((() Tom) Ravi) Amy) Richard) Remi

Tag: Example for reduce method of class java.util.stream.Stream., Stream reduce function example with arguments T arg0, java.util.function.BinaryOperator<T> arg1, How to use reduce method of Stream?, Usage of Stream.reduce, Stream.reduce() examples

Imports

[↑ Method List]

Import statements used in examples.

				java.util.ArrayList
java.util.Arrays
java.util.Collections
java.util.Comparator
java.util.HashSet
java.util.Iterator
java.util.List
java.util.Map
java.util.Optional
java.util.OptionalDouble
java.util.Set
java.util.function.BiConsumer
java.util.function.BiFunction
java.util.function.BinaryOperator
java.util.function.Consumer
java.util.function.Function
java.util.function.IntFunction
java.util.function.Predicate
java.util.function.Supplier
java.util.function.ToDoubleFunction
java.util.function.ToIntFunction
java.util.function.ToLongFunction
java.util.function.UnaryOperator
java.util.stream.Collector
java.util.stream.Collectors
java.util.stream.Stream
org.apache.commons.collections4.multiset.HashMultiSet
com.google.common.collect.HashMultiset
com.google.common.collect.Multiset
		

Tag: Simple working examples of methods / functions of class java.util.stream.Stream along with their console output, java.util.stream.Stream tutorial., Guide to java.util.stream.Stream & its methods., Understanding java.util.stream.Stream with examples.