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

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

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

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

โ€‹

*HashSet ํด๋ž˜์Šค

HashSet ํด๋ž˜์Šค๋Š” ์ง‘ํ•ฉ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉฐ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

โ€‹

โ€‹

HashSetTest.java

package collection.hashset;

import java.util.HashSet;

public class HashSetTest {
	public static void main(String[] args) {
		HashSet<String> hashSet = new HashSet<String>();
		hashSet.add(new String("์ž„์ •์ˆœ"));
		hashSet.add(new String("๋ฐ•ํ˜„์ •"));
		hashSet.add(new String("ํ™์—ฐ์˜"));
		hashSet.add(new String("๊ฐ•๊ฐ์ฐฌ"));
		hashSet.add(new String("๊ฐ•๊ฐ์ฐฌ"));

		System.out.println(hashSet);
	}
}

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

-๊ฐ•๊ฐ์ฐฌ์ด 1๋ช…๋งŒ ์ถœ๋ ฅ๋จ.

 

 

 

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

โ€‹

MemberHashSet.java

package collection.hashset;

import java.util.HashSet;
import java.util.Iterator;

import collection.Member;

public class MemberHashSet {
	private HashSet<Member> hashSet;

	public MemberHashSet() {
		hashSet = new HashSet<Member>();
	}

	public void addMember(Member member) {  // HashSet์— ํšŒ์› ์ถ”๊ฐ€
		hashSet.add(member);
	}

	public boolean removeMember(int memberID) {  // ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ํšŒ์› ์•„์ด๋””์— ํ•ด๋‹นํ•˜๋Š” ํšŒ์› ์‚ญ์ œ 
		Iterator<Member> ir = hashSet.iterator();

		while (ir.hasNext()) {
			Member member = ir.next();           // ํšŒ์›์„ ํ•˜๋‚˜์”ฉ ๊ฐ€์ ธ์™€์„œ
			int tempId = member.getMemberID();   // ์•„์ด๋”” ๋น„๊ต
			if (tempId == memberID) {            // ๊ฐ™์€ ์•„์ด๋””์ธ ๊ฒฝ์šฐ
				hashSet.remove(member);          // ํšŒ์› ์‚ญ์ œ
				return true;
			}
		}
		System.out.println(memberID + "๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		return false;
	}

	public void showAllMember() {
		for (Member member : hashSet) {
			System.out.println(member);
		}
		System.out.println();
	}
}

-ํšŒ์›์„ ์‚ญ์ œํ• ๋•Œ ์“ฐ๋Š” remove( ) ๋ฉ”์„œ๋“œ๊ฐ€ ArrayList์™€๋Š” ์ข€ ๋‹ค๋ฅด๋‹ค. ArrayList์—์„œ๋Š” get(i) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด i๋ฒˆ์งธ์— ํ•ด๋‹นํ•˜๋Š” ํ•ญ๋ชฉ์„ ์‚ญ์ œํ–ˆ์ง€๋งŒ HashSet์—์„œ๋Š” ํ•ด๋‹นํ•˜๋Š” ์•„์ด๋””๋ฅผ ๊ฐ€์ง„ ํšŒ์›์„ ์ฐพ๊ธฐ ์œ„ํ•ด Iterator๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

โ€‹

โ€‹

MemberHashSetTest.java

package collection.hashset;

import collection.Member;

public class MemberHashSetTest {
	public static void main(String[] args) {
		MemberHashSet memberHashSet = new MemberHashSet();

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

		memberHashSet.addMember(memberLee);
		memberHashSet.addMember(memberSon);
		memberHashSet.addMember(memberPart);
		memberHashSet.showAllMember();

		Member memberHong = new Member(1003, "ํ™๊ธธ๋™");  // ์•„์ด๋”” ์ค‘๋ณต ํšŒ์› ์ถ”๊ฐ€
		memberHashSet.addMember(memberHong);
		memberHashSet.showAllMember();
	}
}

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

-๊ฐ™์€ ์•„์ด๋”” 1003์„ ๊ฐ€์ง„ ๋ฐ•์„œํ›ค๊ณผ ํ™๊ธธ๋™์ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค

-๊ฐ์ฒด๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์„ ๊ตฌํ˜„ํ•ด์ค˜์•ผํ•จ

โ€‹

โ€‹

โ€‹

*๊ฐ์ฒด๊ฐ€ ๋™์ผํ•จ์„ ๊ตฌํ˜„ํ•˜๊ธฐ

๊ธฐ๋ณธ์ ์œผ๋กœ ์ธ์Šคํ„ด์Šค ์ฃผ์†Œ๊ฐ€ ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ๊ฐ์ฒด์ด๋‹ค.

Member ํด๋ž˜์Šค์— equals( ) ๋ฉ”์„œ๋“œ์™€ hashCode( ) ๋ฉ”์„œ๋“œ๋ฅผ ์ œ์ •์˜ํ•˜์—ฌ ํšŒ์› ์•„์ด๋””๊ฐ€ ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ํšŒ์›์ž„์„ ๊ตฌํ˜„ํ•ด ์ฃผ์ž.

package collection;

public class Member {
	private int memberID;
	private String memberName;

    ...

	@Override
	public int hashCode() {
		return memberID;  // HashCode() ๋ฉ”์„œ๋“œ๊ฐ€ ํšŒ์› ์•„์ด๋””๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์žฌ์ •์˜
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Member) {
			Member member = (Member) obj;
			if (this.memberID == member.memberID)  // ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ํšŒ์› ์•„์ด๋””๊ฐ€ ์ž์‹ ์˜ ํšŒ์› ์•„์ด๋””์™€ ๊ฐ™๋‹ค๋ฉด true ๋ฐ˜ํ™˜
				return true;
			else
				return false;
		}
		return false;
	}
}

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

