Skip to content

Instantly share code, notes, and snippets.

@marcusvoltolim
Last active April 26, 2022 17:45
Show Gist options
  • Save marcusvoltolim/6b34e2354359443c13bdb79559195ef8 to your computer and use it in GitHub Desktop.
Save marcusvoltolim/6b34e2354359443c13bdb79559195ef8 to your computer and use it in GitHub Desktop.
//##### 1. Removendo elemento pela posição/índice
List<Integer> lista = new ArrayList<>(List.of(1, 2));
lista.remove(1); // retorna 2. Lista resultante [1]
lista.remove(0); // retorna 1. Lista resultante vazia []
//##### 2. Removendo elemento
List<Integer> lista2 = new ArrayList<>(List.of(1, 2));
lista2.remove((Integer) 1); // retorna true. Lista resultante [2]
lista2.remove((Integer) 0); // retorna false. Lista resultante [2]
lista2.remove((Integer) 2); // retorna true. Lista resultante vazia []
//##### 3. Removendo vários elementos (remoção em lote)
List<Integer> lista3 = new ArrayList<>(List.of(1, 2, 3));
lista3.removeAll(List.of(4, 5, 6)); // retorna false. Lista resultante [1, 2, 3]
lista3.removeAll(List.of(2)); // retorna true. Lista resultante [1, 3]
lista3.removeAll(List.of(1, 2, 3)); // retorna true. Lista resultante vazia []
lista3.removeAll(List.of(1, 2, 3)); // retorna false. Lista resultante vazia []
//##### 4. Removendo vários elementos com condição [lambda] (remoção condicional em lote)
List<Integer> lista3 = new ArrayList<>(List.of(1, 2, 3));
lista3.removeIf(e -> e > 3); // remove os numero maiores que 3, nesse caso nenhum e retorna false. Lista resultante [1, 2, 3]
lista3.removeIf(e -> e < 3); // remove os numero menores que 3 (1 e 2) e retorna true. Lista resultante [3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment