You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Guy Matz <gu...@gmail.com> on 2016/06/02 18:18:25 UTC
Looping through a hashmap & removing elements
Hi! I want to loop through a hashmap and delete some elements based on
some criteria . . . I thought there would be some slick groovy method - in
the spirit of findAll, etc. - to do this, but couldn't find it . . . my
java developer workmate suggested:
iter = uidMap.entrySet().iterator()
while (iter.hasNext()) {
entry = iter.next()
key = entry.key
value = entry.value
if (bla, blah, blah) {
iter.remove()
}
Is there a groovier way?
Thanks!
Guy
Re: Looping through a hashmap & removing elements
Posted by emmanuel r <go...@gmail.com>.
Hell yeah there's a Groovy-er (and safer) way:
uidMap = uidMap.findAll { key, value ->
// return true if you want to keep the entry, false to exclude it
}
Notice that this produces a new Map, leaving the original unchanged. Even
though in the example I reuse the variable name.
http://groovy-lang.org/gdk.html
On Jun 2, 2016 2:18 PM, "Guy Matz" <gu...@gmail.com> wrote:
> Hi! I want to loop through a hashmap and delete some elements based on
> some criteria . . . I thought there would be some slick groovy method - in
> the spirit of findAll, etc. - to do this, but couldn't find it . . . my
> java developer workmate suggested:
>
> iter = uidMap.entrySet().iterator()
> while (iter.hasNext()) {
> entry = iter.next()
> key = entry.key
> value = entry.value
>
> if (bla, blah, blah) {
>
> iter.remove()
>
> }
>
>
> Is there a groovier way?
>
>
> Thanks!
>
> Guy
>
>
Re: Looping through a hashmap & removing elements
Posted by Gerald Wiltse <je...@gmail.com>.
Guy,
Please note that removeAll can have dramatic negative impact on your
application if run on a large scale. We recently found that it was the
cause of CPU spikes on most of our JVM's. We had to replace all uses of
"removeAll()" with "findAll()" or "retainAll()"
These do create new list which uses more RAM. However, in many cases these
days, the RAM is less of an issue than the CPU.
Gerald R. Wiltse
jerrywiltse@gmail.com
On Thu, Jun 2, 2016 at 2:56 PM, Guy Matz <gu...@gmail.com> wrote:
> Ahh!! I only saw this removeAll:
> public boolean *removeAll*(Object[]
> <http://docs.oracle.com/javase/7/docs/api/java/lang/Object%5B%5D.html>
> items)
>
> !! Thanks!!!
>
> On Thu, Jun 2, 2016 at 2:27 PM, Winnebeck, Jason <
> Jason.Winnebeck@windstream.com> wrote:
>
>> If you want to actually edit the original map:
>>
>>
>>
>> def uidMap = [
>>
>> a: 123,
>>
>> b: 456,
>>
>> c: 789
>>
>> ]
>>
>>
>>
>> uidMap.entrySet().removeAll { it.key.startsWith('a') }
>>
>>
>>
>> If you want a new map, emmanuel’s solution using findAll is best.
>>
>>
>>
>> Jason
>>
>>
>>
>> *From:* Guy Matz [mailto:guymatz@gmail.com]
>> *Sent:* Thursday, June 02, 2016 2:18 PM
>> *To:* users@groovy.apache.org
>> *Subject:* Looping through a hashmap & removing elements
>>
>>
>>
>> Hi! I want to loop through a hashmap and delete some elements based on
>> some criteria . . . I thought there would be some slick groovy method - in
>> the spirit of findAll, etc. - to do this, but couldn't find it . . . my
>> java developer workmate suggested:
>>
>>
>>
>> iter = uidMap.entrySet().iterator()
>> *while *(iter.hasNext()) {
>> entry = iter.next()
>> key = entry.key
>> value = entry.value
>>
>> if (bla, blah, blah) {
>>
>> iter.remove()
>>
>> }
>>
>>
>>
>> Is there a groovier way?
>>
>>
>>
>> Thanks!
>>
>> Guy
>>
>> ------------------------------
>> This email message and any attachments are for the sole use of the
>> intended recipient(s). Any unauthorized review, use, disclosure or
>> distribution is prohibited. If you are not the intended recipient, please
>> contact the sender by reply email and destroy all copies of the original
>> message and any attachments.
>>
>
>
Re: Looping through a hashmap & removing elements
Posted by Guy Matz <gu...@gmail.com>.
Ahh!! I only saw this removeAll:
public boolean *removeAll*(Object[]
<http://docs.oracle.com/javase/7/docs/api/java/lang/Object[].html> items)
!! Thanks!!!
On Thu, Jun 2, 2016 at 2:27 PM, Winnebeck, Jason <
Jason.Winnebeck@windstream.com> wrote:
> If you want to actually edit the original map:
>
>
>
> def uidMap = [
>
> a: 123,
>
> b: 456,
>
> c: 789
>
> ]
>
>
>
> uidMap.entrySet().removeAll { it.key.startsWith('a') }
>
>
>
> If you want a new map, emmanuel’s solution using findAll is best.
>
>
>
> Jason
>
>
>
> *From:* Guy Matz [mailto:guymatz@gmail.com]
> *Sent:* Thursday, June 02, 2016 2:18 PM
> *To:* users@groovy.apache.org
> *Subject:* Looping through a hashmap & removing elements
>
>
>
> Hi! I want to loop through a hashmap and delete some elements based on
> some criteria . . . I thought there would be some slick groovy method - in
> the spirit of findAll, etc. - to do this, but couldn't find it . . . my
> java developer workmate suggested:
>
>
>
> iter = uidMap.entrySet().iterator()
> *while *(iter.hasNext()) {
> entry = iter.next()
> key = entry.key
> value = entry.value
>
> if (bla, blah, blah) {
>
> iter.remove()
>
> }
>
>
>
> Is there a groovier way?
>
>
>
> Thanks!
>
> Guy
>
> ------------------------------
> This email message and any attachments are for the sole use of the
> intended recipient(s). Any unauthorized review, use, disclosure or
> distribution is prohibited. If you are not the intended recipient, please
> contact the sender by reply email and destroy all copies of the original
> message and any attachments.
>
RE: Looping through a hashmap & removing elements
Posted by "Winnebeck, Jason" <Ja...@windstream.com>.
If you want to actually edit the original map:
def uidMap = [
a: 123,
b: 456,
c: 789
]
uidMap.entrySet().removeAll { it.key.startsWith('a') }
If you want a new map, emmanuel’s solution using findAll is best.
Jason
From: Guy Matz [mailto:guymatz@gmail.com]
Sent: Thursday, June 02, 2016 2:18 PM
To: users@groovy.apache.org
Subject: Looping through a hashmap & removing elements
Hi! I want to loop through a hashmap and delete some elements based on some criteria . . . I thought there would be some slick groovy method - in the spirit of findAll, etc. - to do this, but couldn't find it . . . my java developer workmate suggested:
iter = uidMap.entrySet().iterator()
while (iter.hasNext()) {
entry = iter.next()
key = entry.key
value = entry.value
if (bla, blah, blah) {
iter.remove()
}
Is there a groovier way?
Thanks!
Guy
----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.
Re: Looping through a hashmap & removing elements
Posted by "Nelson, Erick" <Er...@hdsupply.com>.
Does something like this work?
uidMap.keySet().removeAll(uidMap.findAll{it.key == '?'}.keySet())
From: Guy Matz <gu...@gmail.com>>
Reply-To: "users@groovy.apache.org<ma...@groovy.apache.org>" <us...@groovy.apache.org>>
Date: Thursday, June 2, 2016 at 11:18 AM
To: "users@groovy.apache.org<ma...@groovy.apache.org>" <us...@groovy.apache.org>>
Subject: Looping through a hashmap & removing elements
Hi! I want to loop through a hashmap and delete some elements based on some criteria . . . I thought there would be some slick groovy method - in the spirit of findAll, etc. - to do this, but couldn't find it . . . my java developer workmate suggested:
iter = uidMap.entrySet().iterator()
while (iter.hasNext()) {
entry = iter.next()
key = entry.key
value = entry.value
if (bla, blah, blah) {
iter.remove()
}
Is there a groovier way?
Thanks!
Guy