본문 바로가기
개발&IT

groovy if conains key and update value

 

def joinTypeMap=[:]
if ( joinTypeMap.containsKey(type)){
	def value = joinTypeMap.type
    joinTypeMap.type = value + count
 }else{
     joinTypeMap.type = count
 }

길다.. 

 

간단하게

def value = joinTypeMap[type] ?: 0
joinTypeMap[type] = value + count

주의점은 ?: 문구이다. ? : 띄어쓰기 하면 안 된다.

0은 defalut value이다.

 

'개발&IT' 카테고리의 다른 글

구글 포토 PC로 백업. 문제있다.  (0) 2021.08.19
Git tag 일괄 삭제  (0) 2020.09.02
parsing된 xml를 다시 xml 원복하기  (0) 2020.04.17
groovy xml element 삭제  (0) 2020.04.17
OKHttp Client 단점  (0) 2020.01.20