Found 7442 Articles for Java

The toArray(T[] a) T method of Java AbstractCollection class

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

145 Views

The difference between toArray() and toArray(T[] arr) in Java AbstractCollection is that both the methods returns an array containing all of the elements in this collection, but the latter has some additional features i.e. the runtime type of the returned array is that of the specified array.The syntax is as followspublic T[] toArray(T[] arr)Here, arr is the array into which the elements of this collection are to be stored.To work with AbstractCollection class in Java, import the following packageimport java.util.AbstractCollection;The following is an example to implement AbstractCollection toArray() method in JavaExample Live Demoimport java.util.ArrayList; import java.util.AbstractCollection; public class Demo { ... Read More

ArrayBlockingQueue add() method in Java

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

108 Views

To add elements to the ArrayBlockingQueue class, use the add() method.The syntax is as followsboolean add(E ele)Here, ele is the element to be added to the queue. To work with ArrayBlockingQueue class, you need to import the following packageimport java.util.concurrent.ArrayBlockingQueue;The following is an example to add elements in Java ArrayBlockingQueue classExample Live Demoimport java.util.concurrent.ArrayBlockingQueue; public class Demo {    public static void main(String[] args) {       ArrayBlockingQueue q = new ArrayBlockingQueue(7);       q.add(100); q.add(250); q.add(300); q.add(450); q.add(550); q.add(600); q.add(700);       System.out.println("ArrayBlockingQueue = " + q);    } }OutputArrayBlockingQueue = [100, 250, 300, 450, 550, 600, 700]

DoubleStream summaryStatistics() method in Java

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

145 Views

The summaryStatistics() method of the DoubleStream class returns a DoubleSummaryStatistics describing various summary data about the elements of this stream. This is a special case of a reduction.The syntax is as followsDoubleSummaryStatistics summaryStatistics()Here, DoubleSummaryStatistics is a state object for collecting statistics such as count, min, max, average, etc. It works with streams. To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;First, create a DoubleStream and add some elementsDoubleStream doubleStream = DoubleStream.of(20.5, 35.8, 45.9, 50.8, 80.7);Now, get the statisticsDoubleSummaryStatistics details = doubleStream.summaryStatistics();The following is an example to implement DoubleStream summaryStatistics() methodExample Live Demoimport java.util.stream.DoubleStream; import java.util.DoubleSummaryStatistics; public class Demo ... Read More

LongStream map() method in Java

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

130 Views

The map() method of the LongStream class in Java returns a stream consisting of the results of applying the given function to the elements of this stream.The syntax is as follows:LongStream map(LongUnaryOperator mapper)Here, the parameter mapper is a stateless function to apply to each element. The LongUnaryOperator represents an operation on a single long-valued operand that produces a long-valued result.To use the LongStream class in Java, import the following package:import java.util.stream.LongStream;Create a LongStream and add some elements:LongStream longStream1 = LongStream.of(15L, 30L, 45L, 67L, 80L);Now, create another LongStream and map it to a condition set for the elements of longStream1:LongStream longStream2 ... Read More

Generate Infinite Stream of Integers in Java using IntStream.generate()

Nancy Den
Updated on 30-Jul-2019 22:30:25

170 Views

You can also generate Infinite Stream of Integers in Java with IntStream.generate() method. Here, we have used the Random class to get the list of random integers:Random r = new Random();After that use IntStream.generate() and the nextInt() method gets the next random integer:IntStream.generate(r::nextInt)The following is an example displaying how to generate Infinite Stream of Integers with IntStream.generate() in Java:import java.util.stream.*; import java.util.*; public class Main {    public static void main(String[] args) {       Random r = new Random();       IntStream.generate(r::nextInt).forEach(System.out::println);    } }Here is the output:565757777 3535363636 9879879879 -686549988Read More

IntStream builder() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

153 Views

The builder() method in IntStream class is used to return a builder for an IntStream.The syntax is as follows:static IntStream.Builder builder()Here, we have created an IntStream and used the builder() method. An element is added using add() method:IntStream intStream = IntStream.builder().add(25).build();The following is an example to implement IntStream builder() method in Java −Example Live Demoimport java.util.*; import java.util.stream.Stream; import java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream intStream = IntStream.builder().add(25).build();       intStream.forEach(System.out::println);    } }output25

Create KeyValue Tuple using with() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

106 Views

To create a KeyValue tuple in Java, you can use the with() method. Let us first see what we need to work with JavaTuples. To work with KeyValue class in JavaTuples, you need to import the following package:import org.javatuples.KeyValue;Note: Download JavaTuples Jar library to run JavaTuples program. If you are using Eclipse IDE, then Right Click Project → Properties → Java Build Path → Add External Jars  and upload the downloaded JavaTuples jar file. Refer the below guide for all the steps to run JavaTuples:Steps: How to run JavaTuples program in EclipseThe following is an example to create KeyValue tuple using ... Read More

LongStream generate() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

145 Views

The generate() method of the LongStream class returns an infinite sequential unordered stream where each element is generated by the provided LongSupplier.The syntax is as follows:static LongStream generate(LongSupplier s)Here, s is the LongSupplier for generate elements. The LongSupplier is the supplier of long-valued results.To use the LongStream class in Java, import the following package:import java.util.stream.LongStream;The following is an example to implement LongStream generate() method in Java −Example Live Demoimport java.util.stream.LongStream; public class Demo {    public static void main(String[] args){       LongStream longStream = LongStream.generate(()          -> { return (long)(Math.random() * 100); });       ... Read More

Create Decade Tuple from an array in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

120 Views

To create Decade Tuple from array in Java, use the fromArray() method. Here, we will see how to create a Decade Tuple using the fromArray() method. Let us first see what we need to work with JavaTuples. To work with Decade class in JavaTuples, you need to import the following package:import org.javatuples.Decade;Note: Download JavaTuples Jar library to run JavaTuples program. If you are using Eclipse IDE, then Right Click Project → Properties → Java Build Path → Add External Jars and upload the downloaded JavaTuples jar file. Refer the below guide for all the steps to run JavaTuples:Steps: How to ... Read More

LongStream mapToDouble() method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

164 Views

The mapToDouble() method returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.The syntax is as follows:DoubleStream mapToDouble(LongToDoubleFunction mapper)The parameter mapper is a stateless function to apply to each element.To use the LongStream class in Java, import the following package:import java.util.stream.LongStream;The following is an example to implement LongStream mapToDouble() method in JavaExampleimport java.util.stream.LongStream; import java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.of(5000L, 12000L, 15000L, 20000L, 25000L);       DoubleStream s = longStream.mapToDouble(a → (double)a);       System.out.println("Elements of DoubleStream..."); ... Read More

Advertisements