TreeSet
TreeSet
-
๊ฐ์ฒด ์ ๋ ฌ์ ์ฌ์ฉ
-
Set ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ฌ ์ค๋ณต์ ํ์ฉํ์ง ์๊ณ , ์ค๋ฆ์ฐจ์ / ๋ด๋ฆผ์ฐจ์์ผ๋ก ๊ฐ์ฒด๋ฅผ ์ ๋ ฌํ ์ ์์
-
๋ด๋ถ์ ์ผ๋ก ์ด์ง๊ฒ์ํธ๋ฆฌ(binary search tree)๋ก ๊ตฌํ๋๋ฉฐ, ์ด์ง๊ฒ์ํธ๋ฆฌ์ ์ ์ฅํ๊ธฐ ์ํด ๊ฐ ๊ฐ์ฒด๋ฅผ ๋น๊ตํด์ผ ํ๋๋ฐ, ๋น๊ต ๋์์ด ๋๋ ๊ฐ์ฒด์ Comparable ์ด๋ Comparator ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํ ํด์ผ TreeSet์ ์ถ๊ฐ๋ ์ ์์
-
Comparable์ java.lang ํจํค์ง, Comoparator์ java.util ํจํค์ง
-
๋๋ถ๋ถ Comparable ์ฌ์ฉ
-
ex)
-
Member.java
public class Member { private int memberId; private String memberName; public Member(int memberId, String memberName) { this.memberId = memberId; this.memberName = memberName; } public int getMemberId() { return memberId; } public void setMemberId(int memberId) { this.memberId = memberId; } public String getMemberName() { return memberName; } public void setMemberName(String memberName) { this.memberName = memberName; } @Override public String toString() { return memberName + " ํ์๋์ ์์ด๋๋ " + memberId + "์ ๋๋ค."; } @Override public int hashCode() { return memberId; } @Override public boolean equals(Object obj) { if (obj instanceof Member) { Member member = (Member)obj; if (this.memberId == member.memberId) { return true; } else return false; } return false; } }
-
MemberTreeSet.java
import java.util.TreeSet; import java.util.Iterator; public class MemberTreeSet { private TreeSet<Member> treeSet; public MemberTreeSet() { treeSet = new TreeSet<>(); } public void addMember(Member member) { treeSet.add(member); } public boolean removeMember(int memberId) { Iterator<Member> irIterator = treeSet.iterator(); while (irIterator.hasNext()) { Member member = (Member) irIterator.next(); int tempId = member.getMemberId(); if (tempId == memberId) { treeSet.remove(member); return true; } } System.out.println("ํด๋น ์์ด๋๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค."); return false; } public void showAllMember() { for (Member member : treeSet) { System.out.println(member); } } }
-
MemberTreeSetTest.java
import java.util.TreeSet; public class MemberTreeSetTest { public static void main(String[] args) { TreeSet<String> treeSet = new TreeSet<String>(); treeSet.add("์ด๋ฐค"); treeSet.add("๊ฐ์๋ฒฝ"); treeSet.add("์ต์์นจ"); System.out.println(treeSet); } }
-
์ถ๋ ฅ๊ฒฐ๊ณผ
[๊ฐ์๋ฒฝ, ์ด๋ฐค, ์ต์์นจ]
- String ํด๋์ค๋ ์ด๋ฏธ Comoparable ์ธํฐํ์ด์ค๊ฐ ๊ตฌํ๋์ด ์์ -> ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ ๋ผ ์ถ๋ ฅ๋จ
-
Comparable ์ธํฐํ์ด์ค ๊ตฌํํ๊ธฐ
-
์์ด๋ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ธฐ
-
MemberTreeSet.java : ์์ ๋์ผ
-
Member.java
public class Member implements Comparable<Member>{ private int memberId; private String memberName; public Member(int memberId, String memberName) { this.memberId = memberId; this.memberName = memberName; } public int getMemberId() { return memberId; } public void setMemberId(int memberId) { this.memberId = memberId; } public String getMemberName() { return memberName; } public void setMemberName(String memberName) { this.memberName = memberName; } @Override public String toString() { return memberName + " ํ์๋์ ์์ด๋๋ " + memberId + "์ ๋๋ค."; } @Override public int hashCode() { return memberId; } @Override public boolean equals(Object obj) { if (obj instanceof Member) { Member member = (Member)obj; if (this.memberId == member.memberId) { return true; } else return false; } return false; } // ์ธํฐํ์ด์ค implements -> ์ถ์๋ฉ์๋ ๊ตฌํํด์ผํจ // ์ฝ๋ฐฑํจ์ @Override public int compareTo(Member member) { // ๋ฐํ๊ฐ์ int -> // ๋(this)์ ์๋ก ๋ค์ด์จ argument๋ฅผ ๋น๊ตํด์ // ๋ด๊ฒ ๋ํฌ๋ฉด ์์, ์์ผ๋ฉด ์์, ๊ฐ์ผ๋ฉด 0 ๋ฐํ // -> ๊ทธ ๋ฐํ๊ฐ์ผ๋ก ๋ค๋ฅธ๊ฑฐ๋ ๋น๊ตํ๊ณ ๋ ๋น๊ตํ๊ณ ... if (this.memberId > member.memberId) { return 1; } else if (this.memberId < member.memberId) { return -1; } else return 0; // ๋ฐ ํ๋ฉด ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด ์ถ๋ ฅ๋จ // ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ค๋ฉด? 1๊ณผ -1 ๋ฐ๊พธ๋ฉด ๋จ -> ํธ๋ฆฌ ๋ค์ง์ด์ง } }
-
MemberTreeSetTest.java
public class MemberTreeSetTest { public static void main(String[] args) { MemberTreeSet memberTreeSet = new MemberTreeSet(); Member member1 = new Member(104, "์ด๋ฐค"); Member member2 = new Member(102, "๊ฐ์๋ฒฝ"); Member member3 = new Member(103, "์ต์์นจ"); Member member4 = new Member(101, "์ค์ ๋ "); memberTreeSet.addMember(member1); memberTreeSet.addMember(member2); memberTreeSet.addMember(member3); memberTreeSet.addMember(member4); memberTreeSet.showAllMember(); } }
-
์ถ๋ ฅ๊ฒฐ๊ณผ
์ค์ ๋ ํ์๋์ ์์ด๋๋ 101์ ๋๋ค. ๊ฐ์๋ฒฝ ํ์๋์ ์์ด๋๋ 102์ ๋๋ค. ์ต์์นจ ํ์๋์ ์์ด๋๋ 103์ ๋๋ค. ์ด๋ฐค ํ์๋์ ์์ด๋๋ 104์ ๋๋ค.
Comparator ์ธํฐํ์ด์ค ๊ตฌํํ๊ธฐ
-
Member.java
import java.util.Comparator; public class Member implements Comparator<Member>{ private int memberId; private String memberName; public Member(int memberId, String memberName) { this.memberId = memberId; this.memberName = memberName; } public Member() {} // ๋ํดํธ์์ฑ์ ๋ง๋ค์ด์ผ ํจ public int getMemberId() { return memberId; } public void setMemberId(int memberId) { this.memberId = memberId; } public String getMemberName() { return memberName; } public void setMemberName(String memberName) { this.memberName = memberName; } @Override public String toString() { return memberName + " ํ์๋์ ์์ด๋๋ " + memberId + "์ ๋๋ค."; } @Override public int hashCode() { return memberId; } @Override public boolean equals(Object obj) { if (obj instanceof Member) { Member member = (Member)obj; if (this.memberId == member.memberId) { return true; } else return false; } return false; } @Override public int compare(Member member1, Member member2) { // comparable๊ณผ ๋ค๋ฅด๊ฒ ๋งค๊ฐ๋ณ์ ๋ ๊ฐ // ํ๋๋ ๋, ํ๋๋ ๋น๊ต๋์ if (member1.memberId > member2.memberId) { return 1; } else if (member1.memberId < member2.memberId){ return -1; } else return 0; } }
-
MemberTreeSet.java
import java.util.TreeSet; import java.util.Iterator; public class MemberTreeSet { private TreeSet<Member> treeSet; public MemberTreeSet() { treeSet = new TreeSet<>(new Member()); // comparable์ธ ๊ฒฝ์ฐ์ ์๊ด ์์ง๋ง comparator๋ก ๊ตฌํํ ๋๋ // TreeSet ์์ฑ์ ์์ comparator๊ฐ ๊ตฌํ๋ ๊ฒ์ ์ง์ ํด์ค์ผ ํจ!! } public void addMember(Member member) { treeSet.add(member); } public boolean removeMember(int memberId) { Iterator<Member> irIterator = treeSet.iterator(); while (irIterator.hasNext()) { Member member = (Member) irIterator.next(); int tempId = member.getMemberId(); if (tempId == memberId) { treeSet.remove(member); return true; } } System.out.println("ํด๋น ์์ด๋๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค."); return false; } public void showAllMember() { for (Member member : treeSet) { System.out.println(member); } } }
-
MemberTreeSetTest.java : ์์ ๋์ผ
-
์ถ๋ ฅ๊ฒฐ๊ณผ
์ค์ ๋ ํ์๋์ ์์ด๋๋ 101์ ๋๋ค. ๊ฐ์๋ฒฝ ํ์๋์ ์์ด๋๋ 102์ ๋๋ค. ์ต์์นจ ํ์๋์ ์์ด๋๋ 103์ ๋๋ค. ์ด๋ฐค ํ์๋์ ์์ด๋๋ 104์ ๋๋ค.
์ฝ๋ ๊ฐ๋จํ๊ฒ ์ฐ๊ธฐ
@Override
public int compareTo(Member member) {
if (this.memberId > member.memberId) {
return 1;
} else if (this.memberId < member.memberId) {
return -1;
} else return 0;
// ๋ฐ ํ๋ฉด ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด ์ถ๋ ฅ๋จ
// ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ค๋ฉด? 1๊ณผ -1 ๋ฐ๊พธ๋ฉด ๋จ -> ํธ๋ฆฌ ๋ค์ง์ด์ง
}
๋ฐํ๊ฐ์ int๋ก, this.memberId์ member.memberId๋ฅผ ๋น๊ต ํด this๊ฐ ๋ ํฌ๋ฉด ์์(1), ์์ผ๋ฉด ์์(-1), ๊ฐ์ผ๋ฉด 0์ ๋ฐํํ๋ค.
@Override
public int compareTo(Member member) {
return (this.memberId - member.memberId); // ์ค๋ฆ์ฐจ์
return (this.memberId - member.memberId) * (-1); // ๋ด๋ฆผ ์ฐจ์
}