โ€‹

โ€‹โ€‹

โ€‹

*TreeSet ํด๋ž˜์Šค

TreeSet์€ ์ž๋ฃŒ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ •๋ ฌํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.

โ€‹

TreeSetTest.java

package collection.treeset;

import java.util.TreeSet;

public class TreeSetTest {
	public static void main(String[] args) {
		TreeSet<String> treeSet = new TreeSet<String>();
		treeSet.add("ํ™๊ธธ๋™");
		treeSet.add("๊ฐ•๊ฐ์ฐฌ");
		treeSet.add("์ด์ˆœ์‹ ");

		for (String str : treeSet) {
			System.out.println(str);
		}
	}
}

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

โ€‹

 

 

*์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ

ํŠธ๋ฆฌ๋Š” ์ž๋ฃŒ ์‚ฌ์ด์˜ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ.

ํŠธ๋ฆฌ ์ž๋ฃŒ ๊ตฌ์กฐ์—์„œ ๊ฐ ์ž๋ฃŒ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ณต๊ฐ„์„ '๋…ธ๋“œ'๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„์•„๋ž˜๋กœ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์˜ ๊ด€๊ณ„๋ฅผ '๋ถ€๋ชจ-์ž์‹ ๋…ธ๋“œ'๋ผ๊ณ  ํ•œ๋‹ค. ์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ๋Š” ๋…ธ๋“œ์— ์ €์žฅ๋˜๋Š” ์ž๋ฃŒ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ , ๋ถ€๋ชจ๊ฐ€ ๊ฐ€์ง€๋Š” ์ž์‹ ๋…ธ๋“œ์˜ ์ˆ˜๊ฐ€ 2๊ฐœ ์ดํ•˜์ด๋‹ค. ๋˜ํ•œ ์™ผ์ชฝ์— ์œ„์น˜ํ•˜๋Š” ์ž์‹ ๋…ธ๋“œ๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ๋ณด๋‹ค ํ•ญ์ƒ ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. ๋ฐ˜๋Œ€๋กœ ์˜ค๋ฅธ์ชฝ์— ๋†“์ธ ์ž์‹ ๋…ธ๋“œ๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ๋ณด๋‹ค ํ•ญ์ƒ ํฐ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. ๋”ฐ๋ผ์„œ ์–ด๋–ค ํŠน์ • ๊ฐ’์„ ์ฐพ์œผ๋ ค ํ•  ๋•Œ ํ•œ ์ชฝ ๋…ธ๋“œ์™€ ๋น„๊ตํ•ด ๋น„๊ตํ•œ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ด๋ฉด ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ๋ฐฉํ–ฅ์œผ๋กœ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ๋น„๊ต ๋ฒ”์œ„๊ฐ€ ํ‰๊ท  1/2๋งŒํผ์”ฉ ์ค„์–ด๋“ค์–ด ํšจ๊ณผ์ ์œผ๋กœ ์ž๋ฃŒ๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

โ€‹

โ€‹

โ€‹

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

โ€‹

MemberTreeSet.java

package collection.treeset;

import java.util.Iterator;
import java.util.TreeSet;

import collection.Member;

public class MemberTreeSet {
	private TreeSet<Member> treeSet;

	public MemberTreeSet() {
		treeSet = new TreeSet<Member>();
	}

	public void addMember(Member member) {
		treeSet.add(member);
	}

	public boolean removeMember(int memberID) {
		Iterator<Member> ir = treeSet.iterator();

		while (ir.hasNext()) {
			Member member = ir.next();
			int tempId = member.getMemberID();
			if (tempId == memberID) {
				treeSet.remove(member);
				return true;
			}
		}
		System.out.println(memberID + "๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		return false;
	}

	public void showAllMember() {
		for (Member member : treeSet) {
			System.out.println(member);
		}
		System.out.println();
	}
}

 

 

MemberTreeSetTest.java

package collection.treeset;

import collection.Member;

public class MemberTreeSetTest {
	public static void main(String[] args) {
		MemberTreeSet memberTreeSet = new MemberTreeSet();

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

		memberTreeSet.addMember(memberLee);
		memberTreeSet.addMember(memberSon);
		memberTreeSet.addMember(memberPark);
		memberTreeSet.showAllMember();

		Member memberHong = new Member(1003, "ํ™๊ธธ๋™");  // ์•„์ด๋”” ์ค‘๋ณต ํšŒ์› ์ถ”๊ฐ€
		memberTreeSet.addMember(memberHong);
		memberTreeSet.showAllMember();
	}
}

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

-์œ„ ์˜ค๋ฅ˜ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด๋ฉด Member ํด๋ž˜์Šค๊ฐ€ Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๋‹ค๊ณ  ๋‚˜์˜จ๋‹ค.

Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ์˜๋ฏธ๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  Member ํด๋ž˜์Šค๋ฅผ TreeSet์˜ ์š”์†Œ๋กœ ์ถ”๊ฐ€ํ•  ๋•Œ ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ ๋…ธ๋“œ๋ฅผ ๋น„๊ตํ•˜์—ฌ ํŠธ๋ฆฌ๋ฅผ ํ˜•์„ฑํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ๋œป์ด๋‹ค.

๋”ฐ๋ผ์„œ ํšŒ์›์„ TreeSet์— ์ถ”๊ฐ€ํ•  ๋•Œ ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ ๋น„๊ตํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๊ตฌํ˜„ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€