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

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

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

BackEnd/Java

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

ejyoovV 2021. 3. 6. 17:54

๐Ÿ’ก ์‚ฌ์šฉ์ž ์ปค์Šคํ…€ ํด๋ž˜์Šค (VO)๊ฐ์ฒด ์ƒ์„ฑ

ํ•™์ƒ ํด๋ž˜์Šค (ํ•™๋ฒˆ, ์ด๋ฆ„, ๊ตญ์–ด์ ์ˆ˜, ์ˆ˜ํ•™์ ์ˆ˜, ์˜์–ด์ ์ˆ˜, ์ด์ , ์„์ฐจ)

ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ์ž๋ฃŒ ์ถœ๋ ฅ

- ์ƒ์„ฑ์ž ์ƒ์„ฑ

- getter setter ์ƒ์„ฑ

- ์„์ฐจ๋ฅผ ๊ตฌํ•  ๋ฉ”์„œ๋“œ ์ƒ์„ฑ

- toString ์˜ค๋ฒ„๋ผ์ด๋“œ

package baekjoonProject;

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

public class Main {
	public static void main(String[] args) {
		List<Student> students = new ArrayList<Student>();
		
		students.add(new Student("201263346", "ํ™๊ธธ๋™", 75,65,52));
		students.add(new Student("201263344", "๋ณ€ํ•™๋„", 70,60,20));
		students.add(new Student("201263343", "๊ฐ•๊ฐ์ฐฌ", 75,65,52));
		students.add(new Student("201263341", "์„ฑ์ถ˜ํ–ฅ", 10,95,83));
		students.add(new Student("201263342", "์ด์ˆœ์‹ ", 75,65,52));
		students.add(new Student("201263345", "์ผ์ง€๋งค", 75,65,52));
		
		Student.setRanked(students);	
		
		System.out.println("ํ•™์ƒ ๋ชฉ๋ก ์ •๋ ฌ ์ „");
		for(Student stu : students) {
			System.out.println(stu);
		}
	}
}


class Student {
	private String stuNum;
	private String stuName;
	private int kor;
	private int eng;
	private int mat;
	private int subSum;
	private int stuRank;
	
	Student(){
		
	}

	public Student(String stuNum, String stuName, int kor, int eng, int mat) {
//		super(); ์ƒ์„ฑ์ž๋Š” Object๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฒƒ์ด ์—†์–ด๋„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์•Œ์•„์„œ ์ƒ์„ฑํ•˜์—ฌ ํ˜ธ์ถœํ•œ๋‹ค.
		this.stuNum = stuNum;
		this.stuName = stuName;
		this.kor = kor;
		this.eng = eng;
		this.mat = mat;
		this.subSum = kor + eng + mat;
		this.stuRank = 1;
	}

	public String getStuNum() {
		return stuNum;
	}

	public void setStuNum(String stuNum) {
		this.stuNum = stuNum;
	}

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public int getKor() {
		return kor;
	}

	public void setKor(int kor) {
		this.kor = kor;
	}

	public int getEng() {
		return eng;
	}

	public void setEng(int eng) {
		this.eng = eng;
	}

	public int getMat() {
		return mat;
	}

	public void setMat(int mat) {
		this.mat = mat;
	}

	public int getSubSum() {
		return subSum;
	}

	public void setSubSum(int subSum) {
		this.subSum = subSum;
	}

	public int getStuRank() {
		return stuRank;
	}

	public void setStuRank(int stuRank) {
		this.stuRank = stuRank;
	}
	
	public static void setRanked(List<Student> students) {
		for(Student st1 : students) {
			int rank = st1.stuRank;
			for(Student st2 : students) {
				if(st1.getSubSum() < st2.getSubSum()) {
					rank++;
				}
			}
			st1.setStuRank(rank);
		}
	}

	@Override
	public String toString() {
		return "Student [stuNum=" + stuNum + ", stuName=" + stuName + ", kor=" + kor + ", eng=" + eng + ", mat=" + mat
				+ ", subSum=" + subSum + ", stuRank=" + stuRank + "]";
	}
	
	
}

๐Ÿ’ก ํ•™์ƒ ํ•™๋ฒˆ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ์„ค์ • (Comparable - compareTo() ๋ฉ”์„œ๋“œ ๊ตฌํ˜„)

- ์ ์šฉํ•  ๊ฐ์ฒด implements

- compareTo() ๋ฉ”์„œ๋“œ ๊ตฌํ˜„

- ํ•™๋ฒˆ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ์ •๋ ฌ ๋ฆฌํ„ด ์ž…๋ ฅ

package baekjoonProject;

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

