Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7809

Shortcut when creating a map from selected properties of an Object

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • None
    • None
    • syntax
    • None

    Description

      It would be nice if Groovy allows developers to create a map from selected properties of an Object.

      Below are the example:

      Example
      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']
      
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            dodol Donny
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: