์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- lazy loading
- Store
- JavaScript
- .net
- Request
- ์์ค๊ฐ๋ ์ค๋
- c#์ฝ๋ฉ์๊ธฐ์ ์ค์ ํธ
- ๋ผ๋๋ฌด์คdvd
- JSON
- EFCore
- extjs
- c#
- ORM
- dbContext
- ๋์ ๋ณธ์์์
- ์ค๋ ์์ฐ์
- LINQ
- scanner
- minimalAPI
- ์ฝ๋ํ๋ก๊ทธ๋๋จธ์ค
- ๋ช ์์ ์ธ๋ํค
- ์์
- error
- React
- ViewModel
- intellij
- vscode
- extraParams
- mac
- Config
- Today
- Total
ejyoo's ๊ฐ๋ฐ ๋ ธํธ
List์ ์ ๋ ฌ(Sort) - interface : Comparable, Comparator (3) ๋ณธ๋ฌธ
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 |