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);       // ๋‚ด๋ฆผ ์ฐจ์ˆœ

	}

Categories:

Java