ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” ์ปฌ๋ ‰์…˜ Set๊ณ„์—ด HashSet
    JAVA 2020. 8. 12. 23:43

    ๐ŸŽฏ ์ž๋ฐ” ์ปฌ๋ ‰์…˜์˜ ์ข…๋ฅ˜๋ฅผ ์•Œ์•„๋ณด๊ณ  HashSet์„ ์—ฐ์Šตํ•ด๋ณธ๋‹ค.

     

     

     

     

     

    ์ปฌ๋ ‰์…˜ (Collection)

    ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋ณ€์ˆ˜, ๋ฐฐ์—ด ๊ทธ๋ฆฌ๊ณ  ์ปฌ๋ ‰์…˜์ด ์žˆ๋‹ค. ์ปฌ๋ ‰์…˜์€ ํฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ๋•Œ ๋ฐฐ์—ด๋ณด๋‹ค ํšจ์œจ์ ์ด๋‹ค.  ํ•˜๋‚˜์˜ ์ปฌ๋ ‰์…˜์— ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•  ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด์ฒ˜๋Ÿผ ๋ฐฉ ํฌ๊ธฐ์™€ ๋ฐ์ดํ„ฐํƒ€์ž…์„ ์ง€์ •ํ•  ํ•„์š” ์—†์ด 100๊ฐœ๊ฑด 200๊ฐœ๊ฑด ๊ทธ๋ƒฅ ๋‹ค ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

     

     

    ์ปฌ๋ ‰์…˜์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋Š” Collection ์ธํ„ฐํŽ˜์ด์Šค๋‹ค. ์ปฌ๋ ‰์…˜์€ Set๊ณผ List ๊ณ„์—ด๋กœ ๋‚˜๋‰˜๋ฉฐ ํ•จ์ˆ˜ ์ •์˜๋งŒ ๋˜์–ด์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ ๊ตฌํ˜„์€ ์ƒ์†๋ฐ›๋Š” ์•„๋ž˜ ํด๋ž˜์Šค๋“ค์ด ํ•œ๋‹ค. Set๊ณ„์—ด์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต ์ €์žฅ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. List ๊ณ„์—ด์€ ๋ฐ์ดํ„ฐ ์ €์žฅ์ˆœ์„œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต ์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด ๋‘ ๊ณ„์—ด์€ ๋ชจ๋‘ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ํ˜•ํƒœ์ด๋ฉฐ, Map ์ธํ„ฐํŽ˜์ด์Šค๋Š” key-value ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ํ˜•ํƒœ์ด๋‹ค. key๋Š” ์ค‘๋ณต์ €์žฅ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ value๋Š” ์ค‘๋ณต์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๊ณ  ์ €์žฅ ์ˆœ์„œ๊ฐ€ ์—†๋‹ค. 

     

     

    ์ปฌ๋ ‰์…˜ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋Œ€ํ‘œ์ ์ธ ์ถ”์ƒ๋ฉ”์†Œ๋“œ์ด๋‹ค.  Set๊ณ„์—ด๊ณผ List๊ณ„์—ด์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ ํ•˜์œ„ ํด๋ž˜์Šค๋“ค์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. size๋Š” ๋ฐฐ์—ด์˜ length์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด๋‹ค. contains() ๋ฉ”์†Œ๋“œ๋Š” ์ปฌ๋ ‰์…˜ ์•ˆ์— ์–ด๋–ค ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. iterator() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ˜๋ณต๋ฌธ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด๋‹ค.

     

     

    HashSet ํด๋ž˜์Šค ์‚ฌ์šฉํ•˜๊ธฐ

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    public class Ex10_6 {
    	public static void main(String[] args) {	
    		Set<String> s = new HashSet<String>();
    		s.add("ํ™๊ธธ๋™");
    		s.add("์ด์ˆœ์‹ ");
    		s.add("์œ ๊ด€์ˆœ");
    		s.add("ํ™๊ธธ๋™");
    		s.add("ํ™๊ธธ๋™");
    		System.out.println("toString() ์ „์ฒด ์ถœ๋ ฅ:" +s );
    		for(String data : s) {
    			System.out.println("forEach ์ „์ฒด ์ถœ๋ ฅ:"+data);
    		}
    		System.out.println("Set์˜ ๋ฐ์ดํ„ฐ ๊ฐฏ์ˆ˜? "+ s.size());
    		System.out.println("Set์ด ๋น„์–ด์žˆ๋ƒ? "+ s.isEmpty());
    		System.out.println("Set์— ์ด์ˆœ์‹  ํฌํ•จ์—ฌ๋ถ€? "+s.contains("์ด์ˆœ์‹ "));
    		System.out.println("์ด์ˆœ์‹  ์‚ญ์ œ ์‹คํ–‰");
    		s.remove("์ด์ˆœ์‹ ");
    		for (String data : s) {
    			System.out.println("forEach: "+data);
    		}
    		Iterator<String> ite = s.iterator();
    		while(ite.hasNext()) {
    			String data = ite.next();
    			System.out.println("Iterator: "+data);
    		}
    		System.out.println("์ „์ฒด ์‚ญ์ œ ์‹คํ–‰");
    		s.clear();
    		System.out.println("toString() ์ „์ฒด์ถœ๋ ฅ: "+s);
    	}
    }

    Set๊ณ„์—ด์€ ์ €์žฅ ์ˆœ์„œ๊ฐ€ ์—†์–ด์„œ ์ค‘๋ณต์ €์žฅ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ  ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์œ„์น˜๊ฐ’์œผ๋กœ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ๋„ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์šฉ๋„๋กœ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. Set์€ ์ธํ„ฐํŽ˜์ด์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— Setํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ํ•˜์œ„ํด๋ž˜์Šค์ธ HashSet์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

    ๋จผ์ € HashSet ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. Set์€ ์ธํ„ฐํŽ˜์ด์Šค๋ผ ์ง์ ‘ ๊ฐ์ฒด์ƒ์„ฑ์„ ํ•  ์ˆœ ์—†์ง€๋งŒ ๋‹คํ˜•์„ฑ์„ ์ ์šฉํ•ด ๋ฐ์ดํ„ฐํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  <String>ํƒ€์ž…์œผ๋กœ ์ œ๋„ค๋ฆญ์„ ์ง€์ •ํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— HashSet ๊ฐ์ฒด๋Š” Stringํƒ€์ž…๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

    ๊ฐ์ฒด์ƒ์„ฑํ•œ ํ•ด์‰ฌ์…‹์— ์ปฌ๋ ‰์…˜์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ ๋ฉ”์†Œ๋“œ์ธ add()๋ฅผ ์‚ฌ์šฉํ•ด ์ŠคํŠธ๋งํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ํ™๊ธธ๋™์ด ์„ธ ๋ฒˆ์ด๋‚˜ ๋“ฑ์žฅํ•˜์ง€๋งŒ setํŠน์ง•์— ๋”ฐ๋ผ ์ค‘๋ณต์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ™๊ธธ๋™์€ ํ•œ๋ฒˆ๋งŒ ์ €์žฅ๋œ๋‹ค.

    ์ปฌ๋ ‰์…˜์—์„œ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” iterator() ๋ฉ”์†Œ๋“œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค Iterator ํƒ€์ž…์ด๋‹ค. while๋ฌธ ์กฐ๊ฑด์‹ ์•ˆ์— hasNext() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณต ๋Œ๋ฆฌ๋ฉด์„œ ๋‹ค์Œ์— ๋˜ ํ™•์ธํ•  ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ๋ฌผ์–ด๋ณด๊ณ , ์กฐ๊ฑด์ด true์ด๋ฉด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. next() ๋ฉ”์†Œ๋“œ๋Š” ์ฆ๊ฐ์‹์ฒ˜๋Ÿผ ๋‹ค์Œ ์š”์†Œ๋กœ ์ด๋™ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. 

     

    Object [] xxx = s.toArray();
    	for(Object o : xxx) {
    		System.out.println(o);
    	}

    Object [] ํƒ€์ž…์˜ toArray() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปฌ๋ ‰์…˜์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. HashSet์— ์ €์žฅ๋œ ๊ฐ์ฒด๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ forEach ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. ์ปฌ๋ ‰์…˜์—์„œ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜๋˜๋ฉด ์ด์ œ index๊ฐ€ ์ƒ๊ธฐ๊ณ  ์ค‘๋ณต์ €์žฅ์ด ๋˜๋Š” ๋“ฑ ์ €์žฅํƒœ๊ฐ€ ๋ฐ”๋€๋‹ค.

     

     

    Person ํด๋ž˜์Šค๋กœ Set ์—ฐ์Šตํ•˜๊ธฐ

    public class Person {	
    	private String name;
    	private int age;
    	private String address;
    	
    	public Person() {}
    	public Person(String name, int age, String address) {
    		this.name = name;
    		this.age = age;
    		this.address = address;
    	}
    	@Override
    	public String toString() {
    		return "Person [name=" + name + ", age=" + age + ", address=" + address + "]";
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public String getAddress() {
    		return address;
    	}
    	public void setAddress(String address) {
    		this.address = address;
    	}	
    }

    Person ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  private๋ณ€์ˆ˜, ์ƒ์„ฑ์ž, private ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” get, set ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  toString()ํ•จ์ˆ˜๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ–ˆ๋‹ค. toString()์€ ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    public class SetTest3 {
    	public static void main(String[] args) {
    		Set<Person> set = new HashSet<Person>();
    		Person kkkk = new Person("์œ ๊ด€์ˆœ", 17, "์„œ์šธ");
    		set.add(new Person("ํ™๊ธธ๋™", 20, "์„œ์šธ"));
    		set.add(new Person("์ด์ˆœ์‹ ", 44, "์ „๋ผ"));
    		set.add(new Person("์ด์ˆœ์‹ ", 44, "์ „๋ผ"));
    		set.add(kkkk);
            	set.add(kkkk);
    		System.out.println(set);
    		for(Person x : set) {
    			System.out.println(x.getName());
    		}
    		Iterator<Person> iterator = set.iterator();
    		while(iterator.hasNext()) {
    			Person p = iterator.next();
    			System.out.println(p.getName());
    		}		
    	}
    }

    Person ํด๋ž˜์Šค๋ฅผ Set๊ณ„์—ด ์ปฌ๋ ‰์…˜์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒ์œ„ํด๋ž˜์Šค Set์˜ <Person>ํด๋ž˜์Šคํƒ€์ž… ์ œ๋„ค๋ฆญ์œผ๋กœ HashSet ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค. Person ํด๋ž˜์Šค ์ž์ฒด์˜ ๊ฐ์ฒด๋„ ์ƒ์„ฑํ•œ๋‹ค. kkkk์ด๋ฆ„์œผ๋กœ ๊ฐ์ฒด์ƒ์„ฑํ•˜๋ฉฐ ์ดˆ๊ธฐ๊ฐ’์„ ํ•จ๊ป˜ ๋„ฃ์–ด์คฌ๋‹ค.

    Personํด๋ž˜์Šค์— 3๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ ํ•ด์‰ฌ์…‹์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. ์ด ์ค‘์—์„œ ์ด์ˆœ์‹  ๋ฐ์ดํ„ฐ์˜ ๋‚ด์šฉ์€ ์ค‘๋ณต๋œ๋‹ค. ๋‚ด์šฉ์€ ๋™์ผํ•˜์ง€๋งŒ ๊ฐ๊ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋‹ค๋ฅธ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ Set์— ์ค‘๋ณต ์ €์žฅ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ set์— ์ €์žฅ๋œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ์ฐธ์กฐ๋ณ€์ˆ˜ kkkk์˜ ๋ฐ์ดํ„ฐ๋Š” ์ค‘๋ณต์ €์žฅ์ด ์•ˆ๋˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. kkkk๋Š” ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ์–ด์„œ set์— ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค.

     

     

    ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ปฌ๋ ‰์…˜ ์ฃผ๊ณ ๋ฐ›๊ธฐ

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    public class SetTest3 {
    	public Set<Person> getPersonSet(Set<Person> set) {
    		Person kkkk = new Person("์œ ๊ด€์ˆœ", 17, "์„œ์šธ");
    		set.add(new Person("ํ™๊ธธ๋™", 20, "์„œ์šธ"));
    		set.add(new Person("์ด์ˆœ์‹ ", 44, "์ „๋ผ"));
    		set.add(new Person("์ด์ˆœ์‹ ", 44, "์ „๋ผ"));
    		set.add(kkkk);
    		set.add(kkkk);
    		return set;
    	}
    
    	public static void main(String[] args) {
    		SetTest3 test = new SetTest3();
    		// ๋ฉ”์ธ์—์„œ set์„ ์ƒ์„ฑํ•ด์„œ getPersonSet์—๊ฒŒ ๋„˜๊ฒจ์ฃผ๊ธฐ
    		Set<Person> set = new HashSet<Person>(); // ๋น„์–ด์žˆ๋Š” set์ƒ์„ฑ
    		Set<Person> set2 = test.getPersonSet(set); // ๋นˆ set์„ ํ•จ์ˆ˜์— ๋„˜๊น€, ํ•จ์ˆ˜์—์„œ ๊ฐ์ฒด๋ฅผ ๋‹ด์•„์„œ ๋ฆฌํ„ด
    
    		System.out.println(set2);
    		for (Person x : set2) {
    			System.out.println(x.getName());
    		}
    		Iterator<Person> iterator = set2.iterator();
    		while (iterator.hasNext()) {
    			Person p = iterator.next();
    			System.out.println(p.getName());
    		}
    	}
        
    }

    mainํ•จ์ˆ˜์—์„œ Set์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ getPersonSet ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด๋ณธ๋‹ค. main์—์„œ HashSet ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  getPersonSet ๋ฉ”์†Œ๋“œ์— HashSet ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ณ€์ˆ˜์ธ set ์ปฌ๋ ‰์…˜์„ ์ธ์ž๋กœ ๋‹ด์•„์„œ ํ˜ธ์ถœํ–ˆ๋‹ค.

    getPersonSet ๋ฉ”์†Œ๋“œ๋Š” set์„ ์ธ์ž๋กœ ๋ฐ›์•„์„œ ์ปฌ๋ ‰์…˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ €์žฅ์™„๋ฃŒ๋œ ์ปฌ๋ ‰์…˜์„ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค. ๋ฐ˜ํ™˜๋ฐ›์€ ์ปฌ๋ ‰์…˜์„ set2 ๋ณ€์ˆ˜์— ๋„ฃ๊ณ  set2์˜ ๋ฐ์ดํ„ฐ๋“ค์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ปฌ๋ ‰์…˜์— ๋‹ด๊ธด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๊ณ , forEach์™€ Iterator ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด ์ด๋ฆ„๋งŒ ๋ชจ๋‘ ์ถœ๋ ฅํ•ด๋ณธ๋‹ค.

     

     

    getPersonSet(), printPerson() ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง„ setService ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

    import java.util.HashSet;
    import java.util.Set;
    
    public class SetService {
    	public Set<Person> getPersonSet() {
    		Set<Person> set = new HashSet<Person>();
    		Person kkkk = new Person("์œ ๊ด€์ˆœ", 17, "์„œ์šธ");
    		set.add(new Person("ํ™๊ธธ๋™", 20, "์„œ์šธ"));
    		set.add(new Person("์ด์ˆœ์‹ ", 44, "์ „๋ผ"));
    		set.add(new Person("์ด์ˆœ์‹ ", 44, "์ „๋ผ"));
    		set.add(kkkk);
    		return set;
    	}
    	public void printPerson(Set<Person> set) {
    		for (Person person : set) {
    			System.out.println(person);
    		}
    	}
    }

    ์œ„์—์„œ ๋งŒ๋“  getPersonSet ๋ฉ”์†Œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™€ ์ƒˆ๋กœ์šด SetService ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค. Set์— ์ €์žฅ๋œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ณต๋ฌธ๋„ ๊ฐ€์ ธ์™€์„œ printPerson ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค.

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    public class SetTest4 {
    	public static void main(String[] args) {
    		SetService test = new SetService();
    		Set<Person> set = test.getPersonSet(); //์ž๋ฃŒ๋‹ด๊ธด set๋ฐ›์•„์˜ค๊ธฐ
    		test.printPerson(set); //set์˜ ๋‚ด์šฉ ์ถœ๋ ฅ ์š”์ฒญ
    	}
    }

    mainํ•จ์ˆ˜์—์„œ๋Š” setService ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•จ์ˆ˜๋งŒ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ„๊ฒฐํ•ด์กŒ๋‹ค.

     

     

    DAO์—ฐ์Šต

    ํŒจํ‚ค์ง€๋Š” ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์„œ ์—ฐ์Šตํ•ด๋ณธ๋‹ค.

     

    package com.dao;
    
    public class Person {
    	private String name;
    	private int age;
    	private String address;
    	
    	public Person() {}
    	public Person(String name, int age, String address) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.address = address;
    	}	
    	@Override
    	public String toString() {
    		return "Person [name=" + name + ", age=" + age + ", address=" + address + "]";
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public String getAddress() {
    		return address;
    	}
    	public void setAddress(String address) {
    		this.address = address;
    	}	
    }

    Person ํด๋ž˜์Šค๋ฅผ ๊ณ„์† ์‚ฌ์šฉํ•œ๋‹ค.

    package com.dao;
    
    import java.util.HashSet;
    
    public class OracleDAO {
    	//select ํ˜ธ์ถœ์‹œ ๋ฐ์ดํ„ฐ ์ €์žฅ๋œ set๋ฐ˜ํ™˜
    	public HashSet<Person> select() {
    		HashSet<Person> set = new HashSet<Person>();
    		set.add(new Person("ํ™๊ธธ๋™", 20, "์„œ์šธ"));
    		set.add(new Person("์ด์ˆœ์‹ ", 44, "์ „๋ผ"));
    		set.add(new Person("์ด์ˆœ์‹ ", 44, "์ „๋ผ"));
    		return set;
    	}
    }

    OracleDAO ํด๋ž˜์Šค์— select ๋ฉ”์†Œ๋“œ์— <Person> ์ œ๋„ˆ๋ฆญ ํƒ€์ž…๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” HashSet ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ ๋’ค ๊ฐ’์ด ๋“ค์–ด์žˆ๋Š” ํ•ด์‰ฌ์…‹์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    package com.service;
    
    import java.util.HashSet;
    import com.dao.OracleDAO;
    import com.dao.Person;
    
    public class OracleService {
    	OracleDAO dao;
    	
    	public OracleService() {
    		dao = new OracleDAO(); 
    	}
        
    	public HashSet<Person> select() {
    		HashSet<Person> xxx = dao.select();
    		return xxx;
    	}
    }

    OracleService ํด๋ž˜์Šค์— ์œ„์—์„œ ๋งŒ๋“  OracleDAO ํƒ€์ž…์˜ ๋ณ€์ˆ˜ dao๋ฅผ ๋งŒ๋“ค๊ณ  ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ ๋‹ค. ๋ฉ”์ธํ•จ์ˆ˜์—์„œ OracleService ๊ฐ์ฒด ์ƒ์„ฑ์‹œ OracleDAO๋ฅผ ์ƒ์„ฑ ํ›„ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•˜๊ฒŒ๋œ๋‹ค. <Person>ํด๋ž˜์Šค ํƒ€์ž…๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” HashSetํƒ€์ž…์˜ select ํ•จ์ˆ˜๋Š” OracleDAO์˜ ์…€๋ ‰ํŠธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋“ค์„ ๋ฐ›์€ ๋’ค ๊ทธ๊ฑธ xxx๋ณ€์ˆ˜์— ์ €์žฅํ•ด์„œ ๋ฆฌํ„ดํ•œ๋‹ค.

    import java.util.HashSet;
    import com.dao.Person;
    import com.service.OracleService;
    
    public class OracleMain {
    	public static void main(String[] args) {
    		OracleService service = new OracleService();
    		HashSet<Person> xxx = service.select();
    		for (Person s : xxx) {
    			System.out.println(s);
    		}
    	}
    }

    ์ด์ œ ๋ฉ”์ธํ•จ์ˆ˜ ์•ˆ์—์„œ๋Š” OracleService์˜ ๊ฐ์ฒด๋งŒ ์ƒ์„ฑํ•˜๋ฉด ๋œ๋‹ค. ๊ฐ์ฒด์ƒ์„ฑ ์‹œ ์œ„์—์„œ ์ง€์ •ํ•œ ๊ธฐ๋ณธ์ƒ์„ฑ์ž์— ๋”ฐ๋ผ  OracleDAO์˜ ๊ฐ์ฒด๋„ ์ƒ์„ฑ๋œ๋‹ค. OracleService ํด๋ž˜์Šค์˜ select ํ•จ์ˆ˜์—์„œ ๋ฆฌํ„ด๋ฐ›์€ ํ•ด์‰ฌ์…‹ ๋ฐ์ดํ„ฐ๋“ค์„ xxx ๋ณ€์ˆ˜์— ๋‹ด๊ณ  forEach ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

     

    ๐Ÿ˜œ NEXT : List, MAP ๊ณต๋ถ€ํ•˜๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€