Found 4336 Articles for Java 8

LongStream peek() method in Java

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

114 Views

The peek() method of the LongStream class returns a stream with the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.The syntax is as follows −LongStream peek(LongConsumer action)Here, LongConsumer represents an operation that accepts a single long-valued argument and returns no result. The action parameter is a non-interfering action to perform on the elements as they are consumed from the stream.To use the LongStream class in Java, import the following package −import java.util.stream.LongStream;The following is an example to implement LongStream peek() method in JavaExample Live Demoimport java.util.stream.LongStream; public class Demo ... Read More

LongStream rangeClosed() method in Java

Anvi Jain
Updated on 30-Jul-2019 22:30:25

234 Views

The rangeClosed() method of the LongStream class in Java returns a sequential ordered LongStream from startInclusive to endExclusive by an incremental step of 1. This is inclusive of the initial element and the last element.The syntax is as follows −static LongStream rangeClosed(long startInclusive, long endExclusive)Here, startInclusive is the first value, whereas the endExclusive is the last.To use the LongStream class in Java, import the following package −import java.util.stream.LongStream;The following is an example to implement LongStream rangeClosed() method in Java −Example Live Demoimport java.util.stream.LongStream; public class Demo { public static void main(String[] args) { ... Read More

Collectors averagingDouble() method in Java 8

Smita Kapse
Updated on 30-Jul-2019 22:30:25

385 Views

The averagingDouble() method of the Collectors class in Java 8 returns a Collector that is the arithmetic mean of a double-valued function applied to the input elements.The syntax is as follows −public static Collector averagingDouble(ToDoubleFunction

The set() method of CopyOnWriteArrayList in Java

George John
Updated on 30-Jul-2019 22:30:25

76 Views

The set() method of the CopyOnWriteArrayList class is used to replace the element at the specified position in this list with the specified element. It returns the element that gets replaced.The syntax is as followspublic E set(int index, E ele)Here, the parameter index is the index of the element to replace and ele is the element to be stored at the specified position.To work with CopyOnWriteArrayList class, you need to import the following packageimport java.util.concurrent.CopyOnWriteArrayList;The following is an example to implement CopyOnWriteArrayList class set() method in JavaExample Live Demoimport java.util.concurrent.CopyOnWriteArrayList; public class Demo { public static void main(String[] ... Read More

ArrayBlockingQueue clear() Method in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:25

81 Views

The clear() method of the ArrayBlockingQueue class in Java is used to remove all the elements from this queue.The syntax is as followspublic void clear()To work with ArrayBlockingQueue class, you need to import the following packageimport java.util.concurrent.ArrayBlockingQueue;The following is an example to implement clear() method of Java ArrayBlockingQueue classExample Live Demoimport java.util.ArrayList; import java.util.Iterator; import java.util.concurrent.ArrayBlockingQueue; public class Demo { public static void main(String[] args) throws InterruptedException { ArrayBlockingQueue q = new ArrayBlockingQueue(10); q.add(120); q.add(10); ... Read More

Generate Infinite Stream of Integers in Java using Random.ints()

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

166 Views

To generate Infinite Stream of Integers, you can use the Random class and its ints() methodRandom.ints()Here, we have used the ints() method to get the next integer.The following is an example displaying how to generate Infinite Stream of Integers with Random.ints() in JavaExampleimport java.util.stream.*; import java.util.*; public class Demo { public static void main(String[] args) { Random r = new Random(); r.ints().forEach(System.out::println); } }Output78799000099 8787879898 2872737888 . . .

LongStream max() method in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

211 Views

The max() method of the LongStream class in Java returns an OptionalLong describing the maximum element of this stream, or an empty optional if this stream is empty.The syntax is as followsOptionalLong max()Here, OptionalLong is a container object which may or may not contain a long value.To use the LongStream class in Java, import the following packageimport java.util.stream.LongStream;The following is an example to implement LongStream max() method in Java. The isPresent() method of the OptionalLong class returns true if the value is presentExample Live Demoimport java.util.*; import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {     ... Read More

IntStream average() method in Java

George John
Updated on 30-Jul-2019 22:30:25

10K+ Views

The average() method of the IntStream class in Java returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty. It gets the average of the elements of the stream.The syntax is as followsOptionalDouble average()Here, OptionalDouble is a container object which may or may not contain a double value.Create an IntStream with some elementsIntStream intStream = IntStream.of(15, 13, 45, 18, 89, 70, 76, 56);Now, get the average of the elements of the streamOptionalDouble res = intStream.average();The following is an example to implement IntStream average() method in Java. The isPresent() method ... Read More

Generate Infinite Stream of Double in Java using DoubleStream.iterate()

Chandu yadav
Updated on 30-Jul-2019 22:30:25

65 Views

The DoubleStream.iterate() returns an infinite sequential ordered DoubleStream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed.The syntax is as followsstatic DoubleStream iterate(double seed, DoubleUnaryOperator f)Here, seed is the initial element and f is a function to be applied to the previous element to produce a new element.To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;The following is an example to generate infinite stream of Double in Java with DoubleStream.iterate()Exampleimport java.util.stream.DoubleStream; public class Demo { public static void main(String[] args) { ... Read More

LongStream.Builder build() method in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

69 Views

The build() method of the LongStream.Builder class builds the stream, transitioning this builder to the built state.The syntax is as followsLongStream build()To use the LongStream.Builder class in Java, import the following packageimport java.util.stream.LongStream;The following is an example to implement LongStream.Builder build() method in JavaExample Live Demoimport java.util.stream.LongStream; public class Demo { public static void main(String[] args) { LongStream.Builder builder = LongStream.builder(); builder.accept(255L); builder.accept(570L); builder.accept(355L); builder.accept(155L); ... Read More

Advertisements