HashSet


  • HashSet ν΄λž˜μŠ€λŠ” Set μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ > μš”μ†Œλ₯Ό μˆœμ„œμ— 상관없이 μ €μž₯, μ€‘λ³΅λœ 값은 μ €μž₯ν•˜μ§€ μ•ŠμŒ

  • HashSetμ—μ„œ add() λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 쀑볡 없이 μƒˆλ‘œμš΄ μš”μ†Œλ₯Ό μΆ”κ°€ν•˜κΈ° μœ„ν•΄μ„œλŠ” hashCode()와 equals() λ©”μ†Œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”©ν•΄μ•Ό 함

  • 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 + "μž…λ‹ˆλ‹€.";
            }
      
            //	λ§€κ°œλ³€μˆ˜λ‘œ λ„˜μ–΄μ˜¨ 것이 λ‚˜μ™€ 같은지λ₯Ό ν™•μΈν•΄μ£ΌλŠ” equals와 hashcode κ΅¬ν˜„
            @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;
            }   
      
    • MemberHashSet.java

        import java.util.HashSet;
      
        public class MemberHashSet {
      
            private HashSet<Member> hashSet;
                  
            public MemberHashSet() {
                hashSet = new HashSet<>();
            }
      
            public MemberHashSet(int size) {
                hashSet = new HashSet<>(size);
            }
                  
            public void addMember(Member member) {
                hashSet.add(member);
            }
      
            public boolean removeMember(int memberId) {
      
                for (int i = 0; i < arrayList.size(); i++) {
                          
                    Member member = arrayList.get(i);
                    int tempId = member.getMemberId();
                          
                    if (tempId == memberId) {
                        arrayList.remove(i);
                        return true;
                    } 
                }
                      
                System.out.println("ν•΄λ‹Ή 아이디가 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.");
                return false;
            }
                  
            public void showAllMember() {
                for (Member member : hashSet) {
                    System.out.println(member);
                }
            }
        }
      
    • MemberHashSetMain.java

        public class MemberHashSetMain {
      
            public static void main(String[] args) {
      
                MemberHashSet memberHashSet = new MemberHashSet();
                      
                Member member1 = new Member(101, "이밀");
                Member member2 = new Member(102, "κ°•μƒˆλ²½");
                Member member3 = new Member(103, "μ΅œμ•„μΉ¨");
                      
                memberHashSet.addMember(member1);
                memberHashSet.addMember(member2);
                memberHashSet.addMember(member3);
                      
                Member member4 = new Member(103, "μœ€μ €λ…");
                //	hashCode() / equals() λ©”μ„œλ“œ μ‚¬μš© ν•΄ 쀑볡 μ—¬λΆ€λ₯Ό μ²΄ν¬ν•΄μ€˜μ•Ό 함
                memberHashSet.addMember(member4);
                      
                memberHashSet.showAllMember();
      
            }
      
        }
      
    • 좜λ ₯κ²°κ³Ό

        이밀 νšŒμ›λ‹˜μ˜ μ•„μ΄λ””λŠ” 101μž…λ‹ˆλ‹€.
        κ°•μƒˆλ²½ νšŒμ›λ‹˜μ˜ μ•„μ΄λ””λŠ” 102μž…λ‹ˆλ‹€.
        μ΅œμ•„μΉ¨ νšŒμ›λ‹˜μ˜ μ•„μ΄λ””λŠ” 103μž…λ‹ˆλ‹€.
      

Categories:

Java