Found 33676 Articles for Programming

Java Program to Convert the LinkedList into an Array and vice versa

AmitDiwan
Updated on 30-Mar-2022 08:20:05

404 Views

In this article, we will understand how to convert the linked list into an array and vice versa. The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.Below is a demonstration of the same −Suppose our input is −The list is defined as: [Java, Python, Scala, Mysql]The desired output would be −The result array is: Java Python Scala MysqlAlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 ... Read More

Java Program to Get the middle element of LinkedList in a single iteration

Aishwarya Naglot
Updated on 04-Aug-2025 15:50:14

261 Views

The java.util.LinkedList class represents a linked list in Java, specifically a doubly-linked list, i.e., we can traverse through the list either from the beginning or from the end, whichever is closer to the specified index. We can create and perform various operations on a linked list using the methods provided by this class.Retrieving the Middle Element of a LinkedList  In this article, we will understand how to find the middle element of a LinkedList using Java. We will use the following ways to do so: Using a While Loop Using size() ... Read More

Java Program to Implement the queue data structure

Aishwarya Naglot
Updated on 05-Aug-2025 16:36:12

3K+ Views

A Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In, First Out (FIFO). In a queue, the element added first will be removed first. The end where elements are added is called the rear, and the end where elements are removed is called the front. Scenario 1 Let us see an example, scenario: Input: [150, 300, 450, 600] After removing an element from the queue: Output: [300, 450, 600] The following are the ways to implement a queue in Java: Using Array ... Read More

Java Program to Sort ArrayList of Custom Objects by Property

Alshifa Hasnain
Updated on 22-Jan-2025 18:37:57

707 Views

In this article, we will learn to sort ArrayList of custom objects by property in Java. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Problem Statement ArrayList is created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. Below is a demonstration of the same − Input − The list is defined as Java Scala Python Mysql Output  − The list after sorting values: Java Mysql Python Scala ... Read More

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

502 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

476 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

960 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

469 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

Advertisements