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

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

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

BackEnd/Java

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

ejyoovV 2021. 3. 6. 17:24

๐Ÿ’ก ์ •๋ ฌ ๋ฐฉ์‹ 2๊ฐ€์ง€ Comparable, Comparator ์˜ˆ์ œ

 

๐Ÿ’ก ์‚ฌ์šฉ์ž ์ปค์Šคํ…€ ๊ฐ์ฒด Member ์ƒ์„ฑ -> ๊ธฐ๋ณธ ์ •๋ ฌ ์„ค์ •(Comparable)

์‚ฌ์šฉ์ž ์ปค์Šคํ…€ ํด๋ž˜์Šค ์ƒ์„ฑ

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

public class Main {
	public static void main(String[] args) {
		List<Member> memList = new ArrayList<Member>();
		memList.add(new Member(1, "ํ™๊ธธ๋™", "010-1111-1111"));
		memList.add(new Member(4, "๋ณ€ํ•™๋„", "010-2222-2222"));
		memList.add(new Member(6, "์„ฑ์ถ˜ํ–ฅ", "010-3333-3333"));
		memList.add(new Member(3, "์ด์ˆœ์‹ ", "010-4444-4444"));
		memList.add(new Member(2, "๊ฐ•๊ฐ์ฐฌ", "010-5555-5555"));
		memList.add(new Member(5, "์ผ์ง€๋งค", "010-6666-6666"));
		
		System.out.println("์ •๋ ฌ ์ „ : ");
		for(Member mem : memList) {
			System.out.println(mem);
		}
	}
}

class Member{
	private int 	num;
	private String 	name;
	private String 	tel;
	public int getNum() {
		return num;
	}
	
	public Member() {
		
	}
	
	public Member(int num, String name, String tel) {
		super();
		this.num = num;
		this.name = name;
		this.tel = tel;
	}
	
	@Override
	public String toString() {
		return "Member [num=" + num + ", name=" + name + ", tel=" + tel + "]";
	}

	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
}

๐Ÿ’ก ์œ„์˜ ์ฝ”๋“œ ๋ฆฌ๋ทฐ

์œ„์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐœ๋…์ด ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค.

  • toString() ์˜ค๋ฒ„๋ผ์ด๋“œ

์˜ค๋ฒ„๋ผ์ด๋“œ '@Override' ๋ฌธ๊ตฌ ์‚ฌ์šฉ ์ด์œ  : ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ํ•ด์•ผํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ปดํŒŒ์ผ ์‹œ ๊ทธ๊ฒƒ์˜ ์˜คํƒ€๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

์ด ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉ”์ธ ๋ฉ”์„œ๋“œ์—์„œ Member ์ถœ๋ ฅ ์‹œ ์ฃผ์†Œ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

@Override
	public String toString() {
		return "Member [num=" + num + ", name=" + name + ", tel=" + tel + "]";
	}

toString() ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•œ ๊ฒฝ์šฐ
toString() ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ

  • ์ƒ์„ฑ์ž ์„ ์–ธ
public Member() {
		
}
	
public Member(int num, String name, String tel) {
	super();
	this.num = num;
	this.name = name;
	this.tel = tel;
}

public Member(){}

- ๋‚˜ ์ž์‹ ์„ ์˜๋ฏธํ•˜๋Š” ์ƒ์„ฑ์ž

- this(); ๊ฐ€ ์ƒ๋žต๋˜์–ด ์ž‡์Œ.

- ์ดˆ๊ธฐ ๊ฐ’์„ ์„ค์ •ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ๋œ๋‹ค. [num=0, name=null, tel=null]

public Member(int num, String name, String tel){}

- ๋ชจ๋“  ์ƒ์„ฑ์ž๋Š” Object๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•˜๋ฏ€๋กœ super();๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ์ด๋Š” ์ƒ๋žตํ•ด๋„ ์ข‹๋‹ค.

- super()๋ฅผ ์ƒ๋žตํ•˜๋”๋ผ๋„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์•Œ์•„์„œ ํ˜ธ์ถœํ•œ๋‹ค.

- ์ด ๋ฉค๋ฒ„ ์ƒ์„ฑ์ž๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ this๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์ž„์„ ๋ช…์‹œํ•œ๋‹ค.

 

๐Ÿ’ก ๊ธฐ๋ณธ ์ •๋ ฌ ์„ค์ • - Comparable ์ธํ„ฐํŽ˜์ด์Šค์ƒ์† ๋ฐ compareTo ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋“œ ๊ตฌํ˜„

์ •๋ ฌ ์กฐ๊ฑด : ์ด๋ฆ„์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด ๋˜๋„๋ก ์„ค์ •ํ•จ.

compareTo ๋ฉ”์„œ๋“œ ๋‚ด return ๊ฐ’

- this.getName().compareTo(mem.getName()) : ์˜ค๋ฆ„์ฐจ์ˆœ

- this.getName().compareTo(mem.getName()) : ๋‚ด๋ฆผ์ฐจ์ˆœ

class Member implements Comparable<Member>{
	private int 	num;
	private String 	name;
	private String 	tel;
	public int getNum() {
		return num;
	}
	
