Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3427470
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:50:25+00:00 2026-05-18T06:50:25+00:00

The assignment, for my data structures class, is to find the shortest path from

  • 0

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 sorting 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:50:26+00:00Added an answer on May 18, 2026 at 6:50 am
    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.");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.