GiantStepDEV

๋ฌธ์ œ

์ž„์˜์˜ ์œ„์น˜์— ์•„๋ž˜ ๋‚ด์šฉ์ด ๋‹ด๊ธด ํ…์ŠคํŠธ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ ,

ํ•ด๋‹น ํŒŒ์ผ์„ ์ฝ์–ด ์ด์ ์„ ๊ตฌํ•˜๊ณ  ์ด์ ์ด ๋†’์€ ์‚ฌ๋žŒ ์ˆœ์œผ๋กœ ์ด๋ฆ„๊ณผ ์ด์  ์ถœ๋ ฅํ•˜๊ธฐ

(๋‹จ, ์ด์ ์ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์ •๋ ฌ)

.txt

์•ˆ์œ ์ง„ 99 78 45
์ด์˜์ง€ 34 56 100
์ด์œค์ง€ 56 78 34
์šฐ์˜์šฐ 99 98 97
์ •๋ช…์„ 96 99 98
์ด์ค€ํ˜ธ 77 87 88
๊ถŒ๋ฏผ์šฐ 96 93 90
์ตœ์ˆ˜์—ฐ 97 88 87
๋™๊ทธ๋ผ๋ฏธ 45 34 67
๋ฏธ๋ฏธ 45 56 78


๐Ÿธ : ๋ฌธ์ œ๋ฅผ ํŒŒ์•…ํ•œ ๋’ค์—๋Š” ํ•ด๋‹น ์ฒ˜๋ฆฌ ์ˆœ์„œ๋ฅผ ๋จผ์ € ์ •ํ•ด๋‘๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

(1) ํŒŒ์ผ์„ ์ฝ์–ด์˜จ๋‹ค.

โ“Scanner

(2) ์ •๋ณด๋ฅผ ๋‹จ์œ„๋ณ„๋กœ ์ชผ๊ฐ ๋‹ค.

โ“nextLine() : ์ค„๋ฐ”๊ฟˆ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ” , split() : ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ๋  ๊ฒƒ ๊ฐ™์Œ

(3) ๊ทธ ์ •๋ณด๋ฅผ ๊ฐ์ฒด์— ๋‹ด๋Š”๋‹ค.

โ“TreeSet : ์ž๋™์ •๋ ฌ ํ•˜์ง€๋งŒ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— Comparable ์ƒ์† ํ›„ ์ •๋ ฌ์กฐ๊ฑด ๋ฐ˜๋“œ์‹œ ํ•ด์ค˜์•ผ ํ•จ.


 ๐Ÿถ : ๊ฐœ๊ตด. ์งˆ๋ฌธ์ด ์žˆ๋‹ค. ๋‹ด์„ ๋•Œ HashSet์„ ์จ๋„ ๋˜๋‚˜?

 ๐Ÿธ : ์•ˆ๋ผ. ์ด์œ ๋Š” ์•„๋ž˜ ์ž์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ์ง€.


(4) ์ •๋ ฌํ•œ๋‹ค.

โ“Comparable

(5) ์ถœ๋ ฅํ•œ๋‹ค.

โ“ํ–ฅ์ƒ๋œ for๋ฌธ


๐Ÿธ : HashSet๊ณผ TreeSet์˜ ๊ธฐ๋ณธ์ ์ธ ์ •๋ณด๋Š” Java ๊ฒŒ์‹œํŒ์— ์ •๋ฆฌํ•ด๋‘์—ˆ์œผ๋‹ˆ ์•Œ๊ฑฐ๋ผ ์ƒ๊ฐํ•˜๊ณ .. ์ฐจ์ด์ ๋งŒ ๋งํ•ด์ฃผ๊ฒ ๋‹ค.

 

TreeSet : The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

HashSet : It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

์œ„ ์„ค๋ช…๊ณผ ๊ฐ™์ด TreeSet์€ ์ƒ์„ฑ๋˜๋Š” ๋™์‹œ์— ์ž๋™์ •๋ ฌ์ด ๋œ๋‹ค. ํ˜น์€ Comparator(Comparable)์—์„œ ์ง€์ •ํ•œ ์ˆœ์„œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฐ˜๋ฉด์— HashSet์€ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— Comparator๋ฅผ ์ด์šฉํ•œ๋‹ค ํ•ด๋„ ์ •๋ ฌ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.

๋”ฐ๋ผ์„œ, ์ด ๋ฌธ์ œ์—์„œ๋Š” ๋ฐ˜๋“œ์‹œ TreeSet์„ ์‚ฌ์šฉํ•ด์•ผ ์›ํ•˜๋Š”๋Œ€๋กœ ์ถœ๋ ฅํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        TreeSet<Student> hs = new TreeSet<>();
        FileInputStream fis = new FileInputStream("C:/Dev/PracticeJava/src/์ž…์ถœ๋ ฅ์ŠคํŠธ๋ฆผ๋ฌธ์ œ/score.txt");
        Scanner sc = new Scanner(fis);
        while(sc.hasNext()) {
            String line = sc.nextLine();
            String[] strArr = line.split(" ");
            hs.add(new Student(strArr[0], Integer.parseInt(strArr[1]), Integer.parseInt(strArr[2]),Integer.parseInt(strArr[3])));
        }
        for(Student e : hs) {
            System.out.println("์ด๋ฆ„ : " + e.getName());
            System.out.println("์ด์  : " + e.getTotalScore());
            System.out.println("=============================");
        }
    }
}
public class Student implements Comparable<Student>{
    String name;
    int korScore;
    int engScore;
    int mathScore;

    public Student(String name, int korScore, int engScore, int i) {
        this.name = name;
        this.korScore = korScore;
        this.engScore = engScore;
        this.mathScore = mathScore;
    }

    public String getName() {
        return name;
    }

    public int getTotalScore() {
        return korScore + engScore + mathScore;
    }


    @Override
    public int compareTo(Student o) {
        if(this.getTotalScore() == o.getTotalScore()) return this.name.compareTo(o.name);
        return o.getTotalScore() - this.getTotalScore();
    }
}
์ด๋ฆ„ : ์šฐ์˜์šฐ
์ด์  : 197
=============================
์ด๋ฆ„ : ์ •๋ช…์„
์ด์  : 195
=============================
์ด๋ฆ„ : ๊ถŒ๋ฏผ์šฐ
์ด์  : 189
=============================
์ด๋ฆ„ : ์ตœ์ˆ˜์—ฐ
์ด์  : 185
=============================
์ด๋ฆ„ : ์•ˆ์œ ์ง„
์ด์  : 177
=============================
์ด๋ฆ„ : ์ด์ค€ํ˜ธ
์ด์  : 164
=============================
์ด๋ฆ„ : ์ด์œค์ง€
์ด์  : 134
=============================
์ด๋ฆ„ : ๋ฏธ๋ฏธ
์ด์  : 101
=============================
์ด๋ฆ„ : ์ด์˜์ง€
์ด์  : 90
=============================
์ด๋ฆ„ : ๋™๊ทธ๋ผ๋ฏธ
์ด์  : 79
=============================
profile

GiantStepDEV

@kongmi

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!