Java Program for Anagrams

Problem : A text file is given which contains many anagrams line by line. All anagrams should be in groups after processing of this file.

package com;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class FindAnagram {

       public static void main(String[] args) {
              String aFile = "F:\\anagram\\anagramInput.txt";
              Map<String, String> contents = new HashMap<String, String>();
              BufferedReader  input = null;
              String line = null;
             
                  try {
                     input =  new BufferedReader(new FileReader(aFile));
                           while (( line = input.readLine()) != null){
                                  char[] inputArray = line.toCharArray();
                                  Arrays.sort(inputArray);
                                  contents.put(line , String.valueOf(inputArray));
                                  System.out.println(" input line = " + line + "   sorted content of line "+ String.valueOf(inputArray));
                             }
                          
                           showMap(contents);
                           Map<String, String> sortedMap = sortUsingComparator(contents);
                           System.out.println("sorted map");
                           showMap(sortedMap);
                           writeToFile(sortedMap);
                          
                     } catch (IOException e) {
                           e.printStackTrace();
                     } finally {
                           if (input != null) {
                                  try {
                                         input.close();
                                  } catch (IOException e) {
                                         e.printStackTrace();
                                  }
                           }
                     }
       }


       /**
       * This method writes anagram to text file in group
       *
        * @param sortedMap
       */
       private static void writeToFile(Map<String, String> sortedMap) {
              String aFile = "F:\\anagram\\anagramOutput.txt";
              BufferedWriter output = null;
              try {
                     output =  new BufferedWriter(new FileWriter(aFile));
                     for(Map.Entry<String, String> entryMap : sortedMap.entrySet()){
                           output.write(entryMap.getKey()+"\n");
                     }
                     output.flush();
              } catch (IOException e) {
                     e.printStackTrace();
              }
             
       }


       /**
       * This method prints Map
       *
        * @param contents
       */
       private static void showMap(Map<String, String> contents) {
              for(Map.Entry<String, String> content : contents.entrySet()){
                     System.out.println("Key = " + content.getKey() + " Value =  " +  content.getValue());
              }
       }
      
       /**
       * This method sort map on the basis of values
       *
        * @param unsortMap
       * @return Map
       */
       private static Map<String, String> sortUsingComparator(Map<String, String> unsortMap) {
             
              // Convert Map into List
              List<Map.Entry<String, String>> listOfMap = new LinkedList<Map.Entry<String, String>>(unsortMap.entrySet());
              // Sorting listOfMap to compare values of unsortedMap with the help of comparator
              Collections.sort(listOfMap, new Comparator<Map.Entry<String, String>>() {
                     public int compare(Map.Entry<String, String> objectA, Map.Entry<String, String> objectB) {
                           return (objectA.getValue()).compareTo(objectB.getValue());
                     }
              });
              // convert sorted listOfMap back to Map
              Map<String, String> sortedMap = new LinkedHashMap<String, String>();
              for (Iterator<Map.Entry<String, String>> it = listOfMap.iterator(); it.hasNext();) {
                     Map.Entry<String, String> entry = it.next();
                     sortedMap.put(entry.getKey(), entry.getValue());
              }
              return sortedMap;
       }


}

Comments

Popular posts from this blog

Skipping Junit Tests in maven build

Writing text to file and reading text from files in Java