package demo.set;

import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo1 {

	public static void main(String[] args) {
		HashSet<String> names = new HashSet<String>();
		
		names.add("홍길동");
		names.add("김유신");
		names.add("강감찬");
		names.add("홍길동");
		names.add("이순신");
		names.add("유관순");
		
		Iterator<String> it = names.iterator();
		while (it.hasNext()) {
			String name = it.next();
			System.out.println(name);
		}
		System.out.println();
		names.clear();
		
		System.out.println("비어 있는가? " + names.isEmpty());
		System.out.println("저장된 개수 : " + names.size());
		
		for (String name : names) {
			System.out.println(name);
			
		}
	}
}
package demo.set;

import java.util.HashSet;

public class HashSetDemo2 {

	public static void main(String[] args) {
		
		// Contact에서 hashCode()와 equals() 메소드를 재정의해서
		// 객체가 달라도 번호가 동일하면 동일한 객체로 간주하도록 만들었다.
		
		Contact contact1 = new Contact(10, "홍길동", "010-1111-1111");
		Contact contact2 = new Contact(20, "김유신", "010-2222-1111");
		Contact contact3 = new Contact(10, "홍길동", "010-1111-1111");
		Contact contact4 = new Contact(40, "이순신", "010-4444-1111");
		
		HashSet<Contact> contacts = new HashSet<Contact>();
		contacts.add(contact1);
		contacts.add(contact2);
		contacts.add(contact3);
		contacts.add(contact4);
		contacts.add(contact1);
		contacts.add(contact2);

		System.out.println(contacts);
	}
}
package demo.set;

import java.util.TreeSet;

public class TreeSetDemo {

	public static void main(String[] args) {
		TreeSet<String> names = new TreeSet<String>();
		
		names.add("이순신");
		names.add("김유신");
		names.add("강감찬");
		names.add("홍길동");
		names.add("유관순");
		names.add("김구");
		
		for (String name : names) {
			System.out.println(name);
		}
	}
}
package demo.set;

import java.util.Random;
import java.util.TreeSet;

public class TreeSetDemo2 {

	public static void main(String[] args) {
		TreeSet<Integer> lotto = new TreeSet<Integer>();
		Random random = new Random();
		
		while (true) {
			int number = random.nextInt(45) + 1;
			lotto.add(number);
			
			if (lotto.size() == 6) {
				break;
			}
		}
		System.out.println(lotto);
	}
}
package demo.set;

public class Contact {

	private int no;
	private String name;
	private String tel;
	
	public Contact() {}

	public Contact(int no, String name, String tel) {
		super();
		this.no = no;
		this.name = name;
		this.tel = tel;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	@Override
	public String toString() {
		return "Contact [no=" + no + ", name=" + name + ", tel=" + tel + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + no;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Contact other = (Contact) obj;
		if (no != other.no)
			return false;
		return true;
	}
	
}

'자바 > collection' 카테고리의 다른 글

properties, queue, stack  (0) 2019.06.10
sort  (0) 2019.06.10
map  (0) 2019.06.10
list  (0) 2019.06.10

+ Recent posts