Java Articles

Page 182 of 450

Java program to sort a HashMap by Keys and Values

Way2Class
Way2Class
Updated on 07-Nov-2024 2K+ Views

In this article, we will learn the procedures for sorting a HashMap in Java by its keys and values, as well as examine the performance implications associated with each technique. HashMap, a frequently employed data structure, enables programmers to store key-value pairs. This data structure is an exceedingly efficient method for storing data and allows for swift value retrieval based on keys. However, on occasion, it may become necessary to arrange the HashMap by its keys or values.  Different Approaches of Sorting the HashMap Following are the different approaches to sorting a HashMap by Keys and Values − ...

Read More

Java program to swap two numbers using XOR operator

Arjun Thakur
Arjun Thakur
Updated on 05-Nov-2024 879 Views

In this article, we will learn how to swap two numbers using the XOR bitwise operator in Java. The XOR operator is a powerful tool that allows you to perform bitwise operations, and one of its interesting properties is that it can be used to swap two variables without using a temporary variable. This method is efficient and can be used when you need a fast swapping mechanism. Problem StatementGiven two integers, write a Java program to swap their values using the XOR operator.Input Two integers are provided by the user.Output The values of the two integers after swapping. ...

Read More

Java program to swap two numbers using XOR operator

Arjun Thakur
Arjun Thakur
Updated on 05-Nov-2024 879 Views

In this article, we will learn how to swap two numbers using the XOR bitwise operator in Java. The XOR operator is a powerful tool that allows you to perform bitwise operations, and one of its interesting properties is that it can be used to swap two variables without using a temporary variable. This method is efficient and can be used when you need a fast swapping mechanism. Problem StatementGiven two integers, write a Java program to swap their values using the XOR operator.Input Two integers are provided by the user.Output The values of the two integers after swapping. ...

Read More

Java program to generate a random number from an array

karthikeya Boyini
karthikeya Boyini
Updated on 04-Nov-2024 6K+ Views

In this article, we will learn how to generate a random number from an array of integers in Java by using Random class. The Random class provides methods to generate random numbers, and we will use the nextInt(int bound) method to get a random index within the bounds of our array length. Problem Statement Given an array of integers, we need to randomly select and display one element from the array using Java. Input arr = { 10, 30, 45, 60, 78, 99, 120, 140, 180, 200}; Output Random number from the array = 30 Steps to generate ...

Read More

Java program to get the prime numbers with BigInteger type

Nancy Den
Nancy Den
Updated on 04-Nov-2024 742 Views

In this article, we will learn how to generate prime numbers using the BigInteger class in Java. A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. Here, we have the BigInteger type, which has operations for modular arithmetic, GCD calculation, primality testing, prime generation, etc. Steps to get the prime numbers with BigInteger type Following are the steps to get the prime numbers with BigInteger type − First, we will import the BigInteger from the java.math package. ...

Read More

Java program to subtract week from current date

Samual Sam
Samual Sam
Updated on 04-Nov-2024 986 Views

In this article, we will learn to subtract the week from the current date using Java. This is useful when you need to manipulate dates, such as calculating dates from previous weeks for scheduling or tracking purposes. We'll use two approaches: using the Calendar class and the LocalDate class, showcasing how to easily adjust dates. Problem Statement Write a program in Java to subtract a specified number of weeks from the current date and display the updated date − Input Run the program Output Current Date = Mon Nov 04 09:41:18 IST 2024 Updated Date = Mon Oct 21 09:41:18 ...

Read More

Delete middle element of a stack in Java

Sourav Sarkar
Sourav Sarkar
Updated on 01-Nov-2024 565 Views

In order to delete the middle element of a stack, then first we need to remove the elements above the middle element. After removing the middle element, we restore the elements above it to maintain the original sequence. We can achieve this using a recursive approach, as explained step-by-step below. We cannot delete the middle element of the stack directly because, according to the stack property, we can perform only push and pop operations on the top of the stack. Steps to Delete the Middle Element of a Stack Step 1: Determine the Position of the Middle Element, First ...

Read More

Minimum number of jumps to reach end using Java

Pushpa kumari
Pushpa kumari
Updated on 01-Nov-2024 357 Views

In this article, we will learn how to solve the "Minimum Number of Jumps to Reach the End" problem using Java. Let's break it down step-by-step. The idea is to find the smallest number of jumps needed to get from the start to the end of an array. Each element in the array represents the maximum number of steps you can take from that position. Problem StatementGiven an array arr[], where each element represents the maximum number of steps you can move forward from that position, the goal is to start from the beginning of the array and ...

Read More

Java program to put value to a Properties list

karthikeya Boyini
karthikeya Boyini
Updated on 30-Oct-2024 480 Views

In this article, we will learn how to use the Properties class in Java to store and display key-value pairs. The Properties class is useful for handling configuration settings or storing data in a structured way, where both keys and values are strings. By using the put() method, we can easily add values to a property list and later retrieve or display these key-value pairs. This approach provides a simple and efficient way to manage data in a key-value format in Java applications. Problem Statement A properties list contains country-year pairs. Write a Java program to store values ...

Read More

Java program to sort the elements of the stack in descending order

AYUSH MISHRA
AYUSH MISHRA
Updated on 30-Oct-2024 11K+ Views

In this article, we will learn to sort the elements of the stack in descending order. A stack is a data structure that works on the LIFO (Last In First Out) principle which means that the last added item is removed first. One real-life example of a stack is browser history where the last used website appears first. In this article, we are going to discuss, how we can sort the element of stack in descending order in Java.  Problem Statement In the given problem, we have a stack of unsorted integer elements, which we have to sort in descending ...

Read More
Showing 1811–1820 of 4,498 articles
« Prev 1 180 181 182 183 184 450 Next »
Advertisements