Java Word Frequency Analysis Code
Java Word Frequency Analysis Code
Java code
Absolutely! Let's break down the Java code line by line, so you clearly understand what each step does. ---
Full Annotated Version of the Code import [Link].*; public class CollectionUtils { public static void
🔁
Convert to array of lowercase words String[] words = [Link]().split(" "); // 👉
main(String[] args) { // Step 1: Input string String input = "Java is awesome and Java is powerful"; // Step 2:
This gives: ["java",
"is", "awesome", "and", "java", "is", "powerful"] // Step 3: Create a HashMap to store frequency of each word
//👉
map yet } // Step 5: Print word frequency [Link]("Word Frequency:"); [Link](freqMap);
Output: {java=2, is=2, awesome=1, and=1, powerful=1} // Step 6: Find and store duplicate words Set
duplicates = new HashSet<>(); for ([Link] entry : [Link]()) { if ([Link]() > 1) {
[Link]([Link]()); } } [Link]("Duplicate words:"); [Link](duplicates); // 👉
Output: [java, is] // Step 7: Store unique words (Set automatically removes duplicates) Set unique = new
HashSet<>([Link](words)); [Link]("Unique words:"); [Link](unique); // 👉
Output: [java, is, awesome, and, powerful] // Step 8: Find first non-repeating word using the frequency map for
break; //👉 👉
(String word : words) { if ([Link](word) == 1) { [Link]("First non-repeating word: " + word);
Stop after finding the first one } } // Output: awesome (only occurs once, and comes first) //
Step 9: Sort all words alphabetically List wordList = new ArrayList<>([Link](words));
[Link](wordList); [Link]("Sorted words:"); [Link](wordList); // 👉 Output: