๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] Map ์ธํ„ฐํŽ˜์ด์Šค

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 22.
๋ฐ˜์‘ํ˜•

โ–ถ Map ์ธํ„ฐํŽ˜์ด์Šค

Map ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ž๋ฃŒ๋ฅผ ์Œ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

Key-value ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฐ์ฒด์˜ key ๊ฐ’์€ ์œ ์ผํ•˜๋ฉฐ value ๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์žˆ๋‹ค.

Map ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์˜ํ•ด ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.

โ€‹

โ€‹

โ€‹

*HashMap ํด๋ž˜์Šค

HashMap์€ Map ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค ์ค‘ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

HashMap์—์„œ ์ž๋ฃŒ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์€ 'ํ•ด์‹œ ๋ฐฉ์‹'์ด๋‹ค.

ํ•ด์‹œ ๋ฐฉ์‹์˜ ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„์„ 'ํ•ด์‹œ ํ…Œ์ด๋ธ”'์ด๋ผ ํ•œ๋‹ค.

key ๊ฐ’์ด ์ •ํ•ด์ง€๋ฉด ๊ทธ์— ๋Œ€์‘ํ•˜๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ์ €์žฅ ์œ„์น˜๊ฐ€ ์ •ํ•ด์ง€๋Š”๋ฐ ์ด๋Ÿฐ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ 'ํ•ด์‹œ ํ•จ์ˆ˜'์ด๋‹ค.

์ž๋ฃŒ ์ถ”๊ฐ€ ์†๋„๋‚˜ ๊ฒ€์ƒ‰ ์†๋„๊ฐ€ ์ƒ๋‹นํžˆ ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

์„œ๋กœ ๋‹ค๋ฅธ key ๊ฐ’์— ๊ฐ™์€ index(์ €์žฅ ์œ„์น˜)๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ฝ‰ ์ฑ„์šฐ์ง€ ์•Š๊ณ  ์ ์ • ์ˆ˜์ค€์ด ๋˜๋ฉด ํ…Œ์ด๋ธ”์„ ํ™•์žฅํ•ด ์ถฉ๋Œ ๋ฐœ์ƒ ํ™•๋ฅ ์„ ๋‚ฎ์ถ˜๋‹ค.

๋˜ํ•œ, Map ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์‚ฌ์šฉํ•˜๋Š” key ๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ equals( ) ๋ฉ”์„œ๋“œ์™€ hashcode( ) ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

โ€‹

โ˜… key ๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์—†๊ณ  value ๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์žˆ๋‹ค.

โ€‹

โ€‹

โ€‹

*HashMap์„ ํ™œ์šฉํ•ด ํšŒ์› ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ํ•˜๊ธฐ

โ€‹

MemberHashMap.java

package map.hashmap;

import java.util.HashMap;
import java.util.Iterator;

import collection.Member;

public class MemberHashMap {
	private HashMap<Integer, Member> hashMap;

	public MemberHashMap() {
		hashMap = new HashMap<Integer, Member>();
	}

	public void addMember(Member member) {          // key-value ์Œ์œผ๋กœ ์ถ”๊ฐ€
		hashMap.put(member.getMemberID(), member);  // HashMap์— ํšŒ์›์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ
	}

