If a Java class is a generic type, and we are using it with the Gson library for JSON serialization and deserialization, the TypeToken class from com.google.gson.reflect package is used for preserving the generic type information at runtime. What are Generic Types in Java? Generic types in Java mean we don't set any method or class to a specific type, like int, String, etc. Instead, we use a type parameter (like T) to represent the type. This allows us to create classes and methods that can operate on objects of different types, and also provides compile-time safety at the same time. ... Read More
In this article, we will learn the findAny() method with examples in Java. The findAny() method in Java Streams is a tool that is used for fetching an arbitrary element from a stream. It provides a quick and easy way to retrieve any element without writing any conditions. This method returns a container that may or may not contain a non-null value, which is called an Optional object. What is the findAny() Method? The findAny() method retrieves any element from the stream, and the result is wrapped in an Optional object. If the stream is empty, the Optional object will ... Read More
In this article, we will show you how to check whether a number is prime by creating a function in C++. A prime number is a number greater than 1 that has no divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers because they have only two divisors (1 and themselves), whereas 4, 6, 8, and 9 are not prime. Using a Function to Check Prime Number To check if a number is prime, we define a function that performs the check and returns true if the number ... Read More
In this article, we'll show you how to write a C++ program to create a simple calculator that can add, subtract, multiply, or divide using a switch statement. The calculator works by taking two numbers(operands) and an operator (+, -, *, /) from the user, and then it performs the chosen operation. Lets understand it better with an example: Suppose the user enters: a = 5 and b = 4 Then, based on the chosen operator: If the operator is +, the result is 5 + 4 = 9 If the operator is -, the result is 5 ... Read More
In this article, we'll show you how to write a C++ program to calculate the factorial of a number using recursion . The factorial of a number is the result of multiplying all the positive numbers from 1 to that number. It is written as n! and is commonly used in mathematics and programming. Let's understand this with a few examples: //Example 1 Input: 5 The factorial of 5 is: 5 * 4 * 3 * 2 * 1 = 120 Output: 120 //Example 2 Input: 6 The factorial of 6 is: 6 * 5 * 4 * ... Read More
Queue is a linear data structure that follows the First In First Out (FIFO) principle. In this article, we will learn how to use the queue container from the Standard Template Library (STL) in C++. What is Queue? A Queue is a container that stores data in a sequential way. Meaning, the data that is inserted first to the queue is the one to be removed first from it. This is same as a queue of people where, the person who comes first to the queue will served first. The STL library of C++ provides a pre-defined queue container ... Read More
The Prev permutation is an algorithmic operation that rearranges the elements of an array or a range of an array into the previous lexicographically smaller permutation. In this article, we will learn how to use the prev_permutation() function from the Standard Template Library (STL) in C++. What is Prev Permutation? The Prev Permutation is an operation used to generate all the possible permutations of an array in reverse lexicographical order. A permutation is one of the N! arrangements of the elements in an array of size N. The STL library of C++ provide a pre-defined function for performing the ... Read More
A pair is a container provided by the Standard Template Library (STL) in C++ that stores two heterogeneous objects as a single unit. In this article, we will learn how to use a pair from the Standard Template Library (STL) in C++. What is Pair? Pair is a utility container defined in the header that is used to store two related data elements or objects in pairs. The first element will be referenced as first, and the second element will be referenced as second. It is commonly used to return two values from a function using a single ... Read More
Next permutation is an algorithmic operation that rearranges the elements of a range into the next lexicographically greater permutation. In this article, we will learn how to use the next_permutation() function from the Standard Template Library (STL) in C++. What is Next Permutation? The Next Permutation is an operation used to generate all the possible permutations an array in lexicographical order. A permutation is the one of N! arrangements of the elements in an array of size N. If the current sequence is the last permutation, the function transforms it into the first one (i.e., sorted in ascending order). ... Read More
In this article, we'll show you how to write a C++ program to find the Greatest Common Divisor(GCD) of two numbers using Recursion. The GCD of two numbers is the largest number that divides both of them without leaving a remainder. It is also known as the Greatest Common Factor (GCF). For example, the GCD of 36 and 60 is 12, because 12 is the largest number that divides both 36 and 60 without a remainder. Using Recursion to Find the GCD To find the GCD, we use recursion. In recursion, a function keeps calling itself with updated ... Read More