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

HashMap support for easy filtering on keys or values

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Minor
    • Resolution: Won't Fix
    • Affects Version/s: 1.7-beta-1
    • Fix Version/s: None
    • Component/s: groovy-jdk
    • Labels:
      None

      Description

      Proposed Enhancements to HashMap:

      • rejectKeys(list of keys)
      • rejectValues(list of values)
      • includeKeys(list of keys)
      • includeValues(list of values)

      Some examples:

      HashMap.metaClass.rejectKeys = {ArrayList keys ->
          delegate.findAll{ k,v -> !keys.contains(k)}
      } 
      
      HashMap.metaClass.rejectValues = {ArrayList values ->
          delegate.findAll{ k,v -> !values.contains(v)}
      } 
      
      HashMap.metaClass.includeKeys = {ArrayList keys ->
          delegate.findAll{ k,v -> keys.contains(k)}
      }
      
      HashMap.metaClass.includeValues = {ArrayList values ->
          delegate.findAll{ k,v -> values.contains(v)}
      } 
      
      
      map = [a:1, b:2, c:3, d:4]
      
      keys = ['a', 'c']
      
      values = [1, 2]
      
      def not_rejected = map.rejectKeys(keys)
      def included = map.includeKeys(keys)
      
      def not_rejected_v = map.rejectValues(values)
      def included_v = map.includeValues(values)
      
      println not_rejected
      println included
      
      println not_rejected_v
      println included_v
      

      with output:

      [b:2, d:4]
      [a:1, c:3]
      [c:3, d:4]
      [a:1, b:2]
      

        Attachments

          Activity

            People

            • Assignee:
              blackdrag Jochen Theodorou
              Reporter:
              kristianm kristianm
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: