読者です 読者をやめる 読者になる 読者になる

Groovyでmapで値を取得する方法のまとめ

groovy

mapで値を取得する方法のまとめ

8)APIで取得したスター数をはてなスターカウンターに描く9)はてブカウンターと同じ15色に対応する

試しているうちに、Groovyでは値の取得する方法は多数あることがわかった。

  • Javaのmapのようなgetメソッドを使う
  • getter
  • 配列風
  • プロパティ風

mapに対して

  • orangeMap.get("highlight")
  • orangeMap.getbody
  • orangeMap['shadow']
  • orangeMap.star

mapの中にmap

  • ColorMap.get(myColor).get("highlight")
  • ColorMap[myColor]["highlight"]
  • ColorMap[myColor].shadow
  • ColorMap."$myColor".star

可変にするためには、"$myColor"にする。

それぞれ最後がスマートだと思う。

ソース

orangeMap = ["highlight":'#ffd583', "body":'#ff9429', "shadow":'#ff7b18', "star":'#ffdea4']
ColorMap=["deepblue":  ["highlight":'#839ce6', "body":'#1841cd', "shadow":'#10205a', "star":'#83a4ff']
        , "darkgray":  ["highlight":'#949ca4', "body":'#6a6a7b', "shadow":'#414a52', "star":'#cdcdcd']
        , "green:":    ["highlight":'#73bdc5', "body":'#4194a4', "shadow":'#206a73', "star":'#b4f6de']
        , "purple":    ["highlight":'#9473c5', "body":'#6a39a4', "shadow":'#41207b', "star":'#b49ce6']
        , "brown":     ["highlight":'#bd8362', "body":'#9c5a31', "shadow":'#733918', "star":'#e6b494']
        , "red":       ["highlight":'#e65a52', "body":'#cd2018', "shadow":'#9c0000', "star":'#ff9494']
        , "sepia":     ["highlight":'#eed5ac', "body":'#debd6a', "shadow":'#bd8341', "star":'#ffeed5']
        , "pink":      ["highlight":'#f6bdbd', "body":'#ee8383', "shadow":'#cd5a62', "star":'#ffdede']
        , "tea":       ["highlight":'#d5e6cd', "body":'#b4cda4', "shadow":'#94ac7b', "star":'#eef6ee']
        , "lightgray": ["highlight":'#deded5', "body":'#bdbdb4', "shadow":'#9c9494', "star":'#f6f6f6']
        , "lightblue": ["highlight":'#d5e6ff', "body":'#8bb4ff', "shadow":'#6294ee', "star":'#eef6ff']
        , "white":     ["highlight":'#ffffff', "body":'#ffffff', "shadow":'#b4b4b4', "star":'#cdcdcd']
        , "black":     ["highlight":'#b4b4b4', "body":'#525252', "shadow":'#000000', "star":'#b4b4b4']
        , "lime":      ["highlight":'#bdffde', "body":'#5ae6a4', "shadow":'#41bd83', "star":'#deffee']
        , "orange":    ["highlight":'#ffd583', "body":'#ff9429', "shadow":'#ff7b18', "star":'#ffdea4']
]


println "-------各メンバーを取得する-------"


println "orange.highlight=" + orangeMap.get("highlight")
println "orangeMap.body=" + orangeMap.getbody
println "orangeMap.shadow=" + orangeMap['shadow']
println "orangeMap.star=" + orangeMap.star

myColor="orange"
println  "myColor=" +  myColor
println  "myColor.highlight="  + ColorMap.get(myColor).get("highlight")
println  "myColor.body="       + ColorMap[myColor]["highlight"] 
println  "myColor.shadow="     + ColorMap[myColor].shadow
println  "myColor.star="       + ColorMap."$myColor".star
/* NG 
assert println  "myColor.star="       + ColorMap.myColor.star

ERROR java.lang.NullPointerException: Cannot get property 'star' on null object
        at groovysh_evaluate.run (groovysh_evaluate:2)
        ...
        
println  "myColor.star="       + ColorMap.@myColor.star
ERROR groovy.lang.MissingFieldException: No such field: myColor for class: java.util.LinkedHashMap
        at groovysh_evaluate.run (groovysh_evaluate:2)
        ...
        
println  "myColor.star="       + ColorMap.$myColor.star
ERROR java.lang.NullPointerException: Cannot get property 'star' on null object
        at groovysh_evaluate.run (groovysh_evaluate:2)
        ...
*/


println "-------全メンバーを取得する-------"


orangeMap.each{key, value ->
    println key + "=" + value
}


ColorMap.each{ color, colorMap -> 
    colorMap.each{key, value ->
            println color + "." + key + "=" + value
    }
}

結果

