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

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

List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (1) ๋ณธ๋ฌธ

BackEnd/Java

List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (1)

ejyoovV 2021. 3. 6. 16:40

๐Ÿ’ก Comparable, Comparator

๊ธฐ๋ณธ ์ •๋ ฌ์€ Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๊ณ 

๊ทธ ๋’ค์— ์ถ”๊ฐ€๋กœ ์ •๋ ฌ ๊ธฐ์ค€์„ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ Comparator๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

  • Comparable - comparaTo() ๋ฉ”์„œ๋“œ ๊ตฌํ˜„
  • Comparator - compare() ๋ฉ”์„œ๋“œ ๊ตฌํ˜„

๐Ÿ’ก ์ •๋ ฌ ๋ฐฉ์‹?

Collections ์˜ sort() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌ.

๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ(์ˆซ์ž : ์ž‘์€ ์ˆ˜->ํฐ์ˆ˜ / ๋ฌธ์ž : ์‚ฌ์ „์ˆœ)

์ •๋ ฌ ๋ฐฉ์‹ ๋ณ€๊ฒฝ ์›ํ•  ์‹œ, ์ •๋ ฌ ๋ฐฉ์‹ ๊ฒฐ์ •ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ๊ฒƒ.

 

๐Ÿ’ก ๊ธฐ๋ณธ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		
		list.add("์ผ์ง€๋งค");
		list.add("ํ™๊ธธ๋™");
		list.add("์„ฑ์ถ˜ํ–ฅ");
		list.add("๋ณ€ํ•™๋„");
		list.add("์ด์ˆœ์‹ ");
		
		System.out.println("์ •๋ ฌ ์ „ : " + list);
		
		Collections.sort(list);
		System.out.println("์ •๋ ฌ ํ›„ : " + list);
	}
}

 

๐Ÿ’ก ๊ธฐ๋ณธ ์ •๋ ฌ ์™ธ ์ถ”๊ฐ€๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ - Comparator

  ์ˆœ์„œ 1) ์ถ”๊ฐ€ ์ •๋ ฌ ๊ฐ์ฒด์— ๋Œ€ํ•œ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.(์ƒ์†)

  ์ˆœ์„œ 2) Comparator ์˜ compare() ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•œ๋‹ค.

  * ์ฐธ๊ณ  : [์˜ค๋ฆ„์ฐจ์ˆœ : 1 / ๋‚ด๋ฆผ์ฐจ์ˆœ : -1]

๐Ÿ“ compare ๋ฉ”์„œ๋“œ์—์„œ ์–‘์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ ๋‘ ๊ฐ’์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ตฌ์กฐ(์˜ค๋ฆ„์ฐจ์ˆœ ๊ธฐ๋ณธ๊ฐ’)

๐Ÿ“ ์˜ค๋ฆ„์ฐจ์ˆœ ๊ธฐ์ค€์ผ ๋•Œ ๋‹ค์Œ์˜ ๊ทœ์น™์ด ์กด์žฌ

  • ์•ž์˜ ๊ฐ’์ด ํผ : ์–‘์ˆ˜
  • ๊ฐ™์œผ๋ฉด : 0
  • ์•ž์˜ ๊ฐ’์ด ์ž‘์Œ : ์Œ์ˆ˜

๐Ÿ“ String ๊ฐ์ฒด์— ์ •๋ ฌ์„ ์œ„ํ•œ compareTo() ๋ฉ”์„œ๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ. (๋ฐ˜ํ™˜๊ฐ’์€ ์˜ค๋ฆ„์ฐจ์ˆœ์— ๋งž๊ฒŒ ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ)

๐Ÿ“ compareTo()๋Š” Wrapperํด๋ž˜์Šค์™€ Date, File ํด๋ž˜์Šค์—๋„ ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ.

class Desc implements Comparator<String>{

	@Override
	public int compare(String str1, String str2) {
		return str1.compareTo(str2) * -1;
	}
	
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		
		list.add("์ผ์ง€๋งค");
		list.add("ํ™๊ธธ๋™");
		list.add("์„ฑ์ถ˜ํ–ฅ");
		list.add("๋ณ€ํ•™๋„");
		list.add("์ด์ˆœ์‹ ");
		
		System.out.println("์ •๋ ฌ ์ „ : " + list);
		
		Collections.sort(list);
		System.out.println("์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ํ›„ : " + list);
		Collections.shuffle(list);//๋‚ด๋ฆผ์ฐจ์ˆœ ํ™•์ธ ์œ„ํ•ด ์ž๋ฃŒ๋ฅผ ์„ž์Œ.
		
		System.out.println("(์ž๋ฃŒ์„ž์Œ)์ •๋ ฌ ์ „ : " + list);
		Collections.sort(list, new Desc());
		System.out.println("๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ํ›„ : " + list);
	}
}

 

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

List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (3)  (0) 2021.03.06
List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (2)  (0) 2021.03.06
ArrayList  (0) 2021.03.06
Stack๊ณผ Queue  (0) 2021.03.06
ํ•ด์‹œ(Hash) ์•Œ๊ณ ๋ฆฌ์ฆ˜  (0) 2021.03.05