开发者

Sorting all words in a file by length, in one read. (Java)

开发者 https://www.devze.com 2023-01-26 12:37 出处:网络
The assignment, for my data structures class, is to find the shortest path from one word to another.

The assignment, for my data structures class, is to find the shortest path from one word to another.

i.e. Start: bleed -> blend -> blond -> End: blood, with a cost of 3.

I'm given a list of words that I have to group using a map. Where:

Key: length of word, Value: Set of all words with that length.

I already finished the program but I think I can improve performance if I change the way I store the sets in the map. Right now I do a scan through the text file and store each individual word into an ArrayList, then I go through the ArrayList and store all words of length x into a set while removing each word from the List. I continue like that starting from the first element in the ArrayList until the List is empty.

I was wondering it if I could do this so开发者_如何学JAVArting as I'm reading in the file, and avoid the ArrayList altogether.

This is the code I have:

ArrayList<String> wordList = new ArrayList<String>();
Map<Integer, Set> setMap = new HashMap<Integer, Set>();
Graph pathGraph = new Graph();

private void readFile(String file) {
    try {
        FileReader f = new FileReader(file);
        BufferedReader reader = new BufferedReader(f);
        String line = "";
        while ((line = reader.readLine()) != null) {
            wordList.add(line);
        }

    } catch (Exception e) { //Done in case of an exception
        System.out.println("No file found.");
    }
}

private void mapMaker() {
    int wordLength = 1;
    Set<String> wordSet = new HashSet<String>();
    while (!wordList.isEmpty()) {
        wordSet = setBuilder(wordLength);
        if (!wordSet.isEmpty()) {
            setMap.put(wordLength, wordSet);
        }
        wordLength++;
    }
}

private Set<String> setBuilder(int x) {
    Set<String> wordSet = new HashSet<String>();
    int counter = 0;
    while (counter < wordList.size()) {
        if (wordList.get(counter).length() == x) {
            wordSet.add(wordList.get(counter));
            wordList.remove(counter);
        } else {
            counter++;
        }
    }
    return wordSet;
}

Thanks, in advance for any input.


private void readFile(String file) {
    try {
        FileReader f = new FileReader(file);
        BufferedReader reader = new BufferedReader(f);
        String word = "";
        while ((word = reader.readLine()) != null) { 
            int length = word.length();
            if(setMap.containsKey(length)) {
                setMap.get(length).add(word);
            } else {
                Set set = new HashSet<String>();
                set.add(word); 
                setMap.put(length, set);
            }
        }

    } catch (Exception e) { //Done in case of an exception
        System.out.println("No file found.");
    }
}


You can use a Guava's MultiMap:

Example:

        String[] words={"world","hello","abc","bcd","abc"};
        SetMultimap<Integer,String> lenMap=HashMultimap.create();
        for(String str:words)//instead read word's from file in your case
            lenMap.put(str.length(),str);

Output:

{3=[abc, bcd], 5=[hello, world]}
0

精彩评论

暂无评论...
验证码 换一张
取 消