Programming Articles

Page 1518 of 2547

DoubleStream.Builder accept() method in Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 187 Views

The accept() method of the DoubleStream class in Java adds an element to the stream being built.The syntax is as follows −void accept(double ele)Here, ele is the element to be inserted into the stream.To use the DoubleStream.Builder class in Java, import the following package −import java.util.stream.DoubleStream;The following is an example to implement DoubleStream.Builder() accept() method in Java −Exampleimport java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       DoubleStream.Builder builder = DoubleStream.builder();       builder.accept(12.9);       builder.accept(25.6);       builder.accept(35.8);       builder.accept(43.9);       builder.accept(56.3);     ...

Read More

The addAll() method of Java AbstractSequentialList class

Daniol Thomas
Daniol Thomas
Updated on 11-Mar-2026 164 Views

The addAll() method of the AbstractSequentialList class inserts all the elements in the specified collection into this list at the specified position. Set the specified position as the parameter.The syntax is as follows:boolean addAll(int index, Collection

Read More

How to track the order of insertion using Java collections?

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

To track the order of insertion, you can use Map.Entry() in case of a Map. Let’s say we have the following LinkedHashMap −Mapmap = new LinkedHashMap(); map.put("Jack", 0); map.put("Tim", 1); map.put("David", 2); map.put("Tom", 3); map.put("Kevin", 4); map.put("Jeff", 5);Now, loop through Map.Entry and get the order of insertion correctly with Key and Value −for (Map.Entryentry: map.entrySet()) {    System.out.println(entry.getKey() + " => " + entry.getValue()); }Exampleimport java.util.LinkedHashMap; import java.util.Map; public class Demo {    public static void main(String[] args) {       Mapmap = new LinkedHashMap();       map.put("Jack", 0);       map.put("Tim", 1);       map.put("David", ...

Read More

Class declaration with a method that has a parameter in Java

Arushi
Arushi
Updated on 11-Mar-2026 256 Views

A class declaration can contain a method that has a parameter in Java. A program that demonstrates this is given as follows:Exampleclass Message {    public void messagePrint(String msg) {       System.out.println("The message is: " + msg);    } } public class Demo { public static void main(String args[]) { Message m = new Message(); m.messagePrint("Java is fun"); } }OutputThe message is: Java is funNow let us understand the above program.The Message class is created with a single member function ...

Read More

How to use null value as key in Java HashMap

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

Yes, you can set null as key in Java HashMap. For this, let’s first create a HashMap with key and value pair −Mapmap = new HashMap(); map.put("Football", "A"); map.put("Squash", "B"); map.put("Cricket", "C"); map.put("Hockey", "D"); map.put("Rugby", "E");Now, let’s add null value as key −map.put(null, "H");You can try to get the value for key as “null” −map.get(null);Exampleimport java.util.HashMap; import java.util.Map; public class Demo {    public static final void main(String[] args) {       Mapmap = new HashMap();       map.put("Football", "A");       map.put("Squash", "B");       map.put("Cricket", "C");       map.put("Hockey", "D");       ...

Read More

The iterator() method of CopyOnWriteArrayList in Java

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

The iterator() method is used to return an iterator over the elements in this list.The syntax is as followsIterator iterator()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 iterator() method in JavaExampleimport java.util.Arrays; import java.util.Iterator; import java.util.concurrent.CopyOnWriteArrayList; public class Demo {    public static void main(String[] args) {       CopyOnWriteArrayList       arrList = new CopyOnWriteArrayList();       arrList.add(50);       arrList.add(90);       arrList.add(150);       arrList.add(200);       arrList.add(350);       arrList.add(500);       arrList.add(650);   ...

Read More

Duplicating Objects using a Constructor in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 456 Views

A copy constructor can be used to duplicate an object in Java. The copy constructor takes a single parameter i.e. the object of the same class that is to be copied. However, the copy constructor can only be explicitly created by the programmer as there is no default copy constructor provided by Java.A program that demonstrates this is given as follows −Exampleclass NumberValue {    private int num;    public NumberValue(int n) {       num = n;    }    public NumberValue(NumberValue obj) {       num = obj.num;    }    public void display() {   ...

Read More

DoubleStream peek() method in Java

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

The syntax is as followsDoubleStream peek(DoubleConsumer action)Here, DoubleConsumer is an operation that accepts a single double-valued argument and returns no result.To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;The following is an example to implement DoubleStream peek() method in JavaExampleimport java.util.*; import java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       DoubleStream doubleStream = DoubleStream.of(28.7, 35.6, 48.3, 69.8, 75.8, 80.5, 90.8);       System.out.println("Elements in the stream...");       long num = doubleStream.peek(System.out::println).count();       System.out.println("Number of elements in the stream = " + num);    } }OutputElements ...

Read More

IntStream sequential() method in Java

Nancy Den
Nancy Den
Updated on 11-Mar-2026 499 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

How to add elements to AbstractSequentialList class at a specific position in Java?

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

The AbstractSequentialList class has the add() method to add an element to the specific position.The syntax is as followsadd(int index, E ele)Here, index is where the element is to be inserted. The ele is the element to be inserted.To work with the AbstractSequentialList class in Java, you need to import the following packageimport java.util.AbstractSequentialList;The following is an example to implement AbstractSequentialList add() method in JavaExampleimport java.util.LinkedList; import java.util.AbstractSequentialList; public class Demo {    public static void main(String[] args) {       AbstractSequentialList absSequential = new LinkedList();       absSequential.add(0, 50);       absSequential.add(1, 30);       ...

Read More
Showing 15171–15180 of 25,466 articles
Advertisements