Category: Java programming question
-
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
-
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…