In this article, we will learn how to convert the local time to GMT (Greenwich Mean Time) in Java. We need this conversion when we want to standardize time across different time zones or when working with systems that require GMT. We will cover the following methods to convert local time to GMT: Using ZonedDateTime class Using Calendar Class Using Date Class Using ZonedDateTime Class ZonedDateTime class was introduced in Java 8, which is used for converting local time to GMT. We can use the ... Read More
List Interface is a collection in Java that is used for storing elements in a sequence. It also allows us to save duplicates as well as null values. In this article, we will learn how to empty a list in Java. Following are the ways to empty a list in Java: Using clear() Method Using removeAll() Method Using clear() Method The clear() method of the List interface is used to remove all elements from the list. It removes all the elements but returns nothing. Following is the syntax of the ... Read More
A Set is a Collection that cannot contain duplicate elements (same as the mathematical set abstraction). The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Finding the difference between two sets means finding the elements that are not similar in both sets. For example, we have set A = {1, 2, 3} and set B = {2, 3, 4}. The difference between these two sets will be {1, 4} because 1 is not in set B and 4 is not in set A. In this article, we will learn how to ... Read More
HashMap is a part of the Java Collections Framework, and it is used for storing key-value pairs. In this article, we will see how to update the value of a HashMap using a key. We will use following methods to update the value of a HashMap: Using put() Method Using compute() Method Using replace() Method Using put() Method The put() method is used to add a key-value pair to the HashMap. If the key already exists, it will update the value that is stored with that ... Read More
What is a LinkedList? A Linked list is a sequence of data structures where each node contains two parts as follows - Data: The value or information stored in the node. Next: A reference (or pointer) to the next node in the sequence. Detecting a Loop in a LinkedList A loop or cycle means that the last node of a linked list is connected back to one of the nodes earlier in the list, creating a cycle. Or, the next pointer of a node points to itself. The following are the ways ... Read More
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. In this example, we will see how to iterate over a Set in Java using the following methods: Using forEach() Method Using Stream API Using forEach() Method The forEach() method from the Set is used for iterating over each element of the set. We can use this method to perform an action for each element in ... Read More
ArrayList class in Java is a part of the Java Collections Framework and is used for storing a dynamically sized collection of elements. It is similar to an array, but it can grow and shrink in size according to the requirements of the program.Removing Duplicate Elements from an ArrayList In an ArrayList, we can store duplicate elements. In this article, we will see how to remove duplicate elements from an ArrayList in Java. The following are the methods we will use to remove duplicate elements from an ArrayList: Using removeIf() Method ... Read More
In this article, we will learn how to insert an element at a specific index in a Java list. For example, we have a fruit list by certain order and we want to insert a new fruit at a specific index in the list. Following are the ways to insert an element at a specific index in a Java list: Using add() Method Using ListIterator Using Stream API Using add() Method The add() method of the the list interface helps us to insert an element at ... Read More
In this article, we will understand how to find prime numbers between two intervals. Prime numbers are special numbers that have only two factors, 1 and itself, and cannot be divided by any other number. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number. All other prime numbers are odd numbers. Problem Statement Write a program in Java to display all prime numbers between two given intervals. Below is a demonstration of the same. ... Read More
The N-Queens problem is a puzzle where we need to place N queens on an N x N chessboard such that no two queens attack each other. A queen will attack another queen if they are in the same row, column, or diagonal. In this problem, you are given a value of N, and you need find possible arrangements for N queens on N x N chessboard. For example, Consider that we have a chessboard of size 4 x 4. In this case, we can place maximum 4 queens on the chessboard such that no two queens attack ... Read More