简体   繁体   中英

Java: Why does my HashSet and TreeSet contain duplicates?

When my text file contains this:

a b c b

My HashSet and TreeSet say there are 3 unique words.

When my text file contains this:

a b c a

My HashSet and TreeSet say there are 4 unique words.

Why?

  public static int countUnique1A(WordStream words) {

    HashSet<String> hashSetA = new HashSet<String>();
    for (String i : words) {
      hashSetA.add(i);
    }

    return hashSetA.size();
  }

  public static int countUnique1B(WordStream words) {

    TreeSet<String> treeSetA = new TreeSet<String>();
    for (String i : words) {
      treeSetA.add(i);
    }
    return treeSetA.size();
  }

I guess it may be due to spaces between the words. Eg HashSet may contain 'a' and 'a '. Can you try changing:

hashSetA.add(i);

to

hashSetA.add(i.trim());

We need to do the same for treeSetA as well.

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