Category: Java programming question
-
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
-
Comparing Arrays for equality in Java
Two arrays are said to be equal if the arrays have equal number of elements and all corresponding pairs of elements in two arrays are equal. Method 1: Iterative method Following is the logic: This method is not not recommended for large sized array. Method 2: Using Arrays.equals() Arrays.equals() method returns true if the two…
-
Java program to find duplicate characters in a String
Duplicate characters with count from string using loop Following is the logic: Output Duplicate characters with count from string using map Following is the logic: Output
-
Compare two HashMap in Java
equals() method in the HashMap compares two maps by key-value pairs. This method returns true if two maps represent the same mappings. Since HashMap is not an ordered collection, the order of keys in both maps may be different. Compare HashMaps by keys and values Output Compare two hashmaps for same keys If we want…
-
Java program to convert String to boolean
java.lang.Boolean class provides two methods to convert String to boolean. public static boolean parseBoolean(String s) This method parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”. public static Boolean valueOf(String s) This method returns…
-
Write a java program to find common elements between two arrays?
Method 1: Iterative method In this method, we iterate both the given arrays and compare each element of one array with elements of other array. If the two compared elements are found equal then they add element to a HashSet. The method works well even for arrays which contain duplicate elements. Output Method 2: Using retainAll()…
-
Java program to check whether an alphabet is vowel or consonant
The alphabets A, E, I, O and U (small case and upper case) are known as vowels and rest of the alphabets are known as consonants. Method 1: Program to check if character is vowel or consonant using switch case Output Method 2: Program to check if character is vowel or consonant using using if..else…