groovy:000> load mapの値を取得する.groovy
===> {highlight=#ffd583, body=#ff9429, shadow=#ff7b18, star=#ffdea4}
===> {deepblue={highlight=#839ce6, body=#1841cd, shadow=#10205a, star=#83a4ff}, darkgray={highlight=#949ca4, body=#6a6a7b, shadow=#414a52, star=#cdcdcd}, green:={highlight=#73bdc5, body=#4194a4, shadow=#206a73, star=#b4f6de}, purple={highlight=#9473c5, body=#6a39a4, shadow=#41207b, star=#b49ce6}, brown={highlight=#bd8362, body=#9c5a31, shadow=#733918, star=#e6b494}, red={highlight=#e65a52, body=#cd2018, shadow=#9c0000, star=#ff9494}, sepia={highlight=#eed5ac, body=#debd6a, shadow=#bd8341, star=#ffeed5}, pink={highlight=#f6bdbd, body=#ee8383, shadow=#cd5a62, star=#ffdede}, tea={highlight=#d5e6cd, body=#b4cda4, shadow=#94ac7b, star=#eef6ee}, lightgray={highlight=#deded5, body=#bdbdb4, shadow=#9c9494, star=#f6f6f6}, lightblue={highlight=#d5e6ff, body=#8bb4ff, shadow=#6294ee, star=#eef6ff}, white={highlight=#ffffff, body=#ffffff, shadow=#b4b4b4, star=#cdcdcd}, black={highlight=#b4b4b4, body=#525252, shadow=#000000, star=#b4b4b4}, lime={highlight=#bdffde, body=#5ae6a4, shadow=#41bd83, star=#deffee}, orange={highlight=#ffd583, body=#ff9429, shadow=#ff7b18, star=#ffdea4}}
-------各メンバーを取得する-------
===> null
orange.highlight=#ffd583
===> null
orangeMap.body=null
===> null
orangeMap.shadow=#ff7b18
===> null
orangeMap.star=#ffdea4
===> null
===> orange
myColor=orange
===> null
myColor.highlight=#ffd583
===> null
myColor.body=#ffd583
===> null
myColor.shadow=#ff7b18
===> null
myColor.star=#ffdea4
===> null
===> true
-------全メンバーを取得する-------
===> null
highlight=#ffd583
body=#ff9429
shadow=#ff7b18
star=#ffdea4
===> {highlight=#ffd583, body=#ff9429, shadow=#ff7b18, star=#ffdea4}
deepblue.highlight=#839ce6
deepblue.body=#1841cd
deepblue.shadow=#10205a
deepblue.star=#83a4ff
darkgray.highlight=#949ca4
darkgray.body=#6a6a7b
darkgray.shadow=#414a52
darkgray.star=#cdcdcd
green:.highlight=#73bdc5
green:.body=#4194a4
green:.shadow=#206a73
green:.star=#b4f6de
purple.highlight=#9473c5
purple.body=#6a39a4
purple.shadow=#41207b
purple.star=#b49ce6
brown.highlight=#bd8362
brown.body=#9c5a31
brown.shadow=#733918
brown.star=#e6b494
red.highlight=#e65a52
red.body=#cd2018
red.shadow=#9c0000
red.star=#ff9494
sepia.highlight=#eed5ac
sepia.body=#debd6a
sepia.shadow=#bd8341
sepia.star=#ffeed5
pink.highlight=#f6bdbd
pink.body=#ee8383
pink.shadow=#cd5a62
pink.star=#ffdede
tea.highlight=#d5e6cd
tea.body=#b4cda4
tea.shadow=#94ac7b
tea.star=#eef6ee
lightgray.highlight=#deded5
lightgray.body=#bdbdb4
lightgray.shadow=#9c9494
lightgray.star=#f6f6f6
lightblue.highlight=#d5e6ff
lightblue.body=#8bb4ff
lightblue.shadow=#6294ee
lightblue.star=#eef6ff
white.highlight=#ffffff
white.body=#ffffff
white.shadow=#b4b4b4
white.star=#cdcdcd
black.highlight=#b4b4b4
black.body=#525252
black.shadow=#000000
black.star=#b4b4b4
lime.highlight=#bdffde
lime.body=#5ae6a4
lime.shadow=#41bd83
lime.star=#deffee
orange.highlight=#ffd583
orange.body=#ff9429
orange.shadow=#ff7b18
orange.star=#ffdea4
===> {deepblue={highlight=#839ce6, body=#1841cd, shadow=#10205a, star=#83a4ff}, darkgray={highlight=#949ca4, body=#6a6a7b, shadow=#414a52, star=#cdcdcd}, green:={highlight=#73bdc5, body=#4194a4, shadow=#206a73, star=#b4f6de}, purple={highlight=#9473c5, body=#6a39a4, shadow=#41207b, star=#b49ce6}, brown={highlight=#bd8362, body=#9c5a31, shadow=#733918, star=#e6b494}, red={highlight=#e65a52, body=#cd2018, shadow=#9c0000, star=#ff9494}, sepia={highlight=#eed5ac, body=#debd6a, shadow=#bd8341, star=#ffeed5}, pink={highlight=#f6bdbd, body=#ee8383, shadow=#cd5a62, star=#ffdede}, tea={highlight=#d5e6cd, body=#b4cda4, shadow=#94ac7b, star=#eef6ee}, lightgray={highlight=#deded5, body=#bdbdb4, shadow=#9c9494, star=#f6f6f6}, lightblue={highlight=#d5e6ff, body=#8bb4ff, shadow=#6294ee, star=#eef6ff}, white={highlight=#ffffff, body=#ffffff, shadow=#b4b4b4, star=#cdcdcd}, black={highlight=#b4b4b4, body=#525252, shadow=#000000, star=#b4b4b4}, lime={highlight=#bdffde, body=#5ae6a4, shadow=#41bd83, star=#deffee}, orange={highlight=#ffd583, body=#ff9429, shadow=#ff7b18, star=#ffdea4}}

参考

  • Groovy イン・アクション p95〜p100

Map に対する繰り返し処理




最近のキーワード










Groovyの詳細についてはJavadocと以下の書籍を参考にしている。

Groovyイン・アクション
Dierk Konig Andrew Glover Paul King Guillaume Laforge Jon Skeet
毎日コミュニケーションズ
売り上げランキング: 294340



Groovyイン・アクションを読むならあった方が便利かな。

ブックストッパー

トモエ算盤
売り上げランキング: 614




広告を非表示にする