[Java]HashSet, LinkedHashSet 사용법

JANG SEONG SU·2023년 6월 6일
0

Java

목록 보기
1/10

개요

알고리즘을 풀던 중 HashSet을 사용해야 했고, HashSet이 저장 순서가 유지되지 않는다는 점으로 인해, LinkedHashSet을 사용해야했다. 이를 계기로 HashSet과 LinkedListHashSet의 사용법을 간략히 적으려고 한다.


내용

1. HashSet 기본 함수

1-1. HashSet 변수 선언
HashSet<데이터타입> set2 = new HashSet<데이터타입>();
//HashSet<Integer> set = new HashSet<Integer>();		
//HashSet<String> set2 = new HashSet<String>();
1-2. HashSet 값 추가
public class HashSetTest {
	public static void main(String[] args)  {	
		// Integer
		HashSet<Integer> set = new HashSet<Integer>();	
		
		set.add(1);
		set.add(2);
		set.add(3);
		set.add(1);
				
		// String
		HashSet<String> set2 = new HashSet<String>();

		set2.add("a");
		set2.add("b");
		set2.add("c");
		set2.add("a");
	}

1-3. HashSet 값 삭제

public class HashSetTest {
	public static void main(String[] args)  {	
		// Integer
		HashSet<Integer> set = new HashSet<Integer>();	
		set.remove(1);		//1 삭제
		set.clear();		//HashSet 초기화
				
		// String
		HashSet<String> set2 = new HashSet<String>();
		set2.remove("a");
		set2.clear();
	}
}

1-4. HashSet 크기 구하기

public class HashSetTest {
	public static void main(String[] args)  {	
		// Integer
		HashSet<Integer> set = new HashSet<Integer>();	
		
		set.add(1);
		set.add(2);
		set.add(3);
		set.add(1);
		System.out.println("set의 크기 : " + set.size());
	}
}

1-5 HashSet 포함 여부 확인

public class HashSetTest {
	public static void main(String[] args)  {	
		// Integer
		HashSet<Integer> set = new HashSet<Integer>();	
		
		set.add(1);
		set.add(2);
		set.add(3);
		set.add(1);
		
		System.out.println("1은 있는가? : " + set.contains(1));	//출력 : 1은 있는가? true
	}
}

1-5 HashSet 출력

public class HashSetTest {
	public static void main(String[] args)  {	
		HashSet<Integer> set = new HashSet<Integer>();	
		
		set.add(1);
		set.add(2);
		System.out.println("set의 값 : " + set);	//set의 값 : [1, 2]
				
		Iterator iter = set.iterator();
		while(iter.hasNext()) {
			System.out.print(iter.next() + " ");
		}
		System.out.println("");
	}
}

2. HashSet 정렬

2-1. HashSet를 List로 변환하고 정렬
public class HashSetTest {

    public static void main(String[] args) {

        HashSet<String> set = new HashSet<>();
        myHashSet.add("kiwi");
        myHashSet.add("apple");
        myHashSet.add("melon");

        System.out.println("Set: " + myHashSet);

        List<String> list = new ArrayList<>(myHashSet);

        Collections.sort(al);
        System.out.println("Sorted list: " + al);
    }
}

3. LinkedList

HashSet<>은 저장 순서가 유지되지 않는다. 대신 LinkedList<>를 사용하면 저장순서가 유지된다.

public class Main{

    public static void main(String[] args) throws IOException {

        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

        String[] strings = bf.readLine().split(" ");
        int n = Integer.parseInt(strings[1]);

        LinkedHashSet<String> set = new LinkedHashSet<>();
        //HashSet<String> set = new HashSet<>(); 저장 순서 유지 X

        for(int i=0; i<n; i++) {
            set.add(bf.readLine());
        }

        Iterator iter = set.iterator();
        for(String s : set) {
            System.out.println(s);
        }

    }
}
profile
Software Developer Lv.0

0개의 댓글