본문 바로가기
자료구조

[자료구조] Hash Set

by eddypark 2023. 11. 7.

- 입력한 순서가 보장되지 않는다.

- 중복된 값을 허용하지 않는다.

- NULL 삽입이 가능하다.


생성

import java.util.HashSet;

HashSet<String> colors1 = new HashSet<String>(); // 타입 지정
HashSet<String> colors2 = new HashSet<>(); // 타입 생략 가능
HashSet<String> colors3 = new HashSet<>(10); // 초기 용량(Capacity) 설정
HashSet<String> colors4 = new HashSet<>(colors1); // 다른 Collection값으로 초기화
HashSet<String> colors5 = new HashSet<>(Arrays.asList("Blue", "Black", "White")); // Arrays.asList()
HashSet<String> colors6 = new HashSet<>(){{
    add("Blue");
    add("Black");
    add("White");
}};

추가

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
    	HashSet<String> colors = new HashSet<>();
        colors.add("Black");
        colors.add("White");
        colors.add("Green");
        colors.add("Red");
        
        System.out.println(colors);
    }
}

 

- 입력된 순서가 보장되지 않기 때문에 특정 위치에 값을 추가하거나 할 수는 없다.

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
    	HashSet<String> colors = new HashSet<>();
        colors.add("Black");
        colors.add("White");
        colors.add("Green");
        
        HashSet<String> otherColors = new HashSet<>(Array.asList("Black", "Yellow", "Purple"));
        colors.addAll(otherColors);
        
        System.out.println(colors);
    }
}

 

- 다른 컬렉션의 값들을 한 번에 입력할 때는 addAll()을 사용한다.

- 값들을 입력할 때 중복된 값은 제거된다.


삭제

import java.util.HashSet;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
    	HashSet<String> colors = new HashSet<>();
        colors.add("Black");
        colors.add("White");
        colors.add("Green");
        colors.add("Blue");
        colors.add("Red");
        colors.add("Purple");
        
        colors.remove("Red");
        System.out.println(colors); // [White, Blue, Purple, Black, Green]
        
        colors.removelf(color -> color.startsWith("B"));
        System.out.println(colors); // [White, Purple, Green]
        
        colors.removeAll(Arrays.asList("White", "Green"));
        System.out.println(colors); // [Purple]
        
        colors.clear();
        System.out.println(colors); // []
        
    }
}

 

- remove() - 특정 값을 삭제할 때(값이 지워지면 true를 리턴한다. 즉, 값의 존재 여부 확인 가능)  사용한다.

- removelf() - 조건을 적용해서 여러 값을 삭제할 때 사용한다.

- removeAll() - 다른 컬렉션에 있는 값들을 입력 값으로 받아 해당 값들을 찾아서 삭제한다.

- clear() - 모든 값을 삭제한다.


전체 값 확인

import java.util.HashSet;
import java.util.Iterator;

public class Main {
    public static void main(String[] args) {
    	HashSet<String> colors = new HashSet<>();
        colors.add("Black");
        colors.add("White");
        colors.add("Green");
        colors.add("Red");
        
        // for-each loop
        for (String colors : colors) {
        	System.out.print(color + " ");
        }
        System.out.println();
        
        // using iterator
        Iterator<String> iterator = colors.iterator();
        while (iterator.hasNext()) {
        	System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }
}

값 존재 유무 확인

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
    	HashSet<String> colors = new HashSet<>();
        colors.add("Black");
        colors.add("White");
        colors.add("Green");
        colors.add("Red");
        
        System.out.println(colors.contains("Green"));
        System.out.println(colors.contains("Purple"));
    }
}

- contains() - 값이 존재하면 true, 존재하지 않으면 false

- 값의 존재 여부를 파악하는 것이 중요하다면 HashSet은 매우 적절

'자료구조' 카테고리의 다른 글

[자료구조] HashTable  (0) 2023.11.09
[자료구조] TreeSet  (0) 2023.11.07
[자료구조] Vector  (0) 2023.11.06
[자료구조] Stack  (0) 2023.11.06
[자료구조] ArrayList  (0) 2023.11.03