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 ?