Category: Java programming question
-
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
-
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 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:
-
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…
-
Remove a key value pair from HashMap while Iterating over it
1. Introduction While programming you may come across a requirement when you want to remove a key value pair while iterating over a HashMap. The requirement is to search a key in HashMap and if found, remove the key value pair. The way you may think of is to iterate over the HashMap and compare…
-
Convert Iterator to Stream in Java
Sometimes there is a need to convert Iterator to Stream to use lambda expression and other functional programming features. StreamSupport class provides stream(Spliterator spliterator, boolean parallel) method which returns Stream. The second argument in this method is for sequential or parallel stream. We need to provide true for parallel stream. Iterator does not have any…
-
Convert Iterable to Stream in Java
Iterables are very useful but provide limited support for lambda expressions. To get other features introduced in Java 8, sometimes there is a need to convert Iterable to Stream in Java. To convert Iterable to Stream, we first get a Spliterator reference. We then use StreamSupport class’ stream(Spliterator spliterator, boolean parallel) method to get Stream.…
-
Java program to print the sum of all elements of an array
This is a programming question for beginners. The logic for this program is simple. Output