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

  • Home
  • SEARCH
  • 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 8809839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:58:37+00:00 2026-06-14T02:58:37+00:00

I made a small Java program that loads a words list from a txt

  • 0

I made a small Java program that loads a words list from a txt file which user chooses and stores it word-by-word in a TreeSet.
Now I need to write a function that, whenever user types something into a text field (keyPressed), a function gets called and it finds/returns all words in this TreeSet that are beginning with letters that user typed in.
I am new to using Sets so my solution would be to iterate from the first to the last element in the set and printing out the ones that satisfy the criteria:

Iterator <String>itr = dictionary.iterator();
String currentWord;
String tempUserInput = "av"; // Temporary, to simulate user input
while(itr.hasNext()){
   currentWord = itr.next();
   if (currentWord.startsWith(tempUserInput)) {
      System.out.println(currentWord); // Temporary, to simulate output
   }
}

This works properly but, since it needs to pass over 300000 words for a return value, my question is: Is there a more efficient solution to this problem?

  • 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-06-14T02:58:38+00:00Added an answer on June 14, 2026 at 2:58 am

    Best stolution is to use a trie which is a suitable data structure for your problem. It is meant to be used to lower complexity while working of elements (usually strings) that share prefixes.

    Actually with some working I guess you could use a TreeSet by tricking the

    public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
    

    This could work by passing as fromElement the string which is currently typed and as the toElement the same string with added enough 'z' characters as the longest string in the set (this length could be calculated while populating the TreeSet). Eg:

    subSet("foo", true, "foozzzzzzzz", true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a small program that will read a text file, which contains a
I've made a small program which has 2 buttons and each does certain thing.
Problem: Have made a small mail program which works perfectly on my developer pc
I've made a small program in C++ and Qt. The program displays a list
Using java and made a small test server to log incoming user connections. Adding
I made a small change to an old java file in IntelliJ and when
I made this small Java program using eclipse IDE. I have set the workspace
I have made a small program where the user gives the address of an
I made a Java program that did some basic math for kids in primary.
I have made a small java swing application that I want to share with

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.