Saturday, 26 January 2019

Groovy: List: remove element from the list


Groovy provides number of ways to remove an element from the list.
a.   Using removeAt method
b.   Using removeElement method
c.    Using removeAll method
d.   Using minus method
e.   Using – operator
f.     Using pop method

Using removeAt method
public Object removeAt(int index)
This method removes the element from the list and return the removed element.

HelloWorld.groovy
primes = [2, 3, 5, 7, 11, 13, 17, 19]

println primes

removedElement = primes.removeAt(4)

println "Removed Element : $removedElement"
println primes

Output
[2, 3, 5, 7, 11, 13, 17, 19]
Removed Element : 11
[2, 3, 5, 7, 13, 17, 19]

Using removeElement method
public boolean removeElement(Object o)
This method removes the element from list. It returns true, if the element removed from list, else false.


HelloWorld.groovy
primes = [2, 3, 5, 7, 11, 13, 17, 19]

println primes

boolean isRemoved = primes.removeElement(17)

if(isRemoved){
 println "17 is removed from list"
}else{
 println "17 is not found from list"
}

println primes

Output
[2, 3, 5, 7, 11, 13, 17, 19]
17 is removed from list
[2, 3, 5, 7, 11, 13, 19]

Using removeAll method
public boolean removeAll(Closure condition)
Remove the elements from list that satisfies given condition.

public boolean removeAll(Object[] items)
Remove all the items from given list.


HelloWorld.groovy
numbers = [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
println "$numbers"

println "\nRemoving all the elements that are < 3"
numbers.removeAll{it < 3}
println "$numbers"

println "\nRemoving the elements [3, 10]"
numbers.removeAll([3, 10])
println "$numbers"

Output
[0, 1, 2, 3, 4, 5, 3, 2, 1, 10]

Removing all the elements that are < 3
[3, 4, 5, 3, 10]

Removing the elements [3, 10]
[4, 5]

Using minus method
public Collection minus(Iterable removeMe)
Creates new collection by removing given elements from this list

public Collection minus(Object removeMe)
Creates new collection by removing given element.

HelloWorld.groovy

numbers = [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
println "numbers: $numbers"

println "Getting new list by removing [0, 2, 4, 6, 8, 10]"
oddNumbers = numbers.minus([0, 2, 4, 6, 8, 10])
println "\nnumbers: $numbers"
println "oddNumbers: $oddNumbers"

println "Removing 0 from numbers"
exceptZero = numbers.minus(0)
println "\nnumbers: $numbers"
println "exceptZero: $exceptZero"

Output
numbers: [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
Getting new list by removing [0, 2, 4, 6, 8, 10]

numbers: [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
oddNumbers: [1, 3, 5, 3, 1]
Removing 0 from numbers

numbers: [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
exceptZero: [1, 2, 3, 4, 5, 3, 2, 1, 10]

Using minus operator
It implicitly calls minus method.


HelloWorld.groovy
numbers = [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
println "numbers: $numbers"

println "Getting new list by removing [0, 2, 4, 6, 8, 10]"
oddNumbers = numbers - ([0, 2, 4, 6, 8, 10])
println "\nnumbers: $numbers"
println "oddNumbers: $oddNumbers"

println "Removing 0 from numbers"
exceptZero = numbers - 0
println "\nnumbers: $numbers"
println "exceptZero: $exceptZero"

Output
numbers: [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
Getting new list by removing [0, 2, 4, 6, 8, 10]

numbers: [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
oddNumbers: [1, 3, 5, 3, 1]
Removing 0 from numbers

numbers: [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
exceptZero: [1, 2, 3, 4, 5, 3, 2, 1, 10]

Using pop method
public Object pop()
pop() method removes the first element from list.


HelloWorld.groovy
numbers = [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
int length = numbers.size()

println "numbers : $numbers"
for(int i = 0; i < length; i++){
 removed = numbers.pop()
 println "removed : $removed"
 println "numbers : $numbers"
}

Output
numbers : [0, 1, 2, 3, 4, 5, 3, 2, 1, 10]
removed : 0
numbers : [1, 2, 3, 4, 5, 3, 2, 1, 10]
removed : 1
numbers : [2, 3, 4, 5, 3, 2, 1, 10]
removed : 2
numbers : [3, 4, 5, 3, 2, 1, 10]
removed : 3
numbers : [4, 5, 3, 2, 1, 10]
removed : 4
numbers : [5, 3, 2, 1, 10]
removed : 5
numbers : [3, 2, 1, 10]
removed : 3
numbers : [2, 1, 10]
removed : 2
numbers : [1, 10]
removed : 1
numbers : [10]
removed : 10
numbers : []






Previous                                                 Next                                                 Home

No comments:

Post a Comment