본문 바로가기
개발자의 공부방/프로젝트

프로젝트] Map.put 을 사용할 때 주의해야할 점.

by 쌈빡한 쥬니준 2020. 8. 4.

put 에 대한 것.

Map 이나 오브젝트 구조에 .put() 할 때 동일한 키값이 존재하면 덮어씌워진다.

객체참조, 주소참조

 

아래코드에서 주석처리된 부분은 잘못된 부분이다 근데 API를 호출하면 제대로 결과가 나왔다

이유는...? put()을 할 때 동일한 키값으로 계속 넣고 있었으며,  마지막 for문 밖에서도 동일한 key값으로 넣어서 에러가 없이 되는 것이였다. 즉 에러는 뜨지 않지만 결과적으로는 잘못된 방법으로 코딩을 했던 것...

객체참조와 주소참조를 정확하게 알아야하고 원하는 구조와 원하는 값을 자유자재로 다룰 줄 알아야 한다.

for(Map<String, Object> getBannerImg : kioskBannerList) {
			String introBannerImgPrefix  = getBannerImg.get("introBannerImg").toString();
			String headerBannerImgPrefix = getBannerImg.get("headerBannerImg").toString();
			String footerBannerImgPrefix = getBannerImg.get("footerLogoImg").toString();

			/*if(!introBannerImgSuffix.equals("")) {
				resultData.put(kioskBannerListKey, getBannerImg.put("introBannerImg", frontDomainUrl.concat(introBannerImgSuffix)));
			} if(!headerBannerImgSuffix.equals("")) {
				resultData.put(kioskBannerListKey, getBannerImg.put("headerBannerImg", frontDomainUrl.concat(headerBannerImgSuffix)));
			} if(!footerBannerImgSuffix.equals("")) {
				resultData.put(kioskBannerListKey, getBannerImg.put("footerLogoImg", frontDomainUrl.concat(footerBannerImgSuffix)));
			}
			else {
				resultData.put(kioskBannerListKey, getBannerImg.put("introBannerImg", introBannerImgSuffix));
				resultData.put(kioskBannerListKey, getBannerImg.put("headerBannerImg", headerBannerImgSuffix));
				resultData.put(kioskBannerListKey, getBannerImg.put("footerLogoImg", footerBannerImgSuffix));
			}*/

			//String asd = Optional.ofNullable(getBannerImg.get("introBannerImg")).map(s->(String)s).orElse("");
			//String dasd = frontDomainUrl + asd;

			getBannerImg.put("introBannerImg", "".equals(introBannerImgPrefix) ? introBannerImgPrefix : frontDomainUrl + introBannerImgPrefix);
			getBannerImg.put("headerBannerImg", "".equals(headerBannerImgPrefix) ? headerBannerImgPrefix : frontDomainUrl + headerBannerImgPrefix);
			getBannerImg.put("footerLogoImg", "".equals(footerBannerImgPrefix) ? footerBannerImgPrefix : frontDomainUrl + footerBannerImgPrefix);

			if("".equals(introBannerImgPrefix)) {
				getBannerImg.put("introBannerImg", introBannerImgPrefix);
			} else {
				getBannerImg.put("introBannerImg", frontDomainUrl + introBannerImgPrefix);
			}

			if(headerBannerImgPrefix.equals("")) {
				getBannerImg.put("headerBannerImg", headerBannerImgPrefix);
			} else {
				getBannerImg.put("headerBannerImg", frontDomainUrl + headerBannerImgPrefix);
			}

			if(footerBannerImgPrefix.equals("")) {
				getBannerImg.put("footerLogoImg", footerBannerImgPrefix);
			} else {
				getBannerImg.put("footerLogoImg", frontDomainUrl + footerBannerImgPrefix);
			}
			/*else {
				resultData.put(kioskBannerListKey, getBannerImg.put("introBannerImg", frontDomainUrl.concat(introBannerImgPrefix)));
				resultData.put(kioskBannerListKey, getBannerImg.put("headerBannerImg", frontDomainUrl.concat(headerBannerImgPrefix)));
				resultData.put(kioskBannerListKey, getBannerImg.put("footerLogoImg", frontDomainUrl.concat(footerBannerImgPrefix)));
			}*/
		}

 

@Override
	public Map<String, Object> getKioskBannerInfo(Map<String, Object> parameterMap) {
		Map<String, Object> resultData = new LinkedHashMap<>();

		List<Map<String, Object>> kioskBannerList = ticketingMapper.kioskBannerList(parameterMap);
		List<Map<String, Object>> kioskBannerWinApplyGoodsList = ticketingMapper.kioskBannerWinApplyGoodsList(parameterMap);

		String kioskBannerListKey = "kioskBannerList";
		String frontDomainUrl = ticketDomainUrl+"/ticket/image/";
		log.debug("frontDomainUrl=" + frontDomainUrl);

		for(Map<String, Object> getBannerImg : kioskBannerList) {
			String introBannerImgPrefix  = getBannerImg.get("introBannerImg").toString();
			String headerBannerImgPrefix = getBannerImg.get("headerBannerImg").toString();
			String footerBannerImgPrefix = getBannerImg.get("footerLogoImg").toString();

			getBannerImg.put("introBannerImg", "".equals(introBannerImgPrefix) ? introBannerImgPrefix : frontDomainUrl + introBannerImgPrefix);
			getBannerImg.put("headerBannerImg", "".equals(headerBannerImgPrefix) ? headerBannerImgPrefix : frontDomainUrl + headerBannerImgPrefix);
			getBannerImg.put("footerLogoImg", "".equals(footerBannerImgPrefix) ? footerBannerImgPrefix : frontDomainUrl + footerBannerImgPrefix);
		}

		for(Map<String, Object> getWinGoodsBannerImg : kioskBannerWinApplyGoodsList) {
			String winGoodsBannerImgSuffix = getWinGoodsBannerImg.get("winGoodsBannerImg").toString();
			getWinGoodsBannerImg.put("winGoodsBannerImg", frontDomainUrl + winGoodsBannerImgSuffix);
			//resultData.put("kioskBannerWinApplyGoodsList", getWinGoodsBannerImg); // 이 부분을 해놓으면 결과 값이 이렇게 나온다. 이유는? for문을 돌면서 계속 put을 하니깐 순서가 이상하게 나오는 것.
			
			/*kioskBannerWinApplyGoodsList : []
			kioskBannerList : []*/
			
		}

		resultData.put("kioskBannerList", kioskBannerList);
		resultData.put("kioskBannerWinApplyGoodsList", kioskBannerWinApplyGoodsList);
		return resultData;
	}
	

 

if("".equals(introBannerImgPrefix)) {
   getBannerImg.put("introBannerImg", introBannerImgPrefix);
} else {
   getBannerImg.put("introBannerImg", frontDomainUrl + introBannerImgPrefix);
}

if(headerBannerImgPrefix.equals("")) {
   getBannerImg.put("headerBannerImg", headerBannerImgPrefix);
} else {
   getBannerImg.put("headerBannerImg", frontDomainUrl + headerBannerImgPrefix);
}

if(footerBannerImgPrefix.equals("")) {
   getBannerImg.put("footerLogoImg", footerBannerImgPrefix);
} else {
   getBannerImg.put("footerLogoImg", frontDomainUrl + footerBannerImgPrefix);
}

 

getBannerImg.put("introBannerImg", "".equals(introBannerImgPrefix) ? introBannerImgPrefix : frontDomainUrl + introBannerImgPrefix);
getBannerImg.put("headerBannerImg", "".equals(headerBannerImgPrefix) ? headerBannerImgPrefix : frontDomainUrl + headerBannerImgPrefix);
getBannerImg.put("footerLogoImg", "".equals(footerBannerImgPrefix) ? footerBannerImgPrefix : frontDomainUrl + footerBannerImgPrefix);

댓글0