public class Main {
	public static void main(String[] args) {
		List<Student> students = new ArrayList<Student>();
		
		students.add(new Student("201263346", "ํ™๊ธธ๋™", 75,65,52));
		students.add(new Student("201263344", "๋ณ€ํ•™๋„", 70,60,20));
		students.add(new Student("201263343", "๊ฐ•๊ฐ์ฐฌ", 75,65,52));
		students.add(new Student("201263341", "์„ฑ์ถ˜ํ–ฅ", 10,95,83));
		students.add(new Student("201263342", "์ด์ˆœ์‹ ", 75,65,52));
		students.add(new Student("201263345", "์ผ์ง€๋งค", 75,65,52));
		
		Student.setRanked(students);	
		
		System.out.println("ํ•™์ƒ ๋ชฉ๋ก ์ •๋ ฌ ์ „");
		for(Student stu : students) {
			System.out.println(stu);
		}
		System.out.println("โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– ");
		System.out.println("ํ•™๋ฒˆ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ํ›„");
		Collections.sort(students);
		
		for(Student stu : students) {
			System.out.println(stu);
		}
	}
}


class Student implements Comparable<Student>{
	private String stuNum;
	private String stuName;
	private int kor;
	private int eng;
	private int mat;
	private int subSum;
	private int stuRank;
	
	Student(){
		
	}

	public Student(String stuNum, String stuName, int kor, int eng, int mat) {
//		super(); ์ƒ์„ฑ์ž๋Š” Object๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฒƒ์ด ์—†์–ด๋„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์•Œ์•„์„œ ์ƒ์„ฑํ•˜์—ฌ ํ˜ธ์ถœํ•œ๋‹ค.
		this.stuNum = stuNum;
		this.stuName = stuName;
		this.kor = kor;
		this.eng = eng;
		this.mat = mat;
		this.subSum = kor + eng + mat;
		this.stuRank = 1;
	}

	public String getStuNum() {
		return stuNum;
	}

	public void setStuNum(String stuNum) {
		this.stuNum = stuNum;
	}

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public int getKor() {
		return kor;
	}

	public void setKor(int kor) {
		this.kor = kor;
	}

	public int getEng() {
		return eng;
	}

	public void setEng(int eng) {
		this.eng = eng;
	}

	public int getMat() {
		return mat;
	}

	public void setMat(int mat) {
		this.mat = mat;
	}

	public int getSubSum() {
		return subSum;
	}

	public void setSubSum(int subSum) {
		this.subSum = subSum;
	}

	public int getStuRank() {
		return stuRank;
	}

	public void setStuRank(int stuRank) {
		this.stuRank = stuRank;
	}
	
	public static void setRanked(List<Student> students) {
		for(Student st1 : students) {
			int rank = st1.stuRank;
			for(Student st2 : students) {
				if(st1.getSubSum() < st2.getSubSum()) {
					rank++;
				}
			}
			st1.setStuRank(rank);
		}
	}

	@Override
	public String toString() {
		return "Student [stuNum=" + stuNum + ", stuName=" + stuName + ", kor=" + kor + ", eng=" + eng + ", mat=" + mat
				+ ", subSum=" + subSum + ", stuRank=" + stuRank + "]";
	}

	@Override
	public int compareTo(Student stu) {
		return this.getStuNum().compareTo(stu.getStuNum());
	}
	
	
}

๐Ÿ’ก ํ•™์ƒ ์ด์  ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ถ”๊ฐ€ ์ •๋ ฌ ์„ค์ • (Compator - compare() ๋ฉ”์„œ๋“œ ๊ตฌํ˜„)

- ์ด์ ์ด ๊ฐ™์€ ๊ฒฝ์šฐ ํ•™๋ฒˆ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ๊ทธ๊ฒŒ์•„๋‹ˆ๋ฉด ์ด์ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

- ์ƒˆ๋กœ์šด ๊ฐ์ฒด ์ƒ์„ฑ

- ์กฐ๊ฑด ์„ค์ •(๊ฐ™์€๊ฒฝ์šฐ) ์•„๋ž˜ ์ฐธ๊ณ 

๋‚ด๋ฆผ์ฐจ์ˆœ : -1 (์Œ์ˆ˜)
์˜ค๋ฆ„์ฐจ์ˆœ : 1 (์–‘์ˆ˜)
๋ณ€ํ™”์—†์Œ : 0

if(mem1.getNum() > mem2.getNum()) {
	return -1;//๊ทœ์น™์ž„ (์Œ์ˆ˜)
}else if(mem1.getNum() == mem2.getNum()) {
	return 0;//๊ทœ์น™์ž„ ( ๊ฐ™์Œ)
}else {
	return 1;//๊ทœ์น™์ž„ (์–‘์ˆ˜)
}

๐Ÿ“ ์ถ”๊ฐ€ ์ •๋ ฌ ๊ทœ์น™์— ๋Œ€ํ•œ ํด๋ž˜์Šค ์ƒ์„ฑ

String ์—๋Š” ์ •๋ ฌ ๋ฉ”์„œ๋“œ compareTo ๊ฐ€ ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ. 

Integer ์—๋Š” ์ •๋ ฌ ๋ฉ”์„œ๋“œ compare ๊ฐ€ ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ.

