FARHEEN .’s Post

🚀 Java 8 Streams – Word Frequency in a Sentence Here’s a classic interview-style problem 👇 🧩 Input: String str = "I am learning Streams API in JAVA JAVA"; 🎯 Goal: Count the frequency of each word using the power of Java Streams API. At first, we might think of using a Map and a loop. But with Java 8 Streams, we can make it expressive and concise. 💡 Solution: String str = "I am learning Streams API in JAVA JAVA"; Map<String, Long> wordCount = Arrays.stream(str.split(" ")) .collect(Collectors.groupingBy( word -> word, Collectors.counting())); wordCount.forEach((key, value) -> System.out.println(key + " : " + value)); 🔎 What’s happening here? • split(" ") → Breaks sentence into words • stream() → Creates a stream • groupingBy() → Groups identical words • counting() → Counts occurrences Simple. Clean. Powerful ✨ Want to make it better? You could: 1️⃣ Convert everything to lowercase to make it case-insensitive Problems like this show how beautifully Streams handle data transformation with minimal code. #Java #Java8 #Streams #BackendDevelopment #CodingInterview #SoftwareEngineering #LearningJourney #LearnWithMe

  • diagram

To view or add a comment, sign in

Explore content categories