Category Archives: Uncategorized
Why should we use the spring framework?
In this post, We will understand Why should we use the spring framework? The Spring Framework is a popular open-source application framework for building enterprise-grade Java applications. Here are some of the reasons why you should consider using the Spring Framework in your Java projects: Dependency Injection: Spring provides a powerful and flexible Dependency Injection (DI)… Read More »
Why to Use Gradle?
Gradle provides the following list of features: Free and open-source Gradle is a completely open-source project and licensed under the Apache Software License (ASL). High Performance Gradle very quickly executes the task by reusing the output from the previous task execution and it also processes tasks whose only… Read More »
Dynamic Tests example in Junit 5
When we write standard test cases using @Test annotation then tests are static in nature so that is fully specified at compile time and their behavior cannot be changed by anything happening at runtime. A dynamic test is a test that is generated at runtime by a factory method that is annotated with @TestFactory. A… Read More »
Open-Closed Principle (OCP)
This principle says that a class should be written in such a way that it performs its job flawlessly without the assumption that people in the future will simply come and change or modify it. Hence, the class should remain closed for any modification, but it should be open to getting extended. Ways of extending… Read More »
Java Program to find the frequency of each character in String ?
In Post, we will write a couple of java programs to find the frequency of each character in String?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.kkjavatutorials.client; import java.util.LinkedHashMap; import java.util.Map; /** * Java Program to find the frequency of each character in String ? * @author KK JavaTutorials * */ public class CountCharacterFrequencyInString_1 { public static void main(String[] args) { String inputText = "aabb abc"; findEachCharacterCounts(inputText); } private static void findEachCharacterCounts(String inputText) { if(inputText == null) { System.out.println("Enter Valid Text"); return; } Map<Character,Integer> charMapCount = new LinkedHashMap<>(); char[] charArray = inputText.toCharArray(); for (char c : charArray) { if(c == ' ') continue; if(charMapCount.containsKey(c)) { charMapCount.put(c, charMapCount.get(c)+1); }else { charMapCount.put(c, 1); } } charMapCount.forEach((k,v)->System.out.println(k+":"+v)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkjavatutorials.client; import java.util.LinkedHashMap; import java.util.Map; /** * Java Program to find the frequency of each character in String ? * @author KK JavaTutorials * */ public class CountCharacterFrequencyInString_2 { public static void main(String[] args) { String inputText = "aabb abc"; findEachCharacterCounts(inputText); } private static void findEachCharacterCounts(String inputText) { if(inputText == null) { System.out.println("Enter Valid Text"); return; } Map<Character,Integer> charMapCount = new LinkedHashMap<>(); char[] charArray = inputText.toCharArray(); for (char c : charArray) { if(c == ' ') continue; //Below methods are only available from JDK 1.8 onwards charMapCount.computeIfPresent(c, (k,v)->v+1); charMapCount.computeIfAbsent(c, v->1); } charMapCount.forEach((k,v)->System.out.println(k+":"+v)); } } |
Output of above both programs: a:3 b:3 c:1 You May Also Like: Difference between List and Set in Java Collection ? When should we choose LinkedList over ArrayList for a given… Read More »
Java program for Bubble Sort using Iterative Approach
Hello Friends, In this post, we will talk and learn about How to Write a Java program for Bubble Sort using Iterative Approach Finally, we will also write an improved version of the bubble sort as well. Out of the three simple sorting algorithms, bubble sort, insertion sort, and selection sort. The bubble sort is… Read More »
Java program for linear search using the Iterative Approach
Hello Friends, In this post, we will talk and learn about How to Write a Java program for linear search using the Iterative Approach. This program takes input array size & array elements from the user, afterward calling linearSearch(int[] inputArray, int seachKey) method to find search key element in the input array. This algorithm scan search key… Read More »
Java program to Print numbers in sequence using 2 Threads & Executor Framework
In this post, we will learn How to write a Java program to Print numbers in sequence using 2 Threads & Executor Framework In the previous posts, we already had seen How to Print numbers in sequence using 2 Threads(Approach-1) and Print numbers in sequence using 2 Threads(Approach-2) In this post, I have used the same problem statement… Read More »