In this post we will learn How will you find Word Frequency in a collection of words java program with example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.kkjavatutorials.client; import java.util.Arrays; import java.util.List; import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; /** * @author KK JavaTutorials *How will you find Word Frequency in a collection of words ? */ public class FindWordFrequencyTest { public static void main(String[] args) { List<String> wordList = Arrays.asList("Apple", "Ananas", "Bananas", "Mango", "Cherry", "Bananas", "Apple", "Bananas", "Apple", "Cherry", "Mango", "Bananas"); ConcurrentMap<String, Integer> wordCountMap = wordList.stream(). collect(Collectors.toConcurrentMap(w->w, w->1, Integer::sum)); System.out.println(wordCountMap); } } |
Output of Above Program:
1 |
{Apple=3, Bananas=4, Cherry=2, Mango=2, Ananas=1} |
You may also like-
Check if two strings are anagrams or not in java
How to convert string to int without using library functions in java
Check Whether a Given String/Number is a Palindrome in java
How to find first non-repeated character in a given String in Java
How to find first non-repeatable character from a String using Java 8
That’s all about this topic How will you find Word Frequency in a collection of words? If you have any doubts or any suggestions to make please drop a comment.