8. HashSet

연결리스트와 set의 차이는

https://images.velog.io/images/tonyhan18/post/0455adfe-5ee9-40b0-9610-7ece62e5bf7e/image.png

Set의 Class는 위와 같이 구성이 된다. 우리는 HashSet과 TreeSet을 주로 사용하게 될 계획이다.

HashSet: Methods

Constructor은 위의 4가지가 존재한다.

![<https://images.velog.io/images/tonyhan18/post/71158347-b6fa-4d9b-86fb-d8d56d92b47b/image.png>](<https://images.velog.io/images/tonyhan18/post/71158347-b6fa-4d9b-86fb-d8d56d92b47b/image.png>)

이 외의 함수로는 위와 같이 존재한다.

![<https://images.velog.io/images/tonyhan18/post/8d9cdb1b-22df-422e-8bb1-ebf0708912fc/image.png>](<https://images.velog.io/images/tonyhan18/post/8d9cdb1b-22df-422e-8bb1-ebf0708912fc/image.png>)

HashSet: Example 1

class Lecture {
	public static void main(String[] args) {
		String[] strArr = {"1", "1", "2", "2", "3", "3", "4", "4", "4"};
		Set<String> set = new HashSet<>();

        // set에 element가 계속 추가 된다.
		for(int i=0; i<strArr.length; i++) {
			set.add(strArr[i]);
		}
		System.out.println(set);
	}
}

중복을 허용하지 않기 때문에 다음과 같은 출력결과가 나올 것이 예상된다.

https://images.velog.io/images/tonyhan18/post/3ac0296e-4e4e-4d6e-a110-368f2bc26ae8/image.png

// ArrayList로 했을때의 결과

https://images.velog.io/images/tonyhan18/post/b596675e-8b00-48de-a737-52e6353610f3/image.png

HashSet: Example 2