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

Package java.util.stream

Class java.util.stream.Collectors

Examples for usage of all methods of 'java.util.stream.Collectors' 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 static java.util.stream.Collector<T, ?, java.lang.Double> averagingDouble(java.util.function.ToDoubleFunction<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		// Average of list of double values
		List<Double> doubles = Arrays.asList(10d, 12d, 15d, 13d, 10d);
		double average = doubles.stream().collect(Collectors.averagingDouble(d -> d));
		System.out.println("Average of doubles " + doubles + " = " + average);

		// Average of empty list of Double type
		List<Double> emptyDoubles = new ArrayList<>();
		double averageOfEmptyDoubles = emptyDoubles.stream().collect(Collectors.averagingDouble(d -> d));
		System.out.println("Average of empty list of doubles " + doubles + " = " + averageOfEmptyDoubles);

	
					
Output:
Average of doubles [10.0, 12.0, 15.0, 13.0, 10.0] = 12.0
Average of empty list of doubles [10.0, 12.0, 15.0, 13.0, 10.0] = 0.0

Tag: Example for averagingDouble method of class java.util.stream.Collectors., Collectors averagingDouble function example with arguments java.util.function.ToDoubleFunction<? super T> arg0, How to use averagingDouble method of Collectors?, Usage of Collectors.averagingDouble, Collectors.averagingDouble() examples, How to calculate average of double numbers using java 8 streams and lambda.

public static java.util.stream.Collector<T, ?, java.lang.Double> averagingInt(java.util.function.ToIntFunction<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		// Average of list of int values
		List<Integer> integers = Arrays.asList(10, 12, 15, 13, 10);
		double average = integers.stream().collect(Collectors.averagingInt(i -> i));
		System.out.println("Average of integers " + integers + " = " + average);

		// Average of empty list of Integer type
		List<Integer> emptyIntegers = new ArrayList<>();
		double averageOfEmptyIntegers = emptyIntegers.stream().collect(Collectors.averagingDouble(d -> d));
		System.out.println("Average of empty list of integers " + emptyIntegers + " = " + averageOfEmptyIntegers);
	
					
Output:
Average of integers [10, 12, 15, 13, 10] = 12.0
Average of empty list of integers [] = 0.0

Tag: Example for averagingInt method of class java.util.stream.Collectors., Collectors averagingInt function example with arguments java.util.function.ToIntFunction<? super T> arg0, How to use averagingInt method of Collectors?, Usage of Collectors.averagingInt, Collectors.averagingInt() examples, How to calculate average of integer or int numbers using java 8 streams and lambda.

public static java.util.stream.Collector<T, ?, java.lang.Double> averagingLong(java.util.function.ToLongFunction<? super T> arg0)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		// Average of list of long values
		List<Long> longs = Arrays.asList(10l, 12l, 15l, 13l, 10l);
		double average = longs.stream().collect(Collectors.averagingLong(d -> d));
		System.out.println("Average of longs " + longs + " = " + average);

		// Average of empty list of long type
		List<Long> emptyLongs = new ArrayList<>();
		double averageOfEmptyLongs = emptyLongs.stream().collect(Collectors.averagingDouble(d -> d));
		System.out.println("Average of empty list of longs " + emptyLongs + " = " + averageOfEmptyLongs);
	
					
Output:
Average of longs [10, 12, 15, 13, 10] = 12.0
Average of empty list of longs [] = 0.0

Tag: Example for averagingLong method of class java.util.stream.Collectors., Collectors averagingLong function example with arguments java.util.function.ToLongFunction<? super T> arg0, How to use averagingLong method of Collectors?, Usage of Collectors.averagingLong, Collectors.averagingLong() examples, How to calculate average of long numbers using java 8 streams and lambda.

public static java.util.stream.Collector<T, A, RR> collectingAndThen(java.util.stream.Collector<T, A, R> downstream, java.util.function.Function<R, RR> finisher)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		List<Double> doubles = Arrays.asList(10d, 12d, 15d, 13d, 10d);

		// Multiply all values by 10 & collect stream results as a LinkedList.
		// toList() creates List. Use collectingAndThen to create LinekdList from list.
		LinkedList<Double> linkedListOfDoubles = doubles.stream().map(d -> d * 10)
				.collect(Collectors.collectingAndThen(Collectors.toList(), (l -> new LinkedList<>(l))));
		System.out.println("Linked list of values = " + linkedListOfDoubles);

	
					
Output:
Linked list of values = [100.0, 120.0, 150.0, 130.0, 100.0]

Tag: Example for collectingAndThen method of class java.util.stream.Collectors., Collectors collectingAndThen function example with arguments java.util.stream.Collector<T, A, R> downstream, java.util.function.Function<R, RR> finisher, How to use collectingAndThen method of Collectors?, Usage of Collectors.collectingAndThen, Collectors.collectingAndThen() examples, Collect stream results as LinkedList or ArrayList or HashSet or HashMap

public static java.util.stream.Collector<T, ?, java.lang.Long> counting()

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		List<Double> doubles = Arrays.asList(10d, 12d, 15d, 13d, 10d);

		long count = doubles.stream().collect(Collectors.counting());
		System.out.println("Count of elements = " + count);

		long countGreaterThan12 = doubles.stream().filter(d -> d > 12d).collect(Collectors.counting());
		System.out.println("Count of elements greater than 12 = " + countGreaterThan12);
	
					
Output:
Count of elements = 5
Count of elements greater than 12 = 2

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

public static java.util.stream.Collector<T, ?, R> filtering(java.util.function.Predicate<? super T> predicate, java.util.stream.Collector<? super T, A, R> downstream)

[⿺ Javadoc] [↑ Method List] [↓ Imports]
Loading...
Usage:
				
		List<Double> doubles = Arrays.asList(10d, 12d, 15d, 13d, 10d);

		List<Double> filteredList = doubles.stream().collect(Collectors.filtering(d -> d > 12d, Collectors.toList()));
		System.out.println("filteredList values = " + filteredList);

		long countGreaterThan12 = doubles.stream().collect(Collectors.filtering(d -> d > 12d, Collectors.counting()));
		System.out.println("Count of elements greater than 12 = " + countGreaterThan12);

		class Employee {
			public String name;
			public String department;
			public double salary;

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

			@Override
			public String toString() {
				return name + " " + department + " " + salary;
			}
		}

		// grouping by with filtering collectors.
		List<Employee> employees = Arrays.asList(new Employee("James", "IT", 50000),
				new Employee("Amy", "Sales", 60000), new Employee("Tom", "IT", 70000),
				new Employee("John", "Sales", 65000), new Employee("Jimmy", "Sales", 40000));
		Map<Object, ArrayList<Employee>> employeesAbove40k = employees.stream().collect(Collectors.groupingBy(
				e -> e.department,
				Collectors.filtering(e -> e.salary > 40000, Collectors.toCollection(() -> new ArrayList<Employee>()))));

		System.out.println(employeesAbove40k);

	
					
Output:
filteredList values = [15.0, 13.0]
Count of elements greater than 12 = 2
{Sales=[Amy Sales 60000.0, John Sales 65000.0], IT=[James IT 50000.0, Tom IT 70000.0]}

Tag: Example for filtering method of class java.util.stream.Collectors., Collectors filtering function example with arguments java.util.function.Predicate<? super T> predicate, java.util.stream.Collector<? super T, A, R> downstream, How to use filtering method of Collectors?, Usage of Collectors.filtering, Collectors.filtering() examples

Imports

[↑ Method List]

Import statements used in examples.

				java.util.stream.Collectors
java.util.function.ToDoubleFunction
java.util.function.ToIntFunction
java.util.function.ToLongFunction
java.util.stream.Collector
java.util.function.Function
java.util.function.Predicate
java.util.function.Supplier
java.util.ArrayList
java.util.Arrays
java.util.Comparator
java.util.LinkedList
java.util.List
java.util.Map
java.util.function.BinaryOperator
		

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