HashSet
- 중복된 값(원소)는 추가되지 않는 특성을 가짐
package practice;
import java.util.*;
public class hashset {
public static void main(String[] args) {
///////////////////////////////////////////////
System.out.print("1. 추가 : ");
Set hashSet = new HashSet();
hashSet.add("APPLE");
hashSet.add("BANANA");
hashSet.add("ORANGE");
Iterator it = hashSet.iterator();
while(it.hasNext()) System.out.print(it.next()+" ");
System.out.println();
///////////////////////////////////////////////
System.out.print("2. 중복제거 : ");
hashSet.add("APPLE");
it = hashSet.iterator();
while(it.hasNext()) System.out.print(it.next()+" ");
System.out.println();
///////////////////////////////////////////////
System.out.print("3. 삭제 : ");
hashSet.remove("BANANA");
it = hashSet.iterator();
while(it.hasNext()) System.out.print(it.next()+" ");
System.out.println();
///////////////////////////////////////////////
System.out.print("4. 리스트에 추가 : ");
List arrayList = new ArrayList();
arrayList.addAll(hashSet);
for (int i = 0; i < arrayList.size(); i++) {
System.out.print(arrayList.get(i)+" ");
}
System.out.println();
///////////////////////////////////////////////
List<Set<Integer>> list = new ArrayList<>();
list.add(null); // 0번째 배열에는 null
list.add(new HashSet<>()); // 순차적으로 배열에 hastset 지정
list.add(new HashSet<>());
list.get(2).add(new Integer(3));
list.get(2).add(new Integer(4));
list.get(2).add(new Integer(5));
System.out.println("5. 리스트에 객체로 추가 : " + list.get(2));
System.out.println("6. Iterator로 출력 : ");
Iterator it2 = list.get(2).iterator();
while(it2.hasNext()) System.out.print(it2.next()+" ");
}
}
[ Output ]
1. 추가 : APPLE BANANA ORANGE
2. 중복제거 : APPLE BANANA ORANGE
3. 삭제 : APPLE ORANGE
4. 리스트에 추가 : APPLE ORANGE
5. 리스트에 객체로 추가 : [3, 4, 5]
6. Iterator 로 출력 : 3 4 5
프로그래머스 - 동적계획법(Dynamic Programming) - N으로 표현
'Devops > Algorithm' 카테고리의 다른 글
[Java] N진수 변환 ( 3→10진수, 10→3진수 ) (1) | 2021.07.12 |
---|---|
[Java] [정수제곱근판별] Math.sqrt() vs Math.pow() (0) | 2021.06.24 |
[Java] [위장] Combination, HashMap ★ (0) | 2021.04.22 |
[Java] [소수찾기] 소수구하기/순열/조합 (0) | 2021.04.20 |
[Java] String, Character, StringBuilder 활용 (0) | 2021.04.16 |