๋‘˜๋‹ค ๋‚ด๋ฆผ์ฐจ์ˆœ์ด๋ฏ€๋กœ ๊ฒฐ๊ณผ ๋’ค์— -1์„ ๊ณฑํ•จ. ์ธ์ž๊ฐ’์€ ๋ฉ”์„œ๋“œ์— ๋งž๊ฒŒ ๋„˜๊ฒจ์คŒ.

class SortSubSum implements Comparator<Student>{

	@Override
	public int compare(Student stu1, Student stu2) {
		if(stu1.getSubSum() == stu2.getSubSum()) {
			return stu1.getStuNum().compareTo(stu2.getStuName()) * -1;
		}else {
			return Integer.compare(stu1.getSubSum(), stu2.getSubSum()) * -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<Student> students = new ArrayList<Student>();
		
		students.add(new Student("201263346", "ํ™๊ธธ๋™", 75,65,52));
		students.add(new Student("201263344", "๋ณ€ํ•™๋„", 70,60,20));
		students.add(new Student("201263343", "๊ฐ•๊ฐ์ฐฌ", 75,65,52));
		students.add(new Student("201263341", "์„ฑ์ถ˜ํ–ฅ", 10,95,83));
		students.add(new Student("201263342", "์ด์ˆœ์‹ ", 75,65,52));
		students.add(new Student("201263345", "์ผ์ง€๋งค", 75,65,52));
		
		Student.setRanked(students);	
		
		System.out.println("ํ•™์ƒ ๋ชฉ๋ก ์ •๋ ฌ ์ „");
		for(Student stu : students) {
			System.out.println(stu);
		}
		System.out.println("โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– ");
		System.out.println("ํ•™๋ฒˆ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ํ›„");
		Collections.sort(students);
		for(Student stu : students) {
			System.out.println(stu);
		}
		System.out.println("โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– โ– ");
		System.out.println("์ด์  ๋‚ด๋ฆผ์ฐจ์ˆœ, ๋‹จ ์ด์ ์ด ๊ฐ™์„ ๋•Œ ์ด์  ๋‚ด๋ฆผ์ฐจ์ˆœ");
		Collections.sort(students, new SortSubSum());
		for(Student stu : students) {
			System.out.println(stu);
		}
	}
}


class Student implements Comparable<Student>{
	private String stuNum;
	private String stuName;
	private int kor;
	private int eng;
	private int mat;
	private int subSum;
	private int stuRank;
	
	Student(){
		
	}

	public Student(String stuNum, String stuName, int kor, int eng, int mat) {
//		super(); ์ƒ์„ฑ์ž๋Š” Object๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฒƒ์ด ์—†์–ด๋„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์•Œ์•„์„œ ์ƒ์„ฑํ•˜์—ฌ ํ˜ธ์ถœํ•œ๋‹ค.
		this.stuNum = stuNum;
		this.stuName = stuName;
		this.kor = kor;
		this.eng = eng;
		this.mat = mat;
		this.subSum = kor + eng + mat;
		this.stuRank = 1;
	}

	public String getStuNum() {
		return stuNum;
	}

	public void setStuNum(String stuNum) {
		this.stuNum = stuNum;
	}

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public int getKor() {
		return kor;
	}

	public void setKor(int kor) {
		this.kor = kor;
	}

	public int getEng() {
		return eng;
	}

	public void setEng(int eng) {
		this.eng = eng;
	}

	public int getMat() {
		return mat;
	}

	public void setMat(int mat) {
		this.mat = mat;
	}

	public int getSubSum() {
		return subSum;
	}

	public void setSubSum(int subSum) {
		this.subSum = subSum;
	}

	public int getStuRank() {
		return stuRank;
	}

	public void setStuRank(int stuRank) {
		this.stuRank = stuRank;
	}
	
	public static void setRanked(List<Student> students) {
		for(Student st1 : students) {
			int rank = st1.stuRank;
			for(Student st2 : students) {
				if(st1.getSubSum() < st2.getSubSum()) {
					rank++;
				}
			}
			st1.setStuRank(rank);
		}
	}

	@Override
	public String toString() {
		return "Student [stuNum=" + stuNum + ", stuName=" + stuName + ", kor=" + kor + ", eng=" + eng + ", mat=" + mat
				+ ", subSum=" + subSum + ", stuRank=" + stuRank + "]";
	}

	@Override
	public int compareTo(Student stu) {
		return this.getStuNum().compareTo(stu.getStuNum());
	}
}

class SortSubSum implements Comparator<Student>{

	@Override
	public int compare(Student stu1, Student stu2) {
		if(stu1.getSubSum() == stu2.getSubSum()) {
			return stu1.getStuNum().compareTo(stu2.getStuName()) * -1;
		}else {
			return Integer.compare(stu1.getSubSum(), stu2.getSubSum()) * -1;
		}
	}
	
}

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

TreeSet  (0) 2021.03.07
Set  (0) 2021.03.07
List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (2)  (0) 2021.03.06
List์˜ ์ •๋ ฌ(Sort) - interface : Comparable, Comparator (1)  (0) 2021.03.06
ArrayList  (0) 2021.03.06