JAVA
Java - Collection Framework (Set Collection)
아랑팡팡
2023. 4. 14. 19:16
728x90
----------------------------------------------------------------------------------------------------------------------------------------------------
1. Set Collection
- List Collection은 객체의 저장 순서를 유지하지만, Set컬렉션은 저장 순서가 유지되지 않는다. 또한 객체를 중복해서 저장할 수 없고 Set컬렉션은 수학의 집합에 비유된다.
Function | Method | Ex |
객체추가 | boolean add(E e) | 주어진 객체를 저장, 객체가 저장되면 true를 리턴하고 중복 객체이면 false를 리턴한다. |
객체검색 | boolean contain(Object o) | 주어진 객체가 저장되어 있는지 여부 |
boolean isEmpty() | 컬렉션이 비어 있는지 검사 | |
Iterator<E> iterator() | 저장되어 있는 객체를 한 번씩 가져오는 반복자 리턴 | |
int size() | 저장되어 있는 전체 객체 수를 리턴 | |
객체삭제 | void clear() | 저장된 모든 객체를 삭제 |
boolean remove(Object o) | 주어진 객체를 삭제한다. |
- Set Collection은 인덱스로 저장되지 않기 때문에 인덱스로 객체를 검색해서 가져오는 메소드가 없다. 전체 객체를 대상으로 한번씩 반복해서 가져오는 반복자(Iterator)를 제공한다.
----------------------------------------------------------------------------------------------------------------------------------------------------
1) HashSet
- HashSet은 Set인터페이스의 구현 클래스로 HashSet은 중복을 허용하지 않는 Set Interface를 구현하는 클래스이다. (동일한 요소를 저장할 경우 하나의 요소만 저장된다.
- HashSet은 객체를 저장히기 전에 먼저 객체의 hashCode()메소드를 호출해 저장되어 있는 객체들의 해시코드와 비교한다.
package set;
import java.util.HashSet;
import java.util.Iterator;
public class Set01 {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
// 주어진 객체를 저장, 객체가 저장되면 true를 리턴하고 중복 객체이면 false를 리턴한다.
set.add("Lion");
set.add("Lion"); // Lion이 중복되어 하나만 true
set.add("Tiger");
set.add("Elephant");
set.add("Gorilla");
set.add("Giraffe");
// 주어진 객체가 저장되어 있는지 여부를 검사한다.
String ani = "Lion";
if(set.contains(ani)) {
System.out.println("Contains : " + ani + "는 동록된 동물입니다.");
}else {
System.out.println("Contains : " + ani + "는 동록되지 않은 동물입니다.");
}
// 저장되어 있는 전체 객체 수를 리턴한다.
System.out.println("size of set : " + set.size());
// Iterator, Enumeration = 저장되어 있는 객체를 한번씩 가져오는 반복자
Iterator<String> it = set.iterator();
while(it.hasNext()) {
String name = it.next();
System.out.println("Iterator : " + name);
}
// boolean remove(Object o) = 주어진 객체를 삭제한다.
String ani1 = "Lion";
if(set.remove(ani1)) {
System.out.println("Remove : " + ani1 + "delete");
}else {
System.out.println("Remove : " + ani1 + "doesn't exist");
}
// void cler = 저장된 모든 객체를 삭제한다.
set.clear();
// boolean.isEmpty = 컬렉션이 비어 있는지 검사한다.
if(set.isEmpty()) {
System.out.println("Clear,isEmpty : " + "data is not");
}else {
System.out.println("Clear,isEmpty : " + "have data");
}
}
}
2) HashTable
package set;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
public class Set02 {
public static void main(String[] args) {
Hashtable<String, String> ht = new Hashtable<String, String>();
ht.put("유재석", "개그맨");
ht.put("김종국", "가수");
ht.put("류현진", "야구선수");
ht.put("김연아", "피겨선수");
ht.put("황정민", "영화배우");
// 전통적인 방식의 데이터 추출
Enumeration<String> enu = ht.keys();
// Hashtable에 있는 모든 키값만 반복자 enu에 넘겨준다.
while(enu.hasMoreElements()) {
// enu이 가르키는 곳에 데이터가 있는지 확인
String key = enu.nextElement();
String value = ht.get(key);
System.out.println(key + "--> "+value);
}
System.out.println();
// API에서 권장하는 방식의 데이터 추출
Set<String> set = ht.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()) {
String key = it.next();
String value = ht.get(key);
System.out.println(key + "--> " + value);
}System.out.println();
// boolean containt(Object o) = 주어진 객체가 저장되어 있는지를 검사한다.
if(ht.containsKey("김종국")) { // containsKey는 키값의 유무 확인
System.out.println("김종국님은 저희 회원입니다.");
}else {
System.out.println("김종국님은 저희 회원이 아닙니다.");
}
// key값을 이용해서 value를 꺼내올 수 있다.
String value = ht.get("유재석");
System.out.println("유재석--> " + value);
// boolean remove(Object o) = 주어진 객체를 삭제한다.
ht.remove("유재석");
if(ht.get("유재석") == null) {
System.out.println(ht.get("유재석") + "입니다.");
}else {
System.out.println(ht.get("유재석") + "존재합니다.");
}
// int size() - 저장되어 있는 전체 객체 수를 리턴한다.
ht.put("황정민", "영화배우"); // 같은 키값이면 중복되지 않는다.
System.out.println("데이터의 크기 : " + ht.size());
}
}
728x90
반응형
LIST