Articles on Trending Technologies

Technical articles with clear explanations and examples

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 352 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 408 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

Get last entry from NavigableMap in Java

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

To display the last entry from NavigableMap in Java, use the lastEntry() method.Let us first create NavigableMap.NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); n.put("C", 868); n.put("D", 988); n.put("E", 686); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);Get the last entry now.n.lastEntry()The following is an example to get the last entry from NavigableMap.Exampleimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); ...

Read More

Variable in subclass hides the variable in the super class in Java

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

Sometimes the variable in the subclass may hide the variable in the super class. In that case, the parent class variable can be referred to using the super keyword in Java.A program that demonstrates this is given as follows:Exampleclass A {    char ch; } class B extends A {    char ch;    B(char val1, char val2) {       super.ch = val1;       ch = val2;    }    void display() {       System.out.println("In Superclass, ch = " + super.ch);       System.out.println("In subclass, ch = " + ch);    } } ...

Read More

Using run-time polymorphism in Java

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

A single action can be performed in multiple ways using the concept of polymorphism. Run-time polymorphism can be performed by method overriding. The overridden method in this is resolved at compile time.A program that demonstrates run-time polymorphism in Java is given as follows:Exampleclass Animal {    void sound() {       System.out.println("Animal makes sound");    } } class Cat extends Animal {    void sound() {       System.out.println("Cat Meows");    } } class Dog extends Animal {    void sound() {       System.out.println("Dog Barks");    } } class Cow extends Animal {    void sound() ...

Read More

Collectors counting() method in Java 8

Nishtha Thakur
Nishtha Thakur
Updated on 11-Mar-2026 1K+ Views

The counting() method of the Java 8 Collectors class returns a Collector accepting elements of type T that counts the number of input elements.The syntax is as follows −static Collector counting()Here, the parameter −T − type of input elementsLong − This class value of the primitive type long in an objectTo work with Collectors class in Java, import the following package −import java.util.stream.Collectors;The following is an example to implement counting() method in Java 8 −Exampleimport java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("25", "50", "75", ...

Read More

Generate 10 random four-digit numbers in Java

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

To generated random integer, use the Random class with nextInt. At first, create a Random object −Random rand = new Random();The Random above is a random number generator. Now, pick the random numbers one by one. We want 10 random four-digit numbers, therefore loop it until i = 1 to 10 −for (int i = 1; i

Read More

Java Program to generate custom random number -1 or 1

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

To generate custom random number 1 or -1, you need to use nextBoolean(). At first take a loop and create a Random object on each iteration −for (int i = 0; i < 5; i++) {    Random rand = new Random(); }Now, use nextBoolean() to generate 1 on TRUE condition, ekse -1 −for (int i = 0; i < 5; i++) {    Random rand = new Random();    if (rand.nextBoolean())       System.out.println(1);    else       System.out.println(-1); }Exampleimport java.util.Random; public class Demo {    public static void main(String[] args) {       for (int ...

Read More

Can we write an interface without any methods in java?

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

Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces.A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.ExampleConsider the following example, here we have class with name Student which implements the marking interface Cloneable. In the main method we are trying to create an object of the Student class and clone it using the clone() method.import java.util.Scanner; public class Student implements Cloneable {    int age;    String name;    public Student ...

Read More
Showing 29331–29340 of 61,297 articles
Advertisements