Comparable

호모루덴스·2020년 3월 12일
0
public class Student implements Comparable<Student> {
    private String grd; // 학년
    private String nm; //  이름
    
    @Override
    public boolean equals(Object obj) {
        Student objStudent = (Student) obj;

        if (StringUtil.equals(this.getGrd(), objStudent.getGrd()) 
        && StringUtil.equals(this.getNm(), objStudent.getNm())) {
            return true;
        }

        return false;
    }
    
    @Override
    public int compareTo(Student o) {
        if (this.getGrd().compareTo(o.getGrd()) > 0) {
            return 1;
        } else if (this.getGrd().compareTo(o.getGrd()) < 0) {
            return -1;
        } else { // 학년이 같은 경우 이름으로 정렬
            if (this.getNm().compareTo(o.getNm()) > 0) return 1;
            if (this.getNm().compareTo(o.getNm()) < 0) return -1;
            return 0;
        }
    }
    // 직관적으로, method 들이 -1,0,1로 값을 받아 정렬할 것으로
public static void main(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student("4", "4홍길동");
        Student student2 = new Student("4", "4김길동");
        Student student3 = new Student("1", "1홍길동");
        Student student4 = new Student("1", "1김길동");
        String std5 = "AAA";
        String std6 = "AAA";
        String std7 = new String("AAA");
        String std8 = new String("AAA");
		
        /* String은 char값의 hash를 저장.
          .hashCode()로 찍으면 모두 같은 값이 나온다
            (문자열이 같다는 의미)
           String은 Immutable이므로 new로 생성할 경우 같은 문자더라도
           다른 메모리가 할당된다
           하지만 literal Type으로 선언하면 std5,6 같은 주소를 가리킨다
       */
        System.out.println(System.identityHashCode(std5)); //366712642
        System.out.println(System.identityHashCode(std6)); //366712642
        System.out.println(System.identityHashCode(std7)); //1829164700
        System.out.println(System.identityHashCode(std8)); //2018699554
        
        System.out.println("~~~~~~~~~~~~~~~~~~");
        
        int cmpr = student1.compareTo(student2);
        if (cmpr > 0) {
            System.out.println("student grd가 studnet2 grd보다 큽니다.");
        } else if (cmpr == 0) {
            System.out.println("student grd가 studnet2 grd 와 똑같습니다.");
        } else if (cmpr < 0) {
            System.out.println("student grd가 studnet2 grd보다 작습니다.");
        }

        ArrayList<Student> list = new ArrayList<Student>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
		
       /* break point를 걸고 debug 하면 (step into) 
        * Student의 compareTo로 이동한다
        */
        Collections.sort(list);
        /* Comparable.java
         * Lists (and arrays) of objects that implement this interface can be sorted
 	 * automatically by {@link Collections#sort(List) Collections.sort} (and
 	 * {@link Arrays#sort(Object[]) Arrays.sort}).
         */
         

        for (Student student_ : list) {
            System.out.println(student_.toString());
        }
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
        for (Student s : list) {
            if (s.getNm().contains("김")) {
                System.out.println(s);
            }
        }
    }
profile
hola 공무원 때려친 코린이입니다

0개의 댓글