Java/컬렉션

[Java]HashMap 콘솔에 출력하는 방법

DevStory 2022. 8. 29.

HashMap 콘솔에 출력하는 방법

이번 포스팅은 Java의 HashMap 요소를 콘솔에 출력할 수 있는 몇 가지 방법을 소개합니다.


방법 1. 바로 출력

HashMap의 모든 요소를 콘솔에 출력할 수 있는 가장 간단한 방법으로 HashMap 객체를 println() 메서드에 전달합니다.

public static void main(String args[]) {
  Map<String, Integer> hm = new HashMap<>();
  hm.put("둘리", 10);
  hm.put("또치", 20);
  hm.put("마이콜", 30);

  System.out.println(hm);
}

[실행 결과]

{둘리=10, 마이콜=30, 또치=20}

방법 2. for문과 keySet 메서드

HashMap 클래스에서 제공하는 keySet() 메서드와 for문을 사용하여 HashMap의 요소를 원하는 형식으로 콘솔에 출력할 수 있습니다.

 

keySet() 메서드를 사용하여 키 집합을 순회한 다음 get() 메서드를 사용하여 키와 매핑되는 값을 가져옵니다.

public static void main(String args[]) {
  Map<String, Integer> hm = new HashMap<>();
  hm.put("둘리", 10);
  hm.put("또치", 20);
  hm.put("마이콜", 30);

  for (String key: hm.keySet()){
    System.out.println("{" + key+ " => " + hm.get(key) + "}");
  }
}

[실행 결과]

{둘리 => 10}
{마이콜 => 30}
{또치 => 20}

방법 3. Consumer 인터페이스와 entrySet 메서드

함수형 인터페이스인 Consumer와 entrySet() 메서드를 사용하여 HashMap의 요소를 원하는 형식으로 콘솔에 출력할 수 있습니다.

 

[출력 방법]

순서 1. entrySet() 메서드를 호출하여 Set으로 변환 후 forEach() 메서드를 호출합니다.

순서 2. forEach() 메서드의 매개변수로 Consumer 구현 객체를 전달하거나 람다 표현식을 전달합니다.

순서 3. getKey() 메서드와 getValue() 메서드를 사용하여 키와 값을 출력합니다.

public static void main(String args[]) {
  Map<String, Integer> hm = new HashMap<>();
  hm.put("둘리", 10);
  hm.put("또치", 20);
  hm.put("마이콜", 30);

  hm.entrySet()
    .forEach(item -> System.out.println("{" + item.getKey()+ " => " + item.getValue() + "}"));
}

[실행 결과]

{둘리 => 10}
{마이콜 => 30}
{또치 => 20}

방법 4. Arrays 클래스의 asList 메서드

Arrays 클래스의 asList() 메서드를 사용하여 HashMap 요소를 출력할 수 있습니다.

 

시작과 끝에 대괄호가 추가되는 것을 제외하면, 첫 번째 방법과 다른 점이 없습니다.

public static void main(String args[]) {
  Map<String, Integer> hm = new HashMap<>();
  hm.put("둘리", 10);
  hm.put("또치", 20);
  hm.put("마이콜", 30);

  System.out.println("그냥 출력");
  System.out.println(hm);

  System.out.println("\nArrays.asList() 메서드를 사용하여 출력");
  System.out.println(Arrays.asList(hm));
}

[실행 결과]

그냥 출력
{둘리=10, 마이콜=30, 또치=20}

Arrays.asList() 메서드를 사용하여 출력
[{둘리=10, 마이콜=30, 또치=20}]

방법 5. for문과 Map.Entry, entrySet 메서드

for문과 Map.Entry 그리고 entrySet() 메서드를 사용하여 HashMap의 요소를 출력할 수 있습니다.

 

[출력 방법]

순서 1. HashMap에 entrySet() 메서드를 호출하여 Set으로 변환합니다.

순서 2. Set으로 변환된 결과를 for문을 사용하여 순회합니다.

순서 3. Set의 요소를 Map.Entry로 접근합니다.

순서 4. getKey() 메서드와 getValue() 메서드를 사용하여 키와 값을 출력합니다.

public static void main(String args[]) {
  Map<String, Integer> hm = new HashMap<>();
  hm.put("둘리", 10);
  hm.put("또치", 20);
  hm.put("마이콜", 30);

  for (Map.Entry<String,Integer> item : hm.entrySet()){
    System.out.println("{" + item.getKey() +" => "+item.getValue() + "}");
  }
}

[실행 결과]

{둘리 => 10}
{마이콜 => 30}
{또치 => 20}

방법 6. Collections의 singletonList 메서드

Collections 클래스의 정적 메서드 singletonList()를 사용하여 HashMap 요소를 출력할 수 있습니다.

 

Arrays 클래스의 asList() 메서드를 사용하는 방법과 유사합니다.

public static void main(String args[]) {
  Map<String, Integer> hm = new HashMap<>();
  hm.put("둘리", 10);
  hm.put("또치", 20);
  hm.put("마이콜", 30);

  System.out.println(Collections.singletonList(hm));
}

[실행 결과]

[{둘리=10, 마이콜=30, 또치=20}]
반응형

댓글