简体   繁体   中英

Iterator over TreeSet causes infinite loop

For this assignment, I'm required to save instances of a custom data class (called User) each containing 2 strings into a TreeSet. I must then search the TreeSet I created for a string taken from each line of another file. The first file is a .csv file in which each line contains an email address and a name, the .txt file contains only addresses. I have to search for every line in the .txt file, and I also have to repeat the entire operation 4000 times.

I can't use .contains to search the TreeSet because I can't search by User, since the .txt file only contains one of the two pieces of information that User does. According to information I've found in various places, I can take the iterator from my TreeSet and use that to retrieve each User in it, and then get the User's username and compare that directly to the string from the second file. I wrote my code exactly as every site I found suggested, but my program still gets stuck at an infinite loop. Here's the search code I have so far:

for (int i = 0; i < 4000; i++)//repeats search operation 4000 times
{
  try
  {
    BufferedReader fromPasswords = new BufferedReader(new FileReader("passwordInput.txt"));

    while ((line = fromPasswords.readLine()) != null)
    {
      Iterator it = a.iterator();
      while (it.hasNext())
      {
        //the infinite loop happens about here, if I put a println statement here it prints over and over
        if(it.next().userName.compareTo(line) == 0)
          matches++; //this is an int that is supposed to go up by 1 every time a match is found
      }
    }
  }
  catch (Exception e)
  {
    System.out.println("Error while searching TreeSet: " + e);
    System.exit(0);
  }
}

For some additional info, here's my User class.

class User implements Comparable<User>
{
  String userName;
  String password;

  public User() { userName = "none"; password = "none"; }
  public User(String un, String ps) { userName = un; password = ps; } 

  public int compareTo(User u)
  {
    return userName.compareToIgnoreCase(u.userName);
  }
} //User

I've done everything seemingly correctly but it looks to me like iterator doesn't move its pointer even when I call next(). Does anyone see something I'm missing?

Edit: Thanks to KevinO for pointing this out- a is the name of the TreeSet.

Edit: Here's the declaration of TreeSet.

TreeSet<User> a = new TreeSet<User>();

Are you certain there's an infinite loop? You're opening a file 4000 times and iterating through a collection for every line in the file. Depending on size of the file and the collection this could take a very long time.

Some other things to be aware of:

  • Later versions of Java have a more succinct way of opening a file and iterating through all the lines: Files.lines
  • You don't need an Iterator to iterate through a collection. A normal for-each loop will do or convert it to a stream
  • If all you want to do is count the matches then a stream is just as good

Putting all that together:

Path path = Paths.get("passwordInput.txt");
Set<User> users = new TreeSet<>();

long matches = Paths.lines(path)
    .mapToLong(l -> users.stream()
        .map(User::getName).filter(l::equals).count())
    .sum();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM