Programming Articles

Page 1535 of 2547

Deriving a Class in Java

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 4K+ Views

A class can be derived from the base class in Java by using the extends keyword. This keyword is basically used to indicate that a new class is derived from the base class using inheritance. It can also be said that it is used to extend the functionality of the class.A program that demonstrates this is given as follows:Exampleclass A {    void funcA() {       System.out.println("This is class A");    } } class B extends A {    void funcB() {       System.out.println("This is class B");    } } public class Demo {    public ...

Read More

What is overloading? What happens if we overload a main method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 7K+ Views

Overloading is one of the mechanisms to achieve polymorphism. If a class contains two methods with the same name and different parameters, whenever you call this method the method body will be bound with the method call based on the parameters.ExampleIn the following Java program, the Calculator class has two methods with name addition. The only difference between them is that one contains 3 parameters and the other contains 2 parameters.Here, we can call the addition method by passing two integers or three integers. Based on the number of integer values we pass, the respective method will be executed.public class ...

Read More

LongStream mapToInt() method in Java

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 1K+ Views

The mapToInt() method returns an IntStream consisting of the results of applying the given function to the elements of this stream.The syntax is as followsmapToInt(LongToIntFunction mapper)Here, the parameter mapper is the stateless function applied to each element.Declare LongStream and add some elementsLongStream longStream = LongStream.of(1000L, 13000L, 18000L);Now, use the IntStream and mapToInt()IntStream intStream = longStream.mapToInt(val -> (int) val);The following is an example to implement LongStream mapToInt() in JavaExampleimport java.util.*; import java.util.stream.IntStream; import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.of(1000L, 13000L, 18000L);       IntStream intStream = longStream.mapToInt(val ...

Read More

Creating a Multilevel Inheritance Hierarchy in Java

Jai Janardhan
Jai Janardhan
Updated on 11-Mar-2026 13K+ Views

Inheritance involves an object acquiring the properties and behaviour of another object. So basically, using inheritance can extend the functionality of the class by creating a new class that builds on the previous class by inheriting it.Multilevel inheritance is when a class inherits a class which inherits another class. An example of this is class C inherits class B and class B in turn inherits class A.A program that demonstrates a multilevel inheritance hierarchy in Java is given as follows:Exampleclass A {    void funcA() {       System.out.println("This is class A");    } } class B extends A ...

Read More

DoubleStream allMatch() method in Java

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 170 Views

The allMatch() method in the DoubleStream class returns whether all elements of this stream match the provided predicate.The syntax is as follows −boolean allMatch(DoublePredicate predicate)Here, the argument predicate is a stateless predicate to apply to elements of this stream. The DoublePredicate is a predicate of one double-valued argument.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(15.8, 28.7, 35.7, 48.1, 78.9);Now, check whether the condition is TRUE for elements of the stream −boolean res = doubleStream.allMatch(num -> num > 10); The following is an example to implement DoubleStream ...

Read More

Get the first element from a Sorted Set in Java

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

To create a Sorted Set, firstly create a Set.Set s = new HashSet();Add elements to the above set.int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89}; Set s = new HashSet(); try { for(int i = 0; i < 5; i++) { s.add(a[i]); }After that, use TreeSet class to sort.TreeSet sorted = new TreeSet(s);Get the first element, using the first() method −System.out.println("First element of the sorted set = "+ (Integer)sorted.first());The following is the code to get the first element from a Sorted Set ...

Read More

Collectors minBy() method in Java 8

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

The minBy() method of the Collectors class in Java 8 returns a Collector that produces the minimum element according to a given Comparator, described as an OptionalThe syntax is as followsstatic Collector

Read More

C++ Program to Check if a Binary Tree is a BST

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

Binary Search Tree is a binary tree data structure in which we have 3 properties −The left subtree of a binary search tree of a node contains only nodes with keys lesser than the node’s key.The right subtree of a binary search tree node contains only nodes with keys greater than the node’s key.The left and right of a subtree each must also be a binary search tree.AlgorithmBegin    function BSTUtill()       If node is equals to NULL then          Return 1.       If data of node is less than minimum or greater ...

Read More

Insert an element to List using ListIterator in Java

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

Let us first create an ArrayList −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, create a ListIterator from the above ArrayList and insert more elements −ListIterator < Integer > iterator = arrList.listIterator(); iterator.add(1000); iterator.add(2000); iterator.add(3000);Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add(100);       arrList.add(200);       arrList.add(300);       arrList.add(400);       arrList.add(500);       arrList.add(600);       arrList.add(700);       arrList.add(800);       ListIteratoriterator = arrList.listIterator();       iterator.add(1000);       iterator.add(2000);       iterator.add(3000);       for (Integer i: arrList) {          System.out.println(i);       }    } }output1000 2000 3000 100 200 300 400 500 600 700 800

Read More

How to write a class inside an interface in Java?

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

Defining a class within an interface is allowed in Java. If the methods of an interface accept a class as an argument and the class is not used elsewhere, in such cases we can define a class inside an interface.ExampleIn the following example we have an interface with name CarRentalServices and this interface has two methods that accepts an object of the class Car as an argument. Within this interface we have the class Car.interface CarRentalServices {    void lendCar(Car c);    void collectCar(Car c);    public class Car{       int carId;       String carModel;   ...

Read More
Showing 15341–15350 of 25,466 articles
Advertisements