	public boolean removeMember(int memberID) {  // HashMap์—์„œ ํšŒ์›์„ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ
		if (hashMap.containsKey(memberID)) {     // HashMap์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ํ‚ค ๊ฐ’์ธ ํšŒ์› ์•„์ด๋””๊ฐ€ ์žˆ๋‹ค๋ฉด
			hashMap.remove(memberID);            // ํ•ด๋‹น ํšŒ์› ์‚ญ์ œ
			return true;
		}
		System.out.println(memberID + "๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		return false;
	}

	public void showAllMember() {  // Iterator๋ฅผ ์‚ฌ์šฉํ•ด ์ „์ฒด ํšŒ์›์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์„œ๋“œ
		Iterator<Integer> ir = hashMap.keySet().iterator();
		while (ir.hasNext()) {                  // ๋‹ค์Œ key๊ฐ€ ์žˆ์œผ๋ฉด
			int key = ir.next();                // key ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ
			Member member = hashMap.get(key);   // key๋กœ๋ถ€ํ„ฐ value ๊ฐ€์ ธ์˜ค๊ธฐ
			System.out.println(member);
		}
		System.out.println();
	}
}

 

 

MemberHashMapTest.java

package map.hashmap;

import collection.Member;

public class MemberHashMapTest {
	public static void main(String[] args) {
		MemberHashMap memberHashMap = new MemberHashMap();

		Member memberLee = new Member(1001, "์ด์ง€์›");
		Member memberSon = new Member(1002, "์†๋ฏผ๊ตญ");
		Member memberPark = new Member(1003, "๋ฐ•์„œํ›ค");
		Member memberHong = new Member(1004, "ํ™๊ธธ๋™");

		memberHashMap.addMember(memberLee);
		memberHashMap.addMember(memberSon);
		memberHashMap.addMember(memberPark);
		memberHashMap.addMember(memberHong);

		memberHashMap.showAllMember();

		memberHashMap.removeMember(1004);  // ํšŒ์› ์•„์ด๋””(key ๊ฐ’)๊ฐ€ 1004์ธ ํšŒ์› ์‚ญ์ œ
		memberHashMap.showAllMember();
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

*TreeMap ํด๋ž˜์Šค

Map ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค ์ค‘ key ๊ฐ’์œผ๋กœ ์ž๋ฃŒ๋ฅผ ์ •๋ ฌํ•˜๋ ค๋ฉด TreeMap์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

TreeMap์€ TreeSet๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ๋กœ ๊ตฌํ˜„๋˜์—ˆ๋‹ค.

key ๊ฐ’์œผ๋กœ ์ •๋ ฌํ•˜๋ฏ€๋กœ key ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค์— Comparable์ด๋‚˜ Comparator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

โ€‹โ€‹

MemberTreeMap.java

package map.treemap;

import java.util.Iterator;
import java.util.TreeMap;

import collection.Member;

public class MemberTreeMap {
	private TreeMap<Integer, Member> treeMap;

	public MemberTreeMap() {
		treeMap = new TreeMap<Integer, Member>();
	}

	public void addMember(Member member) {
		treeMap.put(member.getMemberID(), member);  // key-value ์Œ์œผ๋กœ ์ถ”๊ฐ€
	}

	public boolean removeMember(int memberID) {
		if (treeMap.containsKey(memberID)) {
			treeMap.remove(memberID);  // key ๊ฐ’์— ๋งž๋Š” ์ž๋ฃŒ ์‚ญ์ œ
			return true;
		}
		System.out.println(memberID + "๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		return false;
	}

	public void showAllMember() {
		Iterator<Integer> ir = treeMap.keySet().iterator();
		while (ir.hasNext()) {
			int key = ir.next();
			Member member = treeMap.get(key);
			System.out.println(member);
		}
		System.out.println();
	}
}

-ํ‚ค ๊ฐ’์„ keySet( ) ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฝ‘์•„ Iterator๋ฅผ ์ด์šฉํ•˜์—ฌ ์ €์žฅ๋œ ๊ฐ’์„ ๋ฝ‘์•„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

โ€‹

โ€‹

MemberTreeMapTest.java

package map.treemap;

import collection.Member;

public class MemberTreeMapTest {
	public static void main(String[] args) {
		MemberTreeMap memberTreeMap = new MemberTreeMap();

		Member memberPark = new Member(1003, "๋ฐ•์„œํ›ค");
		Member memberLee = new Member(1001, "์ด์ง€์›");
		Member memberHong = new Member(1004, "ํ™๊ธธ๋™");
		Member memberSon = new Member(1002, "์†๋ฏผ๊ตญ");  // ํšŒ์› ์•„์ด๋”” ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด ํšŒ์› ์ถ”๊ฐ€

		memberTreeMap.addMember(memberPark);
		memberTreeMap.addMember(memberLee);
		memberTreeMap.addMember(memberHong);
		memberTreeMap.addMember(memberSon);

		memberTreeMap.showAllMember();

		memberTreeMap.removeMember(1004);  // ํšŒ์› ์•„์ด๋””(key ๊ฐ’)๊ฐ€ 1004์ธ ํšŒ์› ์‚ญ์ œ
		memberTreeMap.showAllMember();
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

 

 

์—ฐ์Šต๋ฌธ์ œ Q5) StudentTest์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค์Œ์ฒ˜๋Ÿผ ๋‚˜์˜ค๋„๋ก Student ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๋ผ.

์กฐ๊ฑด. 100:์†ก์ค‘๊ธฐ ๊ฐ€ ์ถœ๋ ฅ๋˜์ง€์•Š๊ฒŒ ํ•  ๊ฒƒ

โ€‹

1. studentID ๋ฅผ int ํ˜•์œผ๋กœ ํ•˜๊ธฐ

โ€‹โ€‹

Student.java

package test;

public class Student {
	private String studentID;
	private String studentName;

	public Student(String studentID, String studentName) {
		this.studentID = studentID;
		this.studentName = studentName;
	}
	
	public int hashCode() {
		return studentID;
	}
	
	public String toString() {
		return studentID + ":" + studentName;
	}
	
	public boolean equals(Object obj) {
		if (obj instanceof Student) {
			Student student = (Student) obj;
			if (this.studentID == student.studentID)
				return true;
			else
				return false;
		}
		return false;
	}
}

 

 

StudentTest.java

package test;

import java.util.HashSet;

public class StudentTest {
	public static void main(String[] args) {
		HashSet<Student> set = new HashSet<Student>();

		set.add(new Student("100", "ํ™๊ธธ๋™"));
		set.add(new Student("200", "๊ฐ•๊ฐ์ฐฌ"));
		set.add(new Student("300", "์ด์ˆœ์‹ "));
		set.add(new Student("400", "์ •์•ฝ์šฉ"));
		set.add(new Student("100", "์†ก์ค‘๊ธฐ"));

		System.out.println(set);
	}
}

โ€‹<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

2. studentID๋ฅผ Stringํ˜•์œผ๋กœ ํ•˜๊ธฐ

โ€‹โ€‹

Student.java

package test;

public class Student {
	private String studentID;
	private String studentName;

	public Student(String studentID, String studentName) {
		this.studentID = studentID;
		this.studentName = studentName;
	}
	
	public int hashCode() {
		return Integer.parseInt(studentID);
	}
	
	public String toString() {
		return studentID + ":" + studentName;
	}
	
	public boolean equals(Object obj) {
		if (obj instanceof Student) {
			Student student = (Student) obj;
			if (this.studentID.equals(student.studentID))
				return true;
			else
				return false;
		}
		return false;
	}
}

 

 

StudentTest.java

package test;

import java.util.HashSet;

public class StudentTest {
	public static void main(String[] args) {
		HashSet<Student> set = new HashSet<Student>();

		set.add(new Student("100", "ํ™๊ธธ๋™"));
		set.add(new Student("200", "๊ฐ•๊ฐ์ฐฌ"));
		set.add(new Student("300", "์ด์ˆœ์‹ "));
		set.add(new Student("400", "์ •์•ฝ์šฉ"));
		set.add(new Student("100", "์†ก์ค‘๊ธฐ"));

		System.out.println(set);
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

 

 

์—ฐ์Šต๋ฌธ์ œ Q6) ๋‹ค์Œ ์ฝ”๋“œ์—์„œ CarTest์˜ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๊ฐ€ true, true, false๊ฐ€ ๋˜๋„๋ก HashMap์„ ์‚ฌ์šฉํ•˜์—ฌ CarFactory ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๋ผ.

โ€‹

์•„๋ž˜์˜ CarTest ํด๋ž˜์Šค๋ฅผ ํ† ๋Œ€๋กœ ์ž‘์„ฑ..

package test;

public class CarTest {
	public static void main(String[] args) {
		CarFactory factory = CarFactory.getInstance();
		Car sonata1 = factory.createCar("์—ฐ์ˆ˜ ์ฐจ");
		Car sonata2 = factory.createCar("์—ฐ์ˆ˜ ์ฐจ");
		System.out.println(sonata1 == sonata2); // true

		Car avante1 = factory.createCar("์Šน์—ฐ ์ฐจ");
		Car avante2 = factory.createCar("์Šน์—ฐ ์ฐจ");
		System.out.println(avante1 == avante2); // true

		System.out.println(sonata1 == avante1); // false
	}
}

 

 

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

package test;

import java.util.HashMap;

public class CarFactory {
	private HashMap<String, Car> hashMap = new HashMap<>();
	private static CarFactory instance = new CarFactory();    // ์‹ฑ๊ธ€ํ†ค

    public CarFactory() {
        hashMap = new HashMap<String, Car>();
    }

	public Car createCar(String name) {
		if (hashMap.containsKey(key))        // hashMap์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ํ‚ค ๊ฐ’์ด ์žˆ๋‹ค๋ฉด
			return hashMap.get(name);        // ์ด๋ฆ„ ๊ฐ€์ ธ์˜ค๊ธฐ
	    }
        return false;
    }

	public static CarFactory getInstance() {
		return instance;
	}
}

-์–ด๋ ต๋‹ค.. ์„ผ์„ธ๋„ ์•„์ง ์šฐ๋ฆฌ๋ ˆ๋ฒจ์—์„  ์–ด๋ ค์šด ๋ฌธ์ œ๋ผ๊ณ ํ•˜์…จ์Œ....

โ€‹

๋”๋ณด๊ธฐ

<์„ ์ƒ๋‹˜์ด ํ•œ ๊ฒƒ>

package test;

import java.util.HashMap;

public class CarFactory {
	private HashMap<String, Car> hashMap = new HashMap<String, Car>();
	private static CarFactory carFactory;    // ์‹ฑ๊ธ€ํ†ค

	public Car createCar(String key) {
		Car car = null;
		if (!hashMap.containsKey(key))       // hashMap์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ํ‚ค ๊ฐ’์ด ์žˆ๋‹ค๋ฉด
			hashMap.put(key, new Car(key));  // ํ‚ค ๊ฐ’ ์ง‘์–ด๋„ฃ๊ธฐ
		car = hashMap.get(key);              // ํ‚ค ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
		return car;
	}

	public static CarFactory getInstance() { // ์‹ฑ๊ธ€ํ†ค
		if (carFactory == null)              // carFactory๊ฐ€ null๊ฐ’์ด๋ฉด
			carFactory = new CarFactory();   // ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ?
		return carFactory;
	}
}

-์ ‘๊ทผ์ œํ•œ์ž์ธ private์€ ๊ฐ์ฒด ์™ธ๋ถ€์—์„œ์˜ ์ ‘๊ทผ์„ ๋ง‰๋Š”๋‹ค.

์ด๋ ‡๊ฒŒ ๊ฐ์ฒด ์ƒ์„ฑ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ๋Š” ๋Œ€๊ฒŒ ์ธ์Šคํ„ด์Šคํ™”๊ฐ€ ๋ถˆํ•„์š”ํ•  ๊ฒฝ์šฐ์ด๋‹ค.

ํ•˜์ง€๋งŒ ์ด์™€ ๊ฐ™์ด ์ธ์Šคํ„ด์Šคํ™” ์—†์ด ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋‚˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ํ•ด๋‹น ๋ณ€์ˆ˜ ๋˜๋Š” ํ•จ์ˆ˜๊ฐ€ static ์˜์—ญ์— ์žˆ์–ด์•ผํ•œ๋‹ค.

(static ๋ณ€์ˆ˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ํ•˜๋‚˜๋งŒ ํ• ๋‹น๋˜๋ฏ€๋กœ ์ธ์Šคํ„ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ƒ์„ฑ๋˜์–ด๋„ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋ฅผ ๊ณต์œ ํ•œ๋‹ค.)

์ด๋Ÿฌํ•œ private์™€ static์˜ ํŠน์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ singleton ํŒจํ„ด๋ฅผ ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€