How should I iterate through a HashSet, removing certain elements, and altering others?

I have the following method which is passed a HashSet<String> of words from IMDB reviews.

private static void reduceVocab(HashSet<String> vocab) {
    for (Iterator<String> i = vocab.iterator(); i.hasNext();) {
        String element = i.next();
        element = element.replaceAll("[^a-zA-Z0-9]", ""); // Need to replace this
        if (element.length() <= 3) {
            i.remove();
        }
    }
}

I want to perform a couple of actions to reduce the size of the HashSet by removing Strings that are too short and removing non-alphanumeric characters. Is there any way to perform what I'm trying to do with element.replaceAll()?

Upvotes: 0

Views: 148

Answers (1)

Paul Boddington
Paul Boddington

Reputation: 37665

You cannot add to a HashSet while iterating over it. This makes what you are trying to do slightly awkward. The line

element = element.replaceAll("[^a-zA-Z0-9]", "");

gives a new string, but the new string won't be in the set.

You can do it like this:

private static void reduceVocab(HashSet<String> vocab) {
    Set<String> copy = new HashSet<>();
    for (String str : vocab) {
        str = str.replaceAll("[^a-zA-Z0-9]", "");
        if (str.length() > 3)
            copy.add(str);
    }
    vocab.clear();
    vocab.addAll(copy);
}

Upvotes: 4

Related Questions