Programming Articles

Page 1528 of 2547

LongStream.Builder build() method in Java

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 167 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 JavaExampleimport 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

Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 643 Views

A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Unchecked to checkedWhen a method in superclass throws an unchecked exception the method the subclass method overriding cannot throw ...

Read More

NavigableSet Class ceiling() method in Java

George John
George John
Updated on 11-Mar-2026 187 Views

The ceiling() method returns the least element greater than or equal to the given element i.e. 30 hereceiling(30)The following is an example to implement the ceiling method in JavaExampleimport java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100); System.out.println("Returned Value = " + set.ceiling(30)); } }OutputReturned Value = 40

Read More

IntStream parallel() method in Java

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

The parallel() method of the IntStream class in Java returns an equivalent parallel stream. The method may return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel.The syntax is as follows:IntStream parallel()Create an IntStream and you can use the range() method as well to set the range of elements:IntStream intStream = IntStream.range(20, 35);Now, use the parallel() method:intStream.parallel()The following is an example to implement IntStream parallel() method in JavaExampleimport java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream intStream = IntStream.range(20, 35);     ...

Read More

Insert all elements of other Collection to Specified Index of Java ArrayList

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 412 Views

The elements of a Collection can be inserted at the specified index of the ArrayList using the method java.util.ArrayList.addAll(). This method has two parameters i.e. the specific index at which to start inserting the Collection elements in the ArrayList and the Collection itself.If there is an element already present at the index specified by ArrayList.addAll() then that element and all subsequent elements are shifted to the right to make the space for the Collection elements in the ArrayList.A program that demonstrates this is given as follow.Exampleimport java.util.ArrayList; import java.util.Vector; public class Demo {    public static void main(String[] args) { ...

Read More

IntStream average() method in Java

George John
George John
Updated on 11-Mar-2026 11K+ 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

Modify the value associated with a given key in Java HashMap

Samual Sam
Samual Sam
Updated on 11-Mar-2026 368 Views

First, create a HashMap and add elements to it −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Now, to modify the value associated with a given key, use the put() method. Here, we are modifying the value for the key “Frames” −hm.put("Frames", "900");The following is an example to modify the value associated with a given key −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // ...

Read More

Create a TreeMap in Java and add key-value pairs

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

A TreeMap cannot contain duplicate keys. TreeMap cannot contain the null key. However, It can have null values.Let us first see how to create a TreeMap −TreeMap m = new TreeMap();Add some elements in the form of key-value pairs −m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");The following is an example to create a TreeMap and add key-value pairs −Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "India");       m.put(2, "US");       m.put(3, "Australia");       ...

Read More

Display File class constants in Java

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

The java.io.File class has display constants File.separatorChar and File.pathSeparatorChar mainly. The File.separatorChar is ‘/’ and the File.pathSeparatorChar is ‘:’ for Unix.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          System.out.println("File.pathSeparatorChar = " + File.pathSeparatorChar);          System.out.println("File.separatorChar = " + File.separatorChar);       } catch(Exception e) {          e.printStackTrace();       }    } }The output of the above program is as follows −OutputFile.pathSeparatorChar = : File.separatorChar = /Now let us ...

Read More

LongStream parallel() method in Java

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

The parallel() method of the LongStream class in Java returns an equivalent stream that is parallel.The syntax is as follows:LongStream parallel()To use the LongStream class in Java, import the following packageExampleimport java.util.stream.LongStream;The following is an example to implement LongStream parallel() method in Java:import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.range(10L, 20L);       System.out.println("Parallel LongStream = ");       longStream.parallel().forEach(System.out::println);    } }outputParallel LongStream = 16 15 10 17 11 13 12 19 18 14

Read More
Showing 15271–15280 of 25,466 articles
Advertisements