Category: Java programming question
-
Sort a given list of decimals in reverse order in java 8
You can sort a list of decimal numbers in reverse order using Java 8 Streams and sorted(Comparator.reverseOrder()). Output: Explanation: This approach is efficient and works for Double, Float, or BigDecimal.
-
Find the frequency of each element in an array or a list
You can find the frequency of each element in an array or list using Java 8 Streams and the Collectors.groupingBy() collector. Example 1: Finding Frequency in a List Output: Example 2: Finding Frequency in an Array Output:
-
Find the frequency of each character in a string using Java 8 streams
You can use Java 8 Streams to count the frequency of each character in a string by using the Collectors.groupingBy() collector. Here’s how: Output: Explanation:
-
Java program to separate odd and even numbers in a list of integers
Approach: Use Collectors.partitioningBy() to separate even and odd numbers into a Map<Boolean, List<Integer>>. Output:
-
Java Program to Reverse a Number
1. Problem For a given number n for example (1234), write a Java program to reverse the number (4321 in this case). 2. Steps reverse a number Here are the steps to reverse a number: 3. Program
-
Java Program to Check if a String is Palindrome
1. Problem A palindrome is a word, phrase, number, or sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). Examples: 2. Steps to check if string is palindrome Here are the steps to check if a string is a palindrome: 3. Program Approach 2 Approach 3
-
Java program to check if two strings are anagram
1. Problem An anagram is a word or phrase formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once. For example: 2. Steps to check if two strings are anagram 3. Program Java 8 Anagram Check using Streams Output: Explanation: Alternative Approach: Using Character Frequency Map You…
-
Java program to reverse an array
1. Problem statement Given an array A of integers, reverse this array A in linear running time using constant memory. 2. Solution The steps to reverse an array can be shown with the help of following figure: 3. Program Following is the program to reverse the array. Output: Approach 2
-
Java program to find first even integer in list of numbers
We’ll see two approaches to get the first even number from a list of numbers. In the first approach, we iterate the list and return the first even number. We’ll use the for loop to iterate the list. In the second approach, we’ll use the Java stream and findFirst() method to get the first even…
-
Count duplicate elements in ArrayList in Java
Problem statement For a given list of elements (String elements in our example), the task is to find which elements are duplicate and the number of times the element appears in the list. List “one”, “two”, “three”, “one”, “three”, “four”, “five”, “six”, “one”, “nine”, “six” Output nine : 1six : 2four : 1one : 3five…