	public Member() {
		
	}
	
	public Member(int num, String name, String tel) {
		super();
		this.num = num;
		this.name = name;
		this.tel = tel;
	}
	
	@Override
	public int compareTo(Member mem) {
		return this.getName().compareTo(mem.getName());
	}
	
	@Override
	public String toString() {
		return "Member [num=" + num + ", name=" + name + ", tel=" + tel + "]";
	}

	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
}

 

๐Ÿ’ก ๊ธฐ๋ณธ ์ •๋ ฌ ์„ค์ • ํ›„ ํ™•์ธ

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

public class Main {
	public static void main(String[] args) {
		List<Member> memList = new ArrayList<Member>();
		memList.add(new Member(1, "ํ™๊ธธ๋™", "010-1111-1111"));
		memList.add(new Member(4, "๋ณ€ํ•™๋„", "010-2222-2222"));
		memList.add(new Member(6, "์„ฑ์ถ˜ํ–ฅ", "010-3333-3333"));
		memList.add(new Member(3, "์ด์ˆœ์‹ ", "010-4444-4444"));
		memList.add(new Member(2, "๊ฐ•๊ฐ์ฐฌ", "010-5555-5555"));
		memList.add(new Member(5, "์ผ์ง€๋งค", "010-6666-6666"));
		
		System.out.println("์ •๋ ฌ ์ „");
		for(Member mem : memList) {
			System.out.println(mem);
		}
		
		System.out.println("โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– ");
		System.out.println("์ด๋ฆ„ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ");
		Collections.sort(memList);
		for(Member mem : memList) {
			System.out.println(mem);
		}
	}
}

class Member implements Comparable<Member>{
	private int 	num;
	private String 	name;
	private String 	tel;
	public int getNum() {
		return num;
	}
	
	public Member() {
		
	}
	
	public Member(int num, String name, String tel) {
		super();
		this.num = num;
		this.name = name;
		this.tel = tel;
	}
	
	@Override
	public int compareTo(Member mem) {
		return this.getName().compareTo(mem.getName());
	}
	
	@Override
	public String toString() {
		return "Member [num=" + num + ", name=" + name + ", tel=" + tel + "]";
	}

	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
}

๐Ÿ’ก ์ถ”๊ฐ€ ์ •๋ ฌ ์„ค์ • - Comparator ์ธํ„ฐํŽ˜์ด์Šค์ƒ์† ๋ฐ compare ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋“œ ๊ตฌํ˜„

์ •๋ ฌ์กฐ๊ฑด : ํ•™๋ฒˆ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

๋ณ„๋„์˜ ํด๋ž˜์Šค ์ƒ์„ฑ.

class SortNumDesc implements Comparator<Member>{

	@Override
	public int compare(Member mem1, Member mem2) {
		return new Integer(mem1.getNum()).compareTo(mem2.getNum()) * -1;
	}
}

์ถœ๋ ฅ ํ™•์ธ

package baekjoonProject;

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<Member> memList = new ArrayList<Member>();
		memList.add(new Member(1, "ํ™๊ธธ๋™", "010-1111-1111"));
		memList.add(new Member(4, "๋ณ€ํ•™๋„", "010-2222-2222"));
		memList.add(new Member(6, "์„ฑ์ถ˜ํ–ฅ", "010-3333-3333"));
		memList.add(new Member(3, "์ด์ˆœ์‹ ", "010-4444-4444"));
		memList.add(new Member(2, "๊ฐ•๊ฐ์ฐฌ", "010-5555-5555"));
		memList.add(new Member(5, "์ผ์ง€๋งค", "010-6666-6666"));
		
		System.out.println("์ •๋ ฌ ์ „");
		for(Member mem : memList) {
			System.out.println(mem);
		}
		
		System.out.println("โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– ");
		System.out.println("์ด๋ฆ„ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ");
		Collections.sort(memList);
		for(Member mem : memList) {
			System.out.println(mem);
		}
		
		System.out.println("โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– ");
		System.out.println("ํ•™๋ฒˆ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ");
		Collections.sort(memList, new SortNumDesc());
		for(Member mem : memList) {
			System.out.println(mem);
		}
	}
}

class SortNumDesc implements Comparator<Member>{

	@Override
	public int compare(Member mem1, Member mem2) {
		return new Integer(mem1.getNum()).compareTo(mem2.getNum()) * -1;
	}
}

class Member implements Comparable<Member>{
	private int 	num;
	private String 	name;
	private String 	tel;
	
	
	public Member() {
		
	}
	
	public Member(int num, String name, String tel) {
		super();
		this.num = num;
		this.name = name;
		this.tel = tel;
	}
	
	@Override
	public int compareTo(Member mem) {
		return this.getName().compareTo(mem.getName());
	}
	
	@Override
	public String toString() {
		return "Member [num=" + num + ", name=" + name + ", tel=" + tel + "]";
	}
    
    public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
}

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

Set  (0) 2021.03.07
List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (3)  (0) 2021.03.06
List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (1)  (0) 2021.03.06
ArrayList  (0) 2021.03.06
Stack๊ณผ Queue  (0) 2021.03.06