简体   繁体   English

TreeSet上的迭代器导致无限循环

[英]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. 对于此分配,我需要将每个包含2个字符串的自定义数据类(称为User)的实例保存到TreeSet中。 I must then search the TreeSet I created for a string taken from each line of another file. 然后,我必须在我创建的TreeSet中搜索从另一个文件的每一行获取的字符串。 The first file is a .csv file in which each line contains an email address and a name, the .txt file contains only addresses. 第一个文件是.csv文件,其中每行包含一个电子邮件地址和一个名称,.txt文件仅包含地址。 I have to search for every line in the .txt file, and I also have to repeat the entire operation 4000 times. 我必须搜索.txt文件中的每一行,并且还必须将整个操作重复4000次。

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. 我无法使用.contains搜索TreeSet,因为无法按用户搜索,因为.txt文件仅包含User所做的两条信息之一。 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. 根据在不同地方找到的信息,我可以从TreeSet中获取迭代器,并使用它来检索其中的每个User,然后获取User的用户名并将其直接与第二个文件中的字符串进行比较。 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. 有关其他信息,这是我的User类。

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(). 我似乎正确地完成了所有操作,但是在我看来,即使当我调用next()时,迭代器也不会移动其指针。 Does anyone see something I'm missing? 有人看到我想念的东西吗?

Edit: Thanks to KevinO for pointing this out- a is the name of the TreeSet. 编辑:感谢KevinO指出这一点-a是TreeSet的名称。

Edit: Here's the declaration of TreeSet. 编辑:这是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. 您将打开一个文件4000次,并对文件中的每一行进行遍历一个集合。 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 更高版本的Java具有更简洁的方式来打开文件并遍历所有行: Files.lines
  • You don't need an Iterator to iterate through a collection. 您不需要Iterator即可迭代集合。 A normal for-each loop will do or convert it to a stream 普通的for-each循环会执行或将其转换为流
  • 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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