๊ด€๋ฆฌ ๋ฉ”๋‰ด

ejyoo's ๊ฐœ๋ฐœ ๋…ธํŠธ

TreeSet ๋ณธ๋ฌธ

BackEnd/Java

TreeSet

ejyoovV 2021. 3. 7. 19:41

๐Ÿ’กTreeSet?

์ž๋™ ์ •๋ ฌ ๊ธฐ๋Šฅ ์กด์žฌํ•จ.

๋…ธ๋“œ ๊ตฌ์„ฑ์ž„.

 

๐Ÿ’กTreeSet ์ž๋™ ์ •๋ ฌ ํ™•์ธ

TreeSet์€ ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž๋™์ •๋ ฌ์ด ๋œ๋‹ค.

๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์‚ฝ์ž…๋œ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰๋๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

import java.util.TreeSet;

public class Main {
	public static void main(String[] args) {
		TreeSet<String> ts = new TreeSet<String>();
		
		for(char ch = 'Z' ; ch >='A' ; ch--) {
			String temp = String.valueOf(ch);
			
			ts.add(temp);
		}
		
		System.out.println("TreeSet์˜ ์ž๋ฃŒ : " + ts);
	}
}

๐Ÿ“String temp = String.valueOf(ch) ์„ค๋ช…

- charํƒ€์ž…์˜ ๋ฌธ์ž๋ฅผ String์œผ๋กœ ํ˜• ๋ณ€ํ™˜ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฌธ์žฅ

- String temp = (String) ch; ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฌธ๋ฒ•์ž„

  * ์บ์ŠคํŒ…์„ ํ•  ์ˆ˜ ์—†๋Š” ๊ตฌ์กฐ์ž„

  * String ๊ณผ char ์‚ฌ์ด์—๋Š” ์ƒ์†๊ด€๊ณ„๊ฐ€ ์—†์Œ. ๋ถ€๋ชจ๊ฐ€ Object๋ผ๋Š”๊ฒƒ๋งŒ ๊ฐ™์Œ. 

  * ๋”ฐ๋ผ์„œ ์ƒ์†๊ด€๊ณ„๊ฐ€ ์—†๋Š” ์ž๋ฃŒํ˜•์˜ ํ˜•๋ณ€ํ™˜์€ ๋ณ€ํ™˜ํ•  ์ž๋ฃŒํ˜•์— ๊ตฌํ˜„๋œ ํ˜•๋ณ€ํ™˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š”๋ฐ ๊ทธ๊ฒƒ์ด valueOf์ž„.

 

๐Ÿ’กTreeSet ์˜ ๋…ธ๋“œ ์ž๋ฃŒ ์ถœ๋ ฅ - ๊ธฐ์ค€๊ฐ’ ์ด์ „ ์ดํ›„ ์ž๋ฃŒ ์ถœ๋ ฅ

TreeSet์— ์ €์žฅ๋œ ์ž๋ฃŒ๋ฅผ ๊ธฐ์ค€ ๋…ธ๋“œ๋ฅผ ์ •ํ•ด๋†“๊ณ  ๊ทธ ๊ธฐ์ค€ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘์€ ์ž๋ฃŒ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

(Tree๋ฅผ ๊ทธ๋ ค์•ผ๋˜๋Š”๋ฐ.. ๊ท€์ฐฎ์•„์„œ ์•ˆ๊ทธ๋ฆผ)

- ๊ธฐ์ค€๊ฐ’์„ ์ •ํ•œ๋‹ค.

- ๊ธฐ์ค€๊ฐ’์„ ํฌํ•จํ•˜์—ฌ ์ถœ๋ ฅํ• ์ง€, ๊ธฐ์ค€๊ฐ’์„ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  ์ถœ๋ ฅํ• ์ง€ ์ •ํ•œ๋‹ค.

- ๊ธฐ์ค€๊ฐ’ ์ด์ „ ์ž๋ฃŒ๋ฅผ ์ถœ๋ ฅ(headSet() ๋ฉ”์„œ๋“œ)ํ• ์ง€ ์ดํ›„ ์ž๋ฃŒ(tailSet() ๋ฉ”์„œ๋“œ)๋ฅผ ์ถœ๋ ฅํ• ์ง€ ์ •ํ•œ๋‹ค.

๐Ÿ“์ž๋ฃŒ ์ถœ๋ ฅ ๋ฐฉ๋ฒ•

๋…ธ๋“œ ์ž๋ฃŒ ์ถœ๋ ฅ ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

- TreeSet์˜ headSet(), tailSet() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ false(๊ธฐ์ค€๊ฐ’ ๋ฏธํฌํ•จ), true(๊ธฐ์ค€๊ฐ’ ํฌํ•จ) ์„ค์ •ํ•œ๋’ค ์ถœ๋ ฅ

- SortSetํด๋ž˜์Šค์— headSet(), tailSet() ๋‚ด ๊ธฐ์ค€๊ฐ’์„ ์ธ์ž๊ฐ’์œผ๋กœ ๋„ฃ์–ด SortSet์— ์‚ฝ์ž…ํ•œ๋’ค SortSet์ถœ๋ ฅ

  • TreeSet ์˜ headSet() ๊ธฐ์ค€๊ฐ’ ๋ฏธํฌํ•จ(false), ๊ธฐ์ค€๊ฐ’ ํฌํ•จ(true)
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) {
		TreeSet<String> ts = new TreeSet<String>();
		
		for(char ch = 'Z' ; ch >='A' ; ch--) {
			String temp = String.valueOf(ch);
			
			ts.add(temp);
		}
		
		System.out.println("TreeSet์˜ ์ž๋ฃŒ : " + ts);
		System.out.println("===============================");
		
		System.out.println("K ์ด์ „ ์ž๋ฃŒ (๊ธฐ์ค€๊ฐ’ ๋ฏธํฌํ•จ) : " + ts.headSet("K", false));
		System.out.println("K ์ด์ „ ์ž๋ฃŒ (๊ธฐ์ค€๊ฐ’ ํฌํ•จ) : " + ts.headSet("K", true));
	}
}

  • TreeSet ์˜ tailSet() ์ดํ›„ ์ž๋ฃŒ ๊ฒ€์ƒ‰ - ๊ธฐ์ค€๊ฐ’ ๋ฏธํฌํ•จ(false), ๊ธฐ์ค€๊ฐ’ ํฌํ•จ(true)
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) {
		TreeSet<String> ts = new TreeSet<String>();
		
		for(char ch = 'Z' ; ch >='A' ; ch--) {
			String temp = String.valueOf(ch);
			
			ts.add(temp);
		}
		
		System.out.println("TreeSet์˜ ์ž๋ฃŒ : " + ts);
		System.out.println("===============================");
		
		System.out.println("K ์ดํ›„ ์ž๋ฃŒ (๊ธฐ์ค€๊ฐ’ ๋ฏธํฌํ•จ) : " + ts.tailSet("K", false));
		System.out.println("K ์ดํ›„ ์ž๋ฃŒ (๊ธฐ์ค€๊ฐ’ ํฌํ•จ) : " + ts.tailSet("K", true));
	}
}

 

๐Ÿ’กTreeSet ์˜ ๋…ธ๋“œ ์ž๋ฃŒ ์ถœ๋ ฅ - ๋ถ€๋ถ„ ์ง‘ํ•ฉ ์ถœ๋ ฅ (๋ฒ”์œ„์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’๋งŒ ์ถœ๋ ฅ)

- TreeSet subSet() ๋ฉ”์„œ๋“œ : ๋ถ€๋ถ„์ง‘ํ•ฉ => ์ผ์ • ๋ฒ”์œ„์˜ ์ง‘ํ•ฉ์„ ์ถœ๋ ฅํ•˜๊ฒ ๋‹ค.

- subSet(๊ธฐ์ค€๊ฐ’1, ๊ธฐ์ค€๊ฐ’2) : ๊ธฐ์ค€๊ฐ’1 ~ ๊ธฐ์ค€๊ฐ’2 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

- ๊ธฐ์ค€๊ฐ’ ํฌํ•จ ๋ฏธํฌํ•จ ๊ฒฐ์ • ๊ฐ€๋Šฅํ•จ.(true : ํฌํ•จ, false : ๋ฏธํฌํ•จ)

- subSet(๊ธฐ์ค€๊ฐ’1, true || false, ๊ธฐ์ค€๊ฐ’2, true || false)

import java.util.TreeSet;

public class Main {
	public static void main(String[] args) {
		TreeSet<String> ts = new TreeSet<String>();
		
		for(char ch = 'Z' ; ch >='A' ; ch--) {
			String temp = String.valueOf(ch);
			
			ts.add(temp);
		}
		
		System.out.println("TreeSet์˜ ์ž๋ฃŒ : " + ts);
		System.out.println("===============================");
		
		System.out.println("K(ํฌํ•จ)๋ถ€ํ„ฐ N(๋ฏธํฌํ•จ)๊นŒ์ง€ : " + ts.subSet("K", true, "N", false));
		System.out.println("K(ํฌํ•จ)๋ถ€ํ„ฐ N(ํฌํ•จ)๊นŒ์ง€ : " + ts.subSet("K", true, "N", true));
		System.out.println("K(๋ฏธํฌํ•จ)๋ถ€ํ„ฐ N(๋ฏธํฌํ•จ)๊นŒ์ง€ : " + ts.subSet("K", false, "N", false));
		System.out.println("K(๋ฏธํฌํ•จ)๋ถ€ํ„ฐ N(ํฌํ•จ)๊นŒ์ง€ : " + ts.subSet("K", false, "N", true));
	}
}

'BackEnd > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Map  (0) 2021.03.08
Hash - equals()์™€ hash() ๋ฉ”์„œ๋“œ ๊ตฌํ˜„  (0) 2021.03.08
Set  (0) 2021.03.07
List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (3)  (0) 2021.03.06
List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (2)  (0) 2021.03.06