Last active
April 26, 2022 17:58
-
-
Save marcusvoltolim/88a703131236e359707b130ed3e3f0c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//##### 1. Removendo elemento pela posição/índice | |
List<Integer> lista = new ArrayList<>(List.of(1, 2)) | |
lista.removeAt(1) // retorna 2. Lista resultante [1] | |
lista.removeAt(0) // retorna 1. Lista resultante vazia [] | |
//#### 2. Removento elemento com método removeElement | |
List<Integer> lista2 = new ArrayList<>(List.of(1, 2)) | |
lista2.removeElement(1) // retorna true. Lista resultante [2] | |
lista2.removeElement(0) // retorna false. Lista resultante [2] | |
lista2.removeElement(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(4, 5, 6) // retorna false. Lista resultante [1, 2, 3] | |
lista3.removeAll(2) // retorna true. Lista resultante [1, 3] | |
lista3.removeAll(1, 2, 3) // retorna true. Lista resultante vazia [] | |
lista3.removeAll(1, 2, 3) // retorna false. Lista resultante vazia [] | |
//##### 4. Removendo vários elementos com condição [closure] (remoção condicional em lote) | |
List<Integer> lista4 = new ArrayList<>(List.of(1, 2, 3)) | |
lista4.removeAll(e -> e > 3) // remove os numero maiores que 3, nesse caso nenhum e retorna false. Lista resultante [1, 2, 3] | |
lista4.removeAll(e -> e < 3) // remove os numero menores que 3 (1 e 2) e retorna true. Lista resultante [3] | |
//#### | |
List<Integer> lista4_1 = new ArrayList<>(List.of(1, 2, 3)) | |
lista4_1.removeAll { it > 3 } // usando sintaxe default de Closure do Groovy | |
lista4_1.removeAll { it < 3 } // usando sintaxe default de Closure do Groovy | |
//##### 5. Removendo elemento com método minus ou operador - | |
List<Integer> lista5 = new ArrayList<>(List.of(1, 2, 3)) | |
lista5.minus(4) // Lista resultante [1, 2, 3] | |
lista5 - 4 // Lista resultante [1, 2, 3] | |
lista5.minus(1).minus(2) // Lista resultante [ 3] | |
lista5 - 2 - 1 // Lista resultante [3] | |
//##### 6. Removendo vários elementos (remoção em lote) com método minus ou operador - | |
List<Integer> lista6 = new ArrayList<>(List.of(1, 2, 3)) | |
lista6.minus([4, 5]) // Lista resultante [1, 2, 3] | |
lista6 - [4, 5] // Lista resultante [1, 2, 3] | |
lista6.minus([2, 1, 5]) // Lista resultante [3] | |
lista6 - [2, 1, 5] // Lista resultante [3] | |
//##### Extra. Intercalando minus(item) e minus(Collection) | |
List<Integer> listaExtra = new ArrayList<>(List.of(1, 2, 3)) | |
listaExtra - [2, 1] - [5, 6] - 3 // Lista resultante vazia [] | |
listaExtra - [2, 1] - [5, 6] - [3] // Lista resultante vazia [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment