티스토리 뷰

반응형

53. Set 인터페이스 - 1 54. Set 인터페이스 - 2

package collection.hashmap;

public class MemberHashMapTest {

	public static void main(String[] args) {

		MemberHashMap manager = new MemberHashMap();
		
		Member memberLee = new Member(100, "Lee");
		Member memberKim = new Member(200, "Kim");
		Member memberPark = new Member(300, "Park");
		Member memberPark2 = new Member(300, "Park");
				
		manager.addMember(memberLee);
		manager.addMember(memberKim);
		manager.addMember(memberPark);
		manager.addMember(memberPark2);
				
		manager.showAllMember();
		
		manager.removeMember(200);
		manager.showAllMember();
		
	}

}
package collection.hashmap;

public class Member {

	private int memberId;
	private String memberName;
	
	public Member() {}
	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;
	}
	
	public String toString() {
		return memberName + "회원님의 아이디는 " + memberId + "입니다.";
	}

	
}
package collection.hashmap;

import java.util.HashMap;
import java.util.Iterator;

public class MemberHashMap {
	
	private HashMap<Integer, Member> hashMap;
	
	public MemberHashMap() {
		hashMap = new HashMap<Integer, Member>();
	}
	
	public void addMember(Member member) {
		hashMap.put(member.getMemberId(), member);
	}
	
	public boolean removeMember(int memberId) {
		if ( hashMap.containsKey(memberId)) {
			hashMap.remove(memberId);
			return true;
		}
		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);
		}

		System.out.println();
		
	}
	

}

|  Set 인터페이스

collection하위의 인터페이스

중복을 허용하지 않음

list 는 순서기반의 인터페이스이지만, set 는 순서가 없음

get(i) 메서드가 제공되지 않음 (iterator로 순회)

저장된 순서와 출력 순서는 다를 수 있음

아이디, 주민번호 , 사번 등 유일한 값이나 객체를 관리할 때 사용

hashset, treeset 클래스

|TreeSet 클래스

객테의 정렬에 사용되는 클래스

중복을 허용하지 않으면서 오름차순이나 내림차순으로 객체를 정렬함

내부적으로 이진 검색트리로 구현되어있음

이진검색 트리에 자료가 저장될때 비교해 저장될 위치를 정함

객체 비교를 위해 comparable 이나 comparator 인터페이스를 구현해야함.

 

package collection.treeset;

import java.util.Comparator;
import java.util.TreeSet;

class MyCompare implements Comparator<String>{

	@Override
	public int compare(String s1, String s2) {

		return s1.compareTo(s2) *(-1);
	}
	
}

public class ComparatorTest {

	public static void main(String[] args) {
		TreeSet<String> treeSet = new TreeSet<String>(new MyCompare());
		treeSet.add("È«±æµ¿");
		treeSet.add("°­°¨Âù");
		treeSet.add("À̼ø½Å");
		
		for( String str : treeSet) {
			System.out.println(str);
		}
	}

}
package collection.treeset;

import java.util.Comparator;

public class Member implements Comparator<Member>{

	private int memberId;
	private String memberName;
	
	public Member() {}
	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;
	}
	
	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;
			return (this.memberId == member.memberId); 
		}
		
		return false;
	}
	
	/*@Override
	public int compareTo(Member member) {
		
		return this.memberName.compareTo(member.getMemberName());
	}*/
	
	@Override
	public int compare(Member member1, Member member2) {

		return (member1.memberId - member2.memberId);
	}
	
	

	}
반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함