Java Map 반복(Iteration)시키는 3가지 방법  

이 세가지 방법 말고도 뭐 다른 방법이 있겠지만 대충 요 3가지 정도만 알고 있어도 충분하고도 넘칠것 같다.

이전까지는 제일 첫번째 방법으로만 Map 에 있는 것들을 꺼내서 썼었는데

세번째 방법도 꽤 간결하고 가독성도 좋은것 같아 앞으로 세번째 방법을 주로 써야 겠다는 생각을 해본다.
 

package com.tistory.stove99;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
public class MapIterationSample {
    public static void main(String[] agrs) {
        Map<String, String> map = new HashMap<String, String>();
         
        map.put("키1""값1");
        map.put("키2""값2");
        map.put("키3""값3");
        map.put("키4""값4");
        map.put("키5""값5");
        map.put("키6""값6");
         
         
        // 방법1
        Iterator<String> keys = map.keySet().iterator();
        while( keys.hasNext() ){
            String key = keys.next();
            System.out.println( String.format("키 : %s, 값 : %s", key, map.get(key)) );
        }
         
        // 방법2
        for( Map.Entry<String, String> elem : map.entrySet() ){
            System.out.println( String.format("키 : %s, 값 : %s", elem.getKey(), elem.getValue()) );
        }
         
        // 방법3
        for( String key : map.keySet() ){
            System.out.println( String.format("키 : %s, 값 : %s", key, map.get(key)) );
        }
    }
}



 



출처: http://stove99.tistory.com/96 [스토브 훌로구]

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
270 Spring batch를 Parallel로 돌려보자 졸리운_곰 2019.01.31 399
269 [GC] 강제로 GC시키기Java 메모리 full 발생시 강제로 GC 시키기 졸리운_곰 2019.01.22 382
» Java Map 반복(Iteration)시키는 3가지 방법 졸리운_곰 2019.01.01 348
267 jpa muli row select result is same row repeat Java 자바 Jpa에서 멀티 로우 반환시 같은값이 반복 file 졸리운_곰 2019.01.01 299
266 [자바] 리스트를 순회하는 방법 졸리운_곰 2018.12.31 281
265 SpringBoot JPA 예제 졸리운_곰 2018.12.31 231
264 SpringBoot JPA 예제(1:N, 양방향) 졸리운_곰 2018.12.31 190
263 SpringBoot JPA 예제(결합 인덱스) 졸리운_곰 2018.12.31 237
262 SpringBoot JPA 예제(@ManyToOne, 단방향) 졸리운_곰 2018.12.31 303
261 SpringBoot JPA 예제(@OneToMany, 단방향) 졸리운_곰 2018.12.31 196
260 JPA OneToOne? 자바 jpa join 졸리운_곰 2018.12.31 204
259 Spring Data JPA 연관관계 매핑하는 방법 졸리운_곰 2018.12.31 180
258 Spring MVC 주요 애노테이션(Annotation)정리 file 졸리운_곰 2018.12.29 270
257 스프링/스프링부트 애노테이션(Annotation) 정리 졸리운_곰 2018.12.29 387
256 Spring Framework - annotation 정리 #1 졸리운_곰 2018.12.29 321
255 [Spring] Annotation 정리 졸리운_곰 2018.12.29 275
254 JPA 생성과 수정시 날짜시간 자동삽입 Hibernate generate timestamp on create and update 졸리운_곰 2018.12.13 2099
253 JPA로 insert / update시 날짜시간 자동으로 설정 How to create an auto-generated Date/timestamp field in a Play! / JPA? 졸리운_곰 2018.12.13 975
252 Spring Data repository with empty IN clause. 졸리운_곰 2018.11.16 237
251 Spring Data JPA Tutorial: Introduction to Query Methods 졸리운_곰 2018.11.16 267
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED