HashMap


  • Map μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„

  • key - valueλ₯Ό 쌍으둜 κ΄€λ¦¬ν•˜λŠ” λ©”μ„œλ“œ κ΅¬ν˜„

  • 검색을 μœ„ν•œ 자료ꡬ쑰

  • keyλ₯Ό μ΄μš©ν•˜μ—¬ 값을 μ €μž₯ν•˜κ³  κ·Έ keyλ₯Ό μ΄μš©ν•˜μ—¬ 값을 κΊΌλ‚΄μ˜€λŠ” 방식

  • hash μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ κ΅¬ν˜„λ¨

  • keyκ°€ λ˜λŠ” κ°μ²΄λŠ” 쀑볡될 수 μ—†μŒ > 객체의 μœ μΌμ„± 비ꡐλ₯Ό μœ„ν•œ equals()λ©”μ„œλ“œμ™€ hashCode()λ©”μ„œλ“œ κ΅¬ν˜„ν•΄μ•Ό 함

  • 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;
            }
      
        }
      
    • MemberHashMap.java

        import java.util.HashMap;
        import java.util.Iterator;
      
        public class MemberHashMap {
      
            private HashMap<Integer, Member> hashMap;
            //	key = Integer, value = Member
                  
            public MemberHashMap() {
                hashMap = new HashMap<Integer, Member>();
            }
                  
            public void addMember (Member member) {
                      
                hashMap.put(member.getMemberId(), member);
                // add X / put O
            }
                  
            public boolean removeMember (int memberId) {
                      
                if (hashMap.containsKey(memberId)) {
                    hashMap.remove(memberId);	
                } 
                      
                System.out.println("ν•΄λ‹Ή 아이디가 μ—†μŠ΅λ‹ˆλ‹€.");
                return false;
            }
                  
            public void showAllMember() {
                      
                Iterator<Integer> ir = hashMap.keySet().iterator();
                      
                while (ir.hasNext()) {
                          
                    int key = ir.next();
                    Member member = hashMap.get(key);
                    System.out.println(member);
                          
                }
                      
            }
        }
      
    • (Main Class) MemberHashMapTest.java

        public class MemberHashMapTest {
      
            public static void main(String[] args) {
                      
                MemberHashMap memberHashMap = new MemberHashMap();
                      
                Member member1 = new Member(101, "이밀");
                Member member2 = new Member(102, "κ°•μƒˆλ²½");
                Member member3 = new Member(103, "μ΅œμ•„μΉ¨");
                      
                memberHashMap.addMember(member1);
                memberHashMap.addMember(member2);
                memberHashMap.addMember(member3);
      
                memberHashMap.showAllMember();
            }
      
        }
      
    • 좜λ ₯κ²°κ³Ό

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

Categories:

Java