0

I am facing same situation as In Java, remove empty elements from a list of Strings. I tried almost everything from resources i could get, but every time i get same error "Exception in thread "main" java.lang.UnsupportedOperationException"

public static void main(String[] args) {
    String s = "Hello World. Want to code?";
    StringTokenizer tokenizer = new StringTokenizer(s, ".!?");

    List<String> words = new ArrayList<String>();
    List<String> statement = new ArrayList<String>();
    List<List<String>> statements = new ArrayList<List<String>>();

    // Seperating words by delimiters ".!?
    while (tokenizer.hasMoreTokens()) {
        words.add(tokenizer.nextToken());
    }
    // O/p is {{Hello World},{ Want to code}}

    // seperating words by space.
    for (int i = 0; i < words.size(); i++) {
        String[] temp2 = words.get(i).split("\\s+");
        statement = Arrays.asList(temp2);
        statements.add(statement);
    }
    // O/P is {{Hello, World},{, Want, to, code}}

    for (List<String> temp : statements) {
    // Here i have [, Want, to, code]   

        // Way-1
        Iterator it = temp.iterator();
        String str = (String) it.next();
        if(str.isEmpty())
            it.remove();

        // Way-2
        temp.removeIf(item -> item.contains(""));

        // Way-3
        temp.removeAll(Collections.singleton(""));

        // Way-4
        temp.removeAll(Arrays.asList(""));

        // way-5
        temp.removeIf(String::isEmpty);
    }
}

As you can see, i have tried 4 ways, none of them are working. Anybody has any idea ?

Community
  • 1
  • 1
user252514
  • 307
  • 1
  • 3
  • 14
  • Try `temp.removeIf(String::isEmpty)` – Andreas Apr 16 '17 at 23:07
  • Not working. :( I edited question. – user252514 Apr 16 '17 at 23:10
  • 1
    Sorry, I meant that as a better alternative to your 4 other ways, not as an answer to your question. The answer is in the duplicate link. – Andreas Apr 16 '17 at 23:12
  • *Where* do you have `new ArrayList(Arrays.asList(stringArray))`?? I only see two `asList()` calls in the code, and *neither of them* are converted to `ArrayList`. Note that `list = new ArrayList(); list.add(Arrays.asList(...));` is **not** the same as `list = new ArrayList(Arrays.asList(...));`. Perhaps you should [read the javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#ArrayList-java.util.Collection-) of the `ArrayList(otherList)` constructor, and see what it does. – Andreas Apr 16 '17 at 23:23
  • Yeah, i deleted that question within a minute. I apologize and Thanks as well. It' working. :) – user252514 Apr 16 '17 at 23:26

1 Answers1

-1

I think I've found a solution: Change the data type of List to ArrayList. This solved it for me:

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        String s = "Hello World. Want to code?";
        StringTokenizer tokenizer = new StringTokenizer(s, ".!?");

        ArrayList<String> words = new ArrayList<String>();
        ArrayList<String> statement = new ArrayList<String>();
        ArrayList<ArrayList<String>> statements = new ArrayList<ArrayList<String>>();

        // Seperating words by delimiters ".!?
        while (tokenizer.hasMoreTokens()) {
            words.add(tokenizer.nextToken());
        }
        // O/p is {{Hello World},{ Want to code}}

        // seperating words by space.
        for (int i = 0; i < words.size(); i++) {
            String[] temp2 = words.get(i).split("\\s+");
            statement = new ArrayList();
            statement.addAll(Arrays.asList(temp2));
            statements.add(statement);
        }
        // O/P is {{Hello, World},{, Want, to, code}}

        for (ArrayList<String> temp : statements) {
        // Here i have [, Want, to, code]

            // Way-2
            temp.removeIf(item -> item.equals(""));
            System.out.println("array: " + temp);
        }
    }
}

The output of the program is this:

array: [Hello, World]
array: [Want, to, code]
mcjcloud
  • 351
  • 2
  • 11