You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2016/04/06 13:17:25 UTC

[jira] [Commented] (GROOVY-7809) Create a map from selected properties of an Object

    [ https://issues.apache.org/jira/browse/GROOVY-7809?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15228115#comment-15228115 ] 

Paul King commented on GROOVY-7809:
-----------------------------------

So you want a shorthand for this:
{code}
hero.properties.findAll{ k, v -> k in ['name', 'strength', 'constitution'] }
{code}


> Create a map from selected properties of an Object
> --------------------------------------------------
>
>                 Key: GROOVY-7809
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7809
>             Project: Groovy
>          Issue Type: New Feature
>          Components: syntax
>            Reporter: Donny
>            Priority: Minor
>
> It would be nice if Groovy allows developers to create a map from selected properties of an Object.
> Below are the example:
> {code:title=Example|theme=FadeToGrey|linenumbers=true|language=groovy|firstline=0001|collapse=false}
> class Hero {
>     String name
>     Integer strength
>     Integer constitution
>     String weakness
> }
> Hero hero = new Hero(name: "Robin", strength: 70, constitution: 80, weakness: "Beautiful Women")
> def mapNoWeakness = hero.asMap(['name', 'strength', 'constitution'])
> println mapNoWeakness // [name: 'Robin', strength: 70, constitution: 80]
> def mapOnlyName = hero.asMap(['name'])
> println mapOnlyName // [name: 'Robin']
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)