You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by "Winnebeck, Jason" <Ja...@windstream.com> on 2015/04/28 20:16:29 UTC

Collection/Map Casting

Is there an elegant way to cast a collection's types?

[1,2,3] as List<String> //doesn't actually work
[1,2,3].collect { it as String } //works but more verbose

[a:1, b:2].collectEntries { k,v -> [k, v as String] } //is there a better way?

The other issue with collectEntries is that the @CompileStatic doesn't know the type returned, so you have to actually cast it twice.

Jason

----------------------------------------------------------------------
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: Collection/Map Casting

Posted by "Winnebeck, Jason" <Ja...@windstream.com>.
Ok, so that is a shorthand for the collect method. But what about to “cast” a Map? I can’t use *. operator on maps?

Jason

From: Shil Sinha [mailto:shil.sinha@gmail.com]
Sent: Tuesday, April 28, 2015 2:45 PM
To: users@groovy.incubator.apache.org
Subject: Re: Collection/Map Casting

For your first example, you could do one of the following:

[1,2,3]*.asType(String), which is equivalent to but slower than
[1,2,3]*.toString()

Generally, collection*.asType(ClassYouWant) will yield the element type changes you want, but you will have to cast the resulting ArrayList to the type of collection you want.

On Tue, Apr 28, 2015 at 2:16 PM, Winnebeck, Jason <Ja...@windstream.com>> wrote:
Is there an elegant way to cast a collection’s types?

[1,2,3] as List<String> //doesn’t actually work
[1,2,3].collect { it as String } //works but more verbose

[a:1, b:2].collectEntries { k,v -> [k, v as String] } //is there a better way?

The other issue with collectEntries is that the @CompileStatic doesn’t know the type returned, so you have to actually cast it twice.

Jason
________________________________
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: Collection/Map Casting

Posted by Shil Sinha <sh...@gmail.com>.
For your first example, you could do one of the following:

[1,2,3]*.asType(String), which is equivalent to but slower than
[1,2,3]*.toString()

Generally, collection*.asType(ClassYouWant) will yield the element type
changes you want, but you will have to cast the resulting ArrayList to the
type of collection you want.

On Tue, Apr 28, 2015 at 2:16 PM, Winnebeck, Jason <
Jason.Winnebeck@windstream.com> wrote:

>  Is there an elegant way to cast a collection’s types?
>
>
>
> [1,2,3] as List<String> //doesn’t actually work
>
> [1,2,3].collect { it as String } //works but more verbose
>
>
>
> [a:1, b:2].collectEntries { k,v -> [k, v as String] } //is there a better
> way?
>
>
>
> The other issue with collectEntries is that the @CompileStatic doesn’t
> know the type returned, so you have to actually cast it twice.
>
>
>
> Jason
>  ------------------------------
> 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.
>