Found 2616 Articles for Java

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

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

267 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

AmitDiwan
Updated on 30-Mar-2022 08:13:49

144 Views

In this article, we will understand how to get the middle element of linkedList in a single iteration. 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 −Input linked list: 100 200 330The desired output would be −The middle element of the list is: 200AlgorithmStep 1 - START Step 2 - Declare a LinkedList namely input_list. Declare five node objects namely head, first_node, second_node, ... Read More

Java Program to Implement the queue data structure

AmitDiwan
Updated on 30-Mar-2022 08:01:28

2K+ Views

In this article, we will understand how to implement the queue data structure. A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).Below is a demonstration of the same −Suppose our input is −Input Queue: [150, 300, 450, 600]The desired output would be −After removing an element, the elements of the queue are: [300, 450, 600]AlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Add elements to it using the ‘offer’ method. Step 4 - Display the queue content Step 5 - ... Read More

Java Program to Implement the graph data structure

AmitDiwan
Updated on 30-Mar-2022 07:45:08

821 Views

In this article, we will understand how to implement the graph data structure. we implement the graph data structure we implement graphs in Java using HashMap collection. HashMap elements are in the form of key-value pairs. We can represent the graph adjacency list in a HashMap.Below is a demonstration of the same −Suppose our input is −Number of Vertices: 5 Number of edges: 5The desired output would be −The connections between the nodes of the Graph are: 1 - 2 1 - 3 1 - 4 2 - 4 2 - 5 3 - 4 3 - 5 4 - ... Read More

Java Program to Sort ArrayList of Custom Objects by Property

AmitDiwan
Updated on 30-Mar-2022 07:40:55

584 Views

In this article, we will understand how to sort arrayList of custom objects by property. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.Array lists are 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 −Suppose our input is −The list is defined as Java Scala Python MysqlThe desired output would be −The list after sorting values: Java Mysql Python ScalaAlgorithmStep 1 - START Step 2 - Declare namely ... Read More

Java Program to Perform the inorder tree traversal

AmitDiwan
Updated on 30-Mar-2022 07:39:14

641 Views

In this article, we will understand how to perform the inorder tree traversal. In InOrder traversal, each node is processed between subtrees.In simpler words, visit left subtree, node and then right subtree.Below is a demonstration of the same −Suppose our input is −Run the programThe desired output would be −The In-Order traversal of the tree_object is: 5->12->6->1->9->AlgorithmStep 1 - START Step 2 - A class with the data specifications is previously defined. Step 3 - Create a new instance of the class. Step 4 - Initialize the instance with relevant values. Step 5 - Invoke the method to perform inorder ... Read More

Java Program to Sort a Map By Values

AmitDiwan
Updated on 30-Mar-2022 07:33:04

344 Views

In this article, we will understand how to sort a map by values. Java HashMap is a hash tablebased implementation of Java's Map interface. It is a collection of key-value pairs.Below is a demonstration of the same −Suppose our input is −Input HashMap: Key = Java, Value = 45 Key = Scala, Value = 20 Key = Mysql, Value = 11 Key = Python, Value = 75The desired output would be −The HashMap after sorting is: Key = Mysql, Value = 11 Key = Scala, Value = 20 Key = Java, Value = 45 Key = Python, Value = 75AlgorithmStep ... Read More

Java Program to Merge two lists

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

357 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

AmitDiwan
Updated on 30-Mar-2022 07:24:34

636 Views

In this article, we will understand how to initialize a list. 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 −Run the programThe desired output would be −Initializing an integer list The elements of the integer list are: [25, 60] Initializing a string list The elements of the string list are: [Java, Program]AlgorithmStep 1 - START Step 2 - Declare an integer list ... Read More

Java Program to Rotate Elements of a List

AmitDiwan
Updated on 30-Mar-2022 07:24:09

327 Views

In this article, we will understand how to rotate elements of a list. 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.Below is a demonstration of the same −Suppose our input is −Input list: [100, 150, 200, 250, 300]The desired output would be −The list after one rotation: [150, 200, 250, 300, 100]AlgorithmStep 1 - ... Read More

Advertisements