Programming Articles

Page 1517 of 2547

What is the IntStream.Builder accept() method in Java

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 193 Views

Insert an element into IntStream using the IntStream.Builder accept() method. It adds element to the stream being built.The syntax is as follows:void accept(int t)Here, parameter t is the input argument.The elements are inserted as shown below in the stream:builder.accept(10); builder.accept(15); builder.accept(25); builder.accept(39); builder.accept(45);The following is an example to implement IntStream.Builder accept() method in Java:Exampleimport java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream.Builder builder = IntStream.builder();       System.out.println("Elements of the stream...");       builder.accept(10);       builder.accept(15);       builder.accept(25);       builder.accept(39);       builder.accept(45); ...

Read More

IntStream.Builder add() method in Java

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 200 Views

To insert element into the stream, you need to use the add() method of the IntStream.Builder.The syntax is as follows:default IntStream.Builder add(int t)Here, parameter t is the element to be inserted.Declare IntStream.Builder:IntStream.Builder builder = IntStream.builder();Add some elements to the Builder using add() method:builder.add(10); builder.add(25); builder.add(33); builder.add(42);The following is an example to implement IntStream.Builder add() method in JavaExampleimport java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream.Builder builder = IntStream.builder();       System.out.println("Elements in the stream...");       builder.add(10);       builder.add(25);       builder.add(33);       builder.add(42);   ...

Read More

Provider getService() method in Java

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

The service that can describe the implementation of a Provider in regards to any algorithm is obtained using the method getService() in the class java.security.Provider. This method requires two parameters i.e. the service type required and the algorithm name of the required service.A program that demonstrates this is given as follows −Exampleimport java.security.*; import java.util.*; public class Demo {    public static void main(String[] argv) throws Exception {       try {          Signature sign = Signature.getInstance("SHA256withDSA");          Provider p = sign.getProvider();          Provider.Service s = p.getService("Signature", sign.getAlgorithm());     ...

Read More

Java Program to convert positive int to negative and negative to positive

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

To convert positive int to negative and vice-versa, use the Bitwise Complement Operator.Let us first initialize a positive int −int positiveVal = 200;Now, let us convert it to negative −int negativeVal = (~(positiveVal - 1));Now, let’s say we have the following negative int −int negativeVal = -300;The following will convert the negative to positive int −positiveVal = ~(negativeVal - 1);Examplepublic class Demo {    public static void main(String[] args) throws java.lang.Exception {       int positiveVal = 100;       int negativeVal = (~(positiveVal - 1));       System.out.println("Result: Positive value converted to Negative = "+negativeVal);   ...

Read More

StringJoiner merge() method in Java 8

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 373 Views

The merge() method of the StringJoiner class in Java 8 is used to merge the contents of the StringJoiner str, which is passed as a parameter. The content gets added as the next element.The syntax is as follows:public StringJoiner merge(StringJoiner str)Here, str is the StringJoiner content to be merged.To work with the StringJoiner in Java 8, import the following package:import java.util.StringJoiner;The following is an example to implement StringJoiner merge() method in Java:Exampleimport java.util.StringJoiner; public class Demo {    public static void main(String[] args) {       // StringJoiner 1       StringJoiner strJoin1 = new StringJoiner(" ");   ...

Read More

Java Program to display a prime number less than the given number

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

Let’s say the value you have set is 20 and you have to display a prime number less than this value i.e. 19 in this case.The following is an example that displays a prime number less than the given number −Examplepublic class Demo {    public static void main(String[] args) {       int val = 20;       boolean[] isprime = new boolean[val + 1];       for (int i = 0; i

Read More

Class declaration with one method in Java

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 425 Views

A class declaration can contain a single method. A program that demonstrates this is given as follows:Exampleclass Message {    public void messagePrint() {       System.out.println("This is a class with a single method");    } } public class Demo {    public static void main(String args[]) {       Message m = new Message();       m.messagePrint();    } }OutputThis is a class with a single methodNow let us understand the above program.The Message class is created with a single member function messagePrint(). A code snippet which demonstrates this is as follows −class Message {   ...

Read More

What uses are there for "placement new" in C++?

Vrundesha Joshi
Vrundesha Joshi
Updated on 11-Mar-2026 270 Views

In this section we will see what is the placement new operator in C++. This placement new is another variation of new operator. The normal new operator performs two things. It allocates memory, and then constructs an object in allocated memory.The new operator allocates memory in the heap section and constructs objects there. But for the placement new operator, it constructs object at the given address. To deallocate memory, we can use delete keyword if the memory is allocated using new operator. But for placement new there is no placement delete feature.So in a nutshell, placement new allows you to ...

Read More

The size() method of Java AbstractCollection class

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

The size() method of the AbstractCollection class returns the numbers of elements in the collection. The method returns Integer.MAX_VALUE if the total number of elemnts in the collection exceeds the Interger.MAX_VALUE.The syntax is as follows:public abstract int size()To work with AbstractCollection class in Java, import the following package:import java.util.AbstractCollection;The following is an example to implement AbstractCollection size() method in Java:Exampleimport java.util.ArrayList; import java.util.AbstractCollection; public class Demo {    public static void main(String[] args) {       AbstractCollection absCollection = new ArrayList();       absCollection.add("Laptop");       absCollection.add("Tablet");       absCollection.add("Mobile");       absCollection.add("E-Book Reader");   ...

Read More

Why variables are declared final in Java

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 1K+ Views

A variable cannot be modified after it is declared as final. In other words, a final variable is constant. So, a final variable must be initialized and an error occurs if there is any attempt to change the value.A program that demonstrates a final variable in Java is given as follows −Examplepublic class Demo {    public static void main(String[] args) {       final double PI = 3.141592653589793;       System.out.println("The value of pi is: " + PI);    } }OutputThe value of pi is: 3.141592653589793Now let us understand the above program.In the main() method in class ...

Read More
Showing 15161–15170 of 25,466 articles
Advertisements