[TIL] 2024.06.24.

limlim·2024년 6월 24일
0

TIL

목록 보기
5/27

TIL 다섯째날, 오늘도 새로 학습한 내용을 적어보자.
오늘은 평소보다 습득한 내용이 많았다.

습득한 지식 및 내용

  • 정렬 (Array.sort() vs Comparable과 Comparator

    : 이전에 코테 연습할 때 정렬을 사용하긴 했었는데 그때는Array.sort()만 사용했었다. 이번에 프로젝트하면서 Comparable과 Comparator를 처음 사용해봤다.

  1. Array.sort()
    : 배열 내에서 Primitive(원시형) 타입을 정렬하기 위해 사용함
    : 기본 정렬은 오름차순임

  2. Comparable
    : 정렬하고자 하는 객체의 클래스에 Comparable 인터페이스를 구현하는 방법임

    class Person implements Comparable<Person> {
       String name;
       int weight;
       int height;
    
       public Person(String name, int height, int weight) {
           this.name = name;
           this.weight = weight;
           this.height = height;
       }
    
       public int getWeight() {
           return weight;
       }
    
       public int getHeight() {
           return height;
       }
    
       @Override
       public int compareTo(Person p) {
           return this.height - p.height; // 오름차순 정렬
       }
    }
    // 1순위: height를 기준으로 오름차순 정렬
    // 2순위: weight를 기준으로 내림차순 정렬
    class Person implements Comparable<Person> {
       String name;
       int weight;
       int height;
    
       public Person(String name, int height, int weight) {
           this.name = name;
           this.weight = weight;
           this.height = height;
       }
    
       public int getWeight() {
           return weight;
       }
    
       public int getHeight() {
           return height;
       }
    
       @Override
       public int compareTo(Person p) {
           if (this.height > p.height) return 1;
           else if (this.height == p.height) { // height가 같다면
               if (this.weight < p.weight) return 1; // weight를 내림차순으로
           }
           return -1;
       }
    }
  3. Comparator
    : 정렬하고자 하는 객체의 클래스에 Comparable 인터페이스를 구현할 수 없을때 사용하는 방법임
    : 또는 Comprable 인터페이스를 통해 이미 정렬기준이 정해져있지만 다른 기준으로 정렬하고 싶을때 사용하는 방법임

Collections.sort(list, new Comparator<Person>() {
    @Override
    public int compare(Person o1, Person o2) {
    	// 오름차순
        return o1.height - o2.height;
    }
});
// 정렬 기준이 2개 이상일 때
Collections.sort(list, new Comparator<Person>() {
    @Override
    public int compare(Person o1, Person o2) {
    	// height 기준 오름차순
        if (o1.height > o2.height) return 1;
        else if (o1.height == o2.height) {
        	// height가 같다면 weight 기준으로 내림차순
            if (o1.weight < o2.weight) return 1;
        }
        return -1;
    }
 });
 // 람다함수로 표현할 때
 Collections.sort(list, (a, b) -> a.getHeight() - b.getHeight());
// stream으로 표현할 때
list.sort(Comparator.comparing(Person::getHeight));
// stream으로 2가지 정렬 기준 표현할 때
list.sort(Comparator.comparing(Person::getHeight).thenComparing(Person::getWeight));
// stream으로 2가지 정렬 기준 표현할 때
list.sort(Comparator.comparing(Person::getHeight).thenComparing(Person::getWeight).reversed());

// But, 위 같은 경우 두 기준 다 내림차순 정렬이 되버림
// 두번째 기준만 내림차순 하고 싶을 때
Comparator<Person> reverseCompare = Comparator.comparing(Person::getWeight).reversed();
list.sort(Comparator.comparing(Person::getHeight).thenComparing(reverseCompare));

cf) 참고 블로그: https://llshl.tistory.com/m/74


  • 오라클 DB링크

    : 예전에 들어봤긴 했지만 정확히 무슨 개념인지 잘 몰랐다. 이번에 프로젝트하면서 개념에 대해 이해할 수 있었다.
    : 한 DB에서 네트워크 상의 다른 DB에 접속하기 위한 설정을 해주는 오라클 객체 (다른 DB에 접속할 수 있는 링크)
    : DB 링크 설정을 하면 한 DB에서 다른 DB의 내용을 볼 수 있음
    : DML문에서 SELECT * FORM 테이블명@링크명; 형식으로 사용하여, 다른 DB에 쿼리를 날릴 수 있음
    : 다른 DB에 특정 유저에 대한 링크를 만들어 해당 스키마에 테이블들을 접근하는 기술


  • 리스트 빈값 확인
// 리스트 선언
List list = new ArrayList();

// 빈값 확인
if(list.isEmpty()) {
}

  • 날짜 정렬 : String 타입으로 날짜를 받아올 때 날짜가 정상적으로 정렬이 되길래 정렬이 제대로 된다고 생각했다. But, 확인해보니 문자열 비교는 문자열의 각 문자를 유니코드 순서대로 비교해 정렬이 됐던 것이다.
    ex) "24/06/03" "24/05/01"보다 유니코드 값이 더 큼
    : But, 이 방법은 문자열이 항상 유효한 날짜 형식으로 되어 있다는 가정이 있을 때만 제대로 작동할 수 있기 때문에 정확하지 않음
    : 그렇기 때문에 날짜로 변환해서 정렬해주는게 가장 정확!
profile
不怕慢,只怕站 개발자

0개의 댓글