Question

I'm having trouble parsing a given text file based on words count frequency. Everything is happening accordingly except ordering, based on words count.

My Approach

  • Parsing the text file
  • Splitting & Storing every words in a String Array
  • passed array will have the count for every unique word and passed to Map
  • and Map will sort this based on Value

Here is the code:

public class TestApp {
    public static void main(String[] args) throws IOException {
        String Line = null;
        Map<String, Integer> finalMap = new HashMap<String, Integer>();
        FileParser parser = new FileParser();
        Reader fileReader = new FileReader("E:\\21350.txt");
        BufferedReader bufferReader = new BufferedReader(fileReader);

        while ((Line = bufferReader.readLine()) != null) {
            String[] str = Line.split("\\s");
            finalMap = parser.parseFile(str);
        }
        // new TestApp().showEntry(finalMap); // Before
        parser.sortByValue(finalMap);
        new TestApp().showEntry(finalMap); // After

        bufferReader.close();
    }

    public void showEntry(Map<String, Integer> map) {

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " --> " + entry.getValue());
        }
    }
    }

    public class FileParser {

    Map<String, Integer> map = new HashMap<String, Integer>();

    public Map<String, Integer> parseFile(String[] strArray) {
        for (String key : strArray) {
            int counter = 1;
            if (map.containsKey(key))
                map.put(key, map.get(key) + 1);
            else
                map.put(key, counter);
        }
        return map;
    }

    public Map<String, Integer> sortByValue(Map<String, Integer> map) {

        List list = new LinkedList(map.entrySet());
        Collections.sort(list, new Comparator() {

            public int compare(Object o1, Object o2) {
                int x = (Integer) ((Map.Entry) o1).getValue();
                int y = (Integer) ((Map.Entry) o2).getValue();

                if (x > y)
                    return -1;
                else if (x < y)
                    return 1;
                else
                    return 0;
            }
        });

        Map<String, Integer> result = new LinkedHashMap<String, Integer>();

        Iterator it = list.iterator();
        while (it.hasNext()) {
            Map.Entry<String, Integer> entry = (Map.Entry) it.next();
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }
}

Whats wrong with this code?

Était-ce utile?

La solution

parser.sortByValue(finalMap); returns a new (sorted) map which you ignore. Instead, you print the old (unsorted) finalMap.

finalMap = parser.sortByValue(finalMap);

should fix this.

Also note that the code above creates one map per file but prints only the last one of all of them.

Depending on what you want, you should either pass the map into parser.parseFile(str); to accumulate the results for all files or sort+print inside the loop to get one result per file.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top