Found 7442 Articles for Java

Java program to perform the inorder tree traversal

Aishwarya Naglot
Updated on 24-Jun-2025 13:15:51

1K+ Views

In-order traversal processes each node by visiting the left subtree first, then the node,  and finally the right subtree. This is particularly useful for certain types of tree structures, such as binary search trees, where in-order traversal will print the values in sorted order. In this article, we will explore how to perform an in-order traversal of a binary tree. Following are the ways to implement in-order traversal in Java: Using Recursion Using Stack (Iteration) Inorder Tree Traversal using Recursion To traverse a tree in order using recursion, we need ... Read More

Java Program to Sort a Map By Values

Aishwarya Naglot
Updated on 01-Sep-2025 13:28:52

500 Views

Java HashMap is a hash table-based implementation of Java's Map interface. It is a collection of key-value pairs.In this article, we will learn to sort a map by values in Java. Following are the ways to sort a map by values in Java: Using Comparator Using Streams Using Comparator We can sort a map by values using a custom comparator. The comparator will compare the values of the map entries and sort them accordingly. Steps to sort a map by values using Comparator: Step 1: Create a HashMap and populate it with some key-value pairs. Step 2: ... Read More

Java Program to Merge two lists

AmitDiwan
Updated on 30-Mar-2022 07:32:25

474 Views

In this article, we will understand how to merge two lists. A list is an ordered collection that allows us to store and access elements sequentially. It contains the index-based methods to insert, update, delete and search the elements. It can also have the duplicate elements.Below is a demonstration of the same −Suppose our input is −First list: [45, 60, 95] Second list: [105, 120]The desired output would be −The list after merging the two lists: [45, 60, 95, 105, 120]AlgorithmStep 1 - START Step 2 - Declare three integer lists namely input_list_1, input_list_2 and result_list. Step 3 - Define ... Read More

Java Program to Initialize a List

Alshifa Hasnain
Updated on 16-Jan-2025 20:22:23

955 Views

In this article, we will learn to initialize a list in Java. A list is an ordered collection that allows us to store and access elements sequentially. It contains index-based methods to insert, update, delete, and search the elements. It can also have duplicate elements. Problem Statement Given a scenario where you need to create and initialize a list in Java, the goal is to understand and implement various approaches to initialize lists effectively.  Input   Initialize an empty list and add elements − "Apple", "Banana", "Cherry" Output  ["Apple", "Banana", "Cherry"] Different Approaches Following are the different approaches to Initialize ... Read More

Java Program to Rotate Elements of a List

Aishwarya Naglot
Updated on 21-Aug-2025 12:31:09

466 Views

The List extends Collection and declares the behavior of a collection that stores a sequence of elements. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. In this article, we will learn to rotate elements of a list in Java. Rotating a list means shifting the elements of the list either to the left or to the right. Following are the ways to rotate elements of a list in Java: ... Read More

Java Program To Find all the Subsets of a String

Shriansh Kumar
Updated on 01-Aug-2024 10:58:32

2K+ Views

In this article, we will understand how to find all the subsets of a string. In Java, String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). A part or a subset of string is called substring. Example Scenario: Input: string = JVM; Output: The subsets of the string are: J, JV, JVM, V, VM, M Finding all Subsets of a String in Java Use the following approaches to find all subsets of a String in Java − Using nested for Loop ... Read More

Java program to divide a string in \\'N\\' equal parts

Alshifa Hasnain
Updated on 23-Oct-2024 17:32:12

837 Views

In this article, we will understand how to divide a string into 'N' equal parts using Java. If the string's length is divisible by N, the string will be split evenly into N parts; otherwise, a message will be displayed indicating that the string cannot be divided into N equal parts. This is demonstrated using both a simple main method and an encapsulated approach. Problem Statement Write a program in Java to divide a string into 'N' equal parts. Below is a demonstration of the same − Input Input string: Java Program is fun! Output The length of the string ... Read More

Java program to get minimum and maximum from a list

Aishwarya Naglot
Updated on 24-Jun-2025 12:51:19

1K+ Views

Minimum and Maximum Values of a ListA list is an ordered collection that allows us to store and access elements sequentially. It contains index-based methods to insert, update, delete, and search the elements. It can also have duplicate elements. The following are the ways to get the minimum and maximum from a list in Java: Using Loops Using Collections Using Streams Minimum and Maximum of a List Using LoopsWe can find the minimum and maximum values in a list by iterating through the elements using loops. We will initialize two variables to save the minimum and maximum values, and ... Read More

Java program to check if a string is empty or null

AmitDiwan
Updated on 08-Nov-2024 22:31:26

992 Views

In this article, we will understand how to check if a string is empty or null in Java. The string is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). We’ll start by learning to detect these cases directly within the main method, and then we’ll see how to achieve the same result using encapsulation for better code organization and reusability. Problem Statement Write a program in Java to check if a string is empty or null. Below is a demonstration of the same − Input Input string: null Output The string is a null ... Read More

Java program to remove all whitespaces from a string

AmitDiwan
Updated on 07-Nov-2024 01:05:37

392 Views

In this article, we will understand how to remove all whitespaces from a string in Java. The String class in Java represents a sequence of characters enclosed in double-quotes. Removing whitespaces from a string is a common operation, especially when processing user input or cleaning data. The replaceAll() method is used with a regular expression to match whitespace characters and replace them with an empty string. Problem StatementGiven a string with whitespaces, write a Java program to remove all whitespaces from the string.Input Initial String = "Java programming is fun to learn."Output String after removing white spaces = ... Read More

Advertisements