Object Oriented Programming Articles

Page 246 of 589

The clear() method of CopyOnWriteArrayListin Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 137 Views

To remove all the elements from the CopyOnWriteArrayList, use the clear() method. It empties the list.The syntax is as follows:void clear()To work with CopyOnWriteArrayList class, you need to import the following package:import java.util.concurrent.CopyOnWriteArrayList;The following is an example to implement CopyOnWriteArrayList class clear() method in Java:Exampleimport java.util.concurrent.CopyOnWriteArrayList; public class Demo {    public static void main(String[] args) {       CopyOnWriteArrayList arrList = new CopyOnWriteArrayList();       arrList.add(220);       arrList.add(250);       arrList.add(400);       arrList.add(500);       arrList.add(650);       arrList.add(700);       arrList.add(800);       System.out.println("CopyOnWriteArrayList String Representation = ...

Read More

LongStream range() method in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 574 Views

The range() 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 exclusive of the last element.The syntax is as follows:static LongStream range(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 range() method in Java:Exampleimport java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.range(20L, 25L);   ...

Read More

IntStream sequential() method in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 493 Views

The sequential() method of the IntStream class in Java is used to return a sequential IntStream. The syntax is as follows:IntStream sequential()First, create an IntStream and elements in a range using the range() method:IntStream intStream1 = IntStream.range(11, 21);Now, for a sequential IntStream, use the sequential() method like this:IntStream intStream2 = intStream1.sequential();The following is an example to implement IntStream sequential() method in Java:Exampleimport java.util.*; import java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream intStream1 = IntStream.range(11, 21);       IntStream intStream2 = intStream1.sequential();       intStream2.forEach(System.out::println);    } }Output11 12 13 14 15 16 17 18 19 20

Read More

LongStream min() method in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 177 Views

The min() method of the LongStream class in Java returns an OptionalLong describing the minimum element of this stream, or an empty optional if this stream is empty.The syntax is as follows:OptionalLong min()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 package:import java.util.stream.LongStream;The following is an example to implement LongStream min() method in Java. The isPresent() method of the OptionalLong class returns true if the value is present:Exampleimport java.util.*; import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       ...

Read More

DoubleStream min() method in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 171 Views

The min() method of the DoubleStream class returns an OptionalDouble describing the minimum element of this stream, or an empty OptionalDouble if this stream is empty.The syntax is as follows:OptionalDoublemin()Here, OptionalDouble is a container object which may or may not contain a double valueTo use the DoubleStream class in Java, import the following package:import java.util.stream.DoubleStream;Create a DoubleStream and add elements to the stream:DoubleStream doubleStream = DoubleStream.of(67.9, 89.9, 10.5, 95.8, 49.6);Get the maximum element from the DoubleStream:OptionalDouble res = doubleStream.max();The following is an example to implement DoubleStream min() method in Java:Exampleimport java.util.OptionalDouble; import java.util.stream.DoubleStream; public class Demo {    public static ...

Read More

DoubleStream count() method in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 209 Views

The count() method of the DoubleStream class returns the count of the elements in the stream.The syntax is as follows:long count()To use the DoubleStream class in Java, import the following package:import java.util.stream.DoubleStream;Create DoubleStream and add some elements:DoubleStream doubleStream = DoubleStream.of(50.8, 67.9, 35.7, 23.6, 89.9);Now, get the count of elements in the DoubleStream:long res = doubleStream.count();The following is an example to implement DoubleStream count() method in Java:Exampleimport java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       DoubleStream doubleStream = DoubleStream.of(50.8, 67.9, 35.7, 23.6, 89.9);       long res = doubleStream.count();       System.out.println("Count ...

Read More

IntStream summaryStatistics() method in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 658 Views

The summaryStatistics() method in the IntStream class is used to return summary data about the elements of this stream. The syntax is as follows:IntSummaryStatistics summaryStatistics()Create an IntStream and add some elements:IntStream intStream = IntStream.of(30, 60, 90);Now, get the summary data about the above elements:IntSummaryStatistics details = intStream.summaryStatistics();The following is an example to implement IntStream summaryStatistics() method in Java:Exampleimport java.util.stream.IntStream; import java.util.IntSummaryStatistics; public class Demo {    public static void main(String[] args) {       IntStream intStream = IntStream.of(30, 60, 90);       IntSummaryStatistics details = intStream.summaryStatistics();       System.out.println("Details = "+details);    } }OutputDetails = IntSummaryStatistics{count=3, sum=180, ...

Read More

Java Program to get prime numbers using the Sieve of Eratosthenes algorithm

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 475 Views

To find all prime numbers up to any given limit, use the Sieve of Eratosthenes algorithm. At first we have set the value to be checked −int val = 30;Now, we have taken a boolean array with a length one more than the val −boolean[] isprime = new boolean[val + 1];Loop through val and set numbers as TRUE. Also, set 0 and 1 as false since both these number are not prime −isprime[0] = false; isprime[1] = false;Following is an example showing rest of the steps to get prime numbers using the Sieve of Eratosthenes algorithm −Examplepublic class Demo { ...

Read More

How to convert Integer array list to integer array in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

To convert integer array list to integer array is not a tedious task. First, create an integer array list and add some elements to it −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, assign each value of the integer array list to integer array. We used size() to get the size of the integer array list and placed the same size to the newly created integer array −final int[] arr = new int[arrList.size()]; int index = 0; for (final Integer value: arrList) {    arr[index++] = value; }Exampleimport java.util.ArrayList; public class ...

Read More

Java Program to calculate the area of a triangle using Heron's Formula

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 5K+ Views

Heron’s formula gives the area of a triangle when the length of all three sides are already known.Let’s say we have the following three sides of a triangle −s1 = 15191235.0; s2 = 15191235.0; s3 = 1.01235479;Now, use the Heron’s formulae to find the area −area = (s1+s2+s3)/2.0d; resArea = Math.sqrt(area* (area - s1) * (area - s2) * (area - s3));Examplepublic class Demo {    public static void main(String[] args) {       // sides of a triangle       double s1, s2, s3;       double area, resArea;       // three sides of ...

Read More
Showing 2451–2460 of 5,881 articles
« Prev 1 244 245 246 247 248 589 Next »
Advertisements