Zeta Oph's Study

[Data Structure (Java)] Set 본문

프로그래밍/Java

[Data Structure (Java)] Set

Zeta Oph 2024. 6. 5. 18:35

https://crane206265.tistory.com/88

 

[Data Structure (Java)] Hash (Separate Chaining & Linear Probing)

https://crane206265.tistory.com/87 [Data Structure (Java)] Map ADThttps://crane206265.tistory.com/86 [Data Structure (Java)] Heapsorthttps://crane206265.tistory.com/85 [Data Structure (Java)] Heaphttps://crane206265.tistory.com/84 [Data Structure (Java

crane206265.tistory.com

이번에는 Set입니다. 사실 Hash랑 Map이 재밌긴 했지만 실제로는 Set을 더 많이 쓸거 같은 이 기분..


Set ADT

: an unordered collection of distinct keys

- elements of a set : unique keys of a map without any associated values


Set API

API of Set(data type of element : E)

methods explanation
int size() return # of elements in the set
boolean isEmpty() return if the set is empty
boolean add(E e) add the given element e to the set,
return if the set changed
boolean remove(E e) remove the given element e from the set,
return if the set changed
boolean contains(E e) return if the given element e is in set

 

Java's Set Operations (operations above is also implemented)

methods explanation
boolean addAll(Collection<E> c) add all elements from the given collection c to the set,
return if the set changed
boolean containsAll(Collection<E> c) return if all elements from the given collection c is in the set
boolean removeAll(Collection<E> c) remove all elements from the given collection c from the set
return if the set changed
boolean retainAll(Collection<E> c) remove elements not found in the given collection c from the set
return if the set changed
boolean equals(Set<E> s) return if the given set s and this set is equal
Iterator<> iterator() return the iterator over the elements in the set
Object[] toArray() return the array over the elements in the set

Set Implementation

in java.util package

1. java.util.HashSet : unordered set implementation with a hash table
2. java.util.LinkedHashSet : set implementation with a hash table (in insertion order)
3. java.util.TreeSet : sorted set implementation with a binary tree