SEO Word Count in Java 8

SEO (Search Engine Optimization) is very important for any website. Website which even has a great content also fails in SEO, because of the search engine optimization factors.

As per 2020 recent repots from google“, Google considers around 200+ factors to provide the ranks to the websites it crawls.

SEO Word Count

SEO Word count is one of the most important factory out of 200+ factors to rank a website.

Here is a Java 8 SEO word counter program to print the list of words with the occurrences count.

SEOWordCount.java:

public class SEOWordCount {
 public static void main(String[] args){
   String articleText = "YOUR_ARTICLE_CONTENT";
   List<String> wordsList = Stream.of(articleText.split(" ")).collect(Collectors.toList());
   Map<String, Long> wordsCountMap = wordsList.stream().map(eachWord -> eachWord).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
   System.out.println(wordsCountMap);
 }
}

This SEO Word count java program will print the result something like this,

{seo=10, optimization=7, rank=14,google=2}

The above information will be really helpful to optimize your article for search engines like google and improve the articles based on your keywords to target.

If you like to print the above map results in the descending order by words counts then, add this line at the last of the above SEO word count Java program.

Sort – HashMap by values using Java 8 streams:

wordCountMap = wordCountMap.entrySet().stream().sorted(Map.Entry.<String, Long> comparingByValue().reversed()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2)-> e1, LinkedHashMap::new));
System.out.println(wordCountMap);

Hashmap value orders print the results in descending order now (because of .sorted, .reversed and linkedhashmap to keep the orders), Still confused, then read this to understand the difference between hashmap and linkedhashmap.

{rank=14, seo=10, optimization=7, google=2}

Things to note:

  • I have useed LinkedHashMap because by default the sorted results are collected in the HashMap only, which can not maintain the sorting order/insertion orders.
  • So always prefer to use LinkedHashMap, when ever you would like to keep the insertion or sorting orders.
  • .sorted() will perform ascending order only by default.
  • In the above sample program, we have used .reversed() inside the .sorted(), to change the order from ascending to descending orders.

 507 total views,  52 views today

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.