-1

I am trying to remove stop words from a tweet and I am adding the tokens first, then looping over them to see if they match a word in the stopword set, and if yes, removing them. I am getting a Java ConcurrentModificationErorr. Here's a snippet.

while ((line = br.readLine()) != null) {

                //store tweet splits
                LinkedHashSet<String> tweets    = new LinkedHashSet<String>();

                //We need to extract tweet and their constituent words
                String [] tweet = line.split(",");
                String input =tweet[1];
                String [] constituent = input.split(" ");

                //add all tokens in set
                for (String a : constituent) {
                    tweets.add(a.trim());
                }

                System.out.println("Before: "+tweets);


                //replace stopword
                for (String word : tweets) {
                    if (stopwords.contains(word)) {
                    tweets.remove(word);
                    }
                }

            System.out.println("After: "+tweets);
            //System.out.println("Tweet: "+sb.toString());
Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
  • you can see http://stackoverflow.com/questions/17279519/removing-items-from-list-in-java – warriorg Apr 27 '14 at 07:38
  • 1
    What you get is not a *ConcurrentModificationErorr* (sic). It's a *ConcurrentModificationException*. Just searching for *ConcurrentModificationException* here would lead you to dozens of similar questions. – JB Nizet Apr 27 '14 at 07:40
  • I solved it using a duplicate Set. Check my answer. – Ali Gajani Apr 27 '14 at 07:48

2 Answers2

1
for (String word : tweets) {
                        if (stopwords.contains(word)) {
                        tweets.remove(word);
                        }
                    }

the above code is causing concurrent modification exception because modifying collection while iterating so to avoid it use as below

   for(String word : new HashSet<String>(tweets)) {
                        if (stopwords.contains(word)) {
                        tweets.remove(word);
                        }
                    }
Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
0

I solved it using a duplicate LinkedHashSet.

LinkedHashSet<String> tweets_set    = new LinkedHashSet<String>(tweets);
                System.out.println("Before: "+tweets);

                //replace stopword
                for (String word : tweets_set) {
                    if (stopwords.contains(word)) {
                    tweets.remove(word);
                    }
                }
Ali Gajani
  • 14,762
  • 12
  • 59
  • 100