ArrayList<Character> soglas = new ArrayList<>(); 

contains: М м м л р м .
It is necessary, what would become: М м м л р м .

    3 answers 3

    With the help of streams:

     List<Character> result = list .stream() .filter(c -> !Character.isSpaceChar(c)) .collect(toList()); 

    Before java 8 this is done in the usual loop:

     for (int i = 0; i < list.size(); i++) if (Character.isSpaceChar(list.get(i))) list.remove(i); 
    • thanks, it worked so well - enotdk

    You can in different ways. If there is no restriction only on the 7th Java, then it is possible so :

     List<String> list = new ArrayList<>(Arrays.asList("How are you", "How you doing", "Joe", "Mike")); list.removeIf(s -> !s.contains("How")); //убрать все строки без How 

    In your case, according to @Artem Konovalov, you can do this :

     list.removeIf(Character::isSpaceChar); //убрать каждый char, если он пробел 

    If you use Java 7, you can do this with rxJava :

     List<Character> characters = new ArrayList<Character>() {{ add('a'); add(' '); add('b'); add(' '); add('c'); }}; Observable.from(characters) .filter(new Func1<Character, Boolean>() { @Override public Boolean call(Character character) { return !Character.isWhitespace(character); } }) .toList() .subscribe(new Action1<List<Character>>() { @Override public void call(List<Character> characters) { System.out.print(characters); } }); 

    If RetroLambda connected, then it can be shorter:

     List<Character> characters = new ArrayList<Character>() {{ add('a'); add(' '); add('b'); add(' '); add('c'); }}; Observable.from(characters) .filter(character -> !Character::isWhitespace) .toList() .subscribe(characters -> System::out); 
    • one
      removeIf (Character :: isSpaceChar); - Artem Konovalov
    • swears at the removeIf method - enotdk
    • java version what? - Artem Konovalov
    • the seventh is obtained if 1.7 - enotdk
    • will not work. The removeIf method appeared in version 1.8 - Artem Konovalov

    Run through the list, if the current element is '' - you delete it soglas.remove ([current index])

    • for(Object listElement : soglas) { if (listElement.equals(" ")) soglas.remove(listElement); } for(Object listElement : soglas) { if (listElement.equals(" ")) soglas.remove(listElement); } - enotdk
    • doesn't work like this - enotdk
    • check for a space using the symbol method .isWhitespace () - sscream
    • @sscream is not why not working. - Qwertiy