Skip to main content


Java Stream Collectors summingInt(), summingLong(), summingDouble()

In the tutorial Java Stream Collectors averagingInt(), averagingLong(), averagingDouble() we saw how Collectors class in Java Stream API provides methods like Collectors.averagingInt(), Collectors.averagingLong(), Collectors.averagingDouble() to get average of Stream on Integers, Long and Double respectively. Same way if you want to quickly add the stream elements there are Collectors.summing() methods which have the following forms for different types- summingInt(ToIntFunction<? super T> mapper) - To get sum of stream of integers. If no elements are present, the result is 0. summingLong(ToLongFunction<? super T> mapper) - To get sum of stream of longs. If no elements are present, the result is 0. summingDouble(ToDoubleFunction<? super T> mapper) - To get sum of stream of doubles. If no elements are present, the result is 0. Argument passed to the methods is of type ToIntFunction, ToLongFunction and ToDoubleFunction respectively. These are functio

Java Stream Collectors averagingInt(), averagingLong(), averagingDouble()

If you want average of Stream elements there are handy utility methods available in Collectors class to quickly do that, the methods are- Collectors.averagingInt()- To get average of stream of integers. Collectors.averagingLong()- To get average of stream of longs. Collectors.averagingDouble()- To get average of stream of doubles. Collectors.averaging methods in Java Syntax of these methods is as given below- Collector<T,?,Double> averagingInt(ToIntFunction<? super T> mapper) Collector<T,?,Double> averagingLong(ToLongFunction<? super T> mapper) Collector<T,?,Double> averagingDouble(ToDoubleFunction<? super T> mapper) All of these methods return a Collector that produces the arithmetic mean of the values. Argument passed is of type ToIntFunction, ToLongFunction and ToDoubleFunction respectively. These are functional interfaces with methods applyAsInt(T value) , applyAsLong(T value) , applyAsDouble(T value) that produces a i

Java Stream - Collectors Class And collect() Method

When using Java streams , most of the time you will have a Collection as a source for the stream but you can also do the opposite i.e. obtain a Collection from a Stream. To do that you can use collect() method in the Java Stream API. Here note that collect() method performs a mutable reduction operation on the elements of this stream which returns a mutable result container. This mutable result container can be a Collection class like ArrayList, HashSet or a StringBuilder etc. Table of contents collect() method in Java Stream Collectors class in Java Stream Java Collectors example Using collect method with Combiner collect() method in Java Stream collect() method is a terminal operation and there are two overloaded collect() methods. 1- <R,A> R collect(Collector<? super T,A,R> collector) - Performs a mutable reduction operation on the elements of this stream using a Collector. In the method type parameters are as- T- The type of input elements

Java Stream findFirst(), findAny() With Examples

In the Java Stream API there are two methods findFirst() and findAny() where- findFirst() returns the first element of the Stream. findAny() returns any element of the Stream. Table of contents Java Stream findFirst() method Java Stream findFirst() examples Java Stream findAny() method Java Stream findAny() examples Java Stream findFirst() method The findFirst() method returns an Optional describing the first element of this stream in case Stream is not empty, returns an empty Optional if the stream is empty. Optional<T> findFirst() findFirst() is a short-circuiting terminal operation which means it may terminate in finite time when presented with infinite input. Java Stream findFirst() examples 1. A simple example to get first element of the list where List is the source for the Stream. import java.util.Arrays; import java.util.List; import java.util.Optional; public class FindFirstDemo { public static void main(String[] args) { L

Java Primitive Type Streams With Examples

Java Stream API was one of the important addition in Java 8 and provides a very efficient way to process collection of objects. Since Stream only works with object references so using it with primitive types is not very efficient. You will need to use wrapper classes to wrap the primitive data types when used with Stream i.e. Stream<Integer>, Stream<Long>, Stream<Double> making it inefficient because of conversion of primitive types to corresponding objects. Fortunately Java Stream API provides primitive specializations of Stream for the primitive types int, long and double. The primitive type streams available in Java are- IntStream - This is the int primitive specialization of Stream. LongStream - long primitive specialization of Stream. DoubleStream - This is the double primitive specialization of Stream. Table of contents Creating primitive Streams empty() method of() method range() and rangeClosed() methods Using Ar

ArrayList Vs CopyOnWriteArrayList in Java

ArrayList is part of Java from version 1.2 where as CopyOnWriteArrayList was added in Java 5 as a thread-safe variant of ArrayList. In this post we’ll see the differences between ArrayList and CopyOnWriteArrayList in Java . CopyOnWriteArrayList Vs ArrayList in Java 1- Thread Safety: CopyOnWriteArrayList is thread safe. Thread safety in CopyOnWriteArrayList is achieved by making a fresh copy of the underlying array for all mutative operations (add, set, and so on). ArrayList in Java is not thread safe. 2- External synchronization: CopyOnWriteArrayList is already thread safe so no external synchronization needed. ArrayList can be synchronized externally by using Collections.synchronizedList() method which returns a synchronized List backed by the specified List. Read more about synchronizing ArrayList in this post- How to Synchronize ArrayList in Java 3- Iterator fail-fast or fail-safe: The iterator returned by CopyOnWriteArrayList is fail-safe and the iterator is

LinkedBlockingDeque in Java With Examples

LinkedBlockingDeque in Java is an implementation of BlockingDeque interface and is part of java.util.concurrent package. LinkedBlockingDeque, just like LinkedBlockingQueue , is an optionally-bounded blocking queue which means the capacity for the queue can be specified thus making it bounded or it can be left unspecified in which case capacity of the deque is Integer.MAX_VALUE. LinkedBlockingDeque in Java LinkedBlockingDeque is based on linked nodes where each node holds reference to both previous and next nodes. Linked nodes are dynamically created upon each insertion unless this would bring the deque above capacity. Since LinkedBlockingDeque is a deque (double ended queue) elements can be inserted and removed from both ends of the queue that is how LinkedBlockingDeque differs from the BlockingQueue implementations like ArrayBlockingQueue where elements are added to the end of the queue and retrieved from the head of the queue. LinkedBlockingDeque implementation is th