简体   繁体   中英

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 String s that are too short and removing non-alphanumeric characters. Is there any way to perform what I'm trying to do with element.replaceAll() ?

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);
}

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