๐Ÿ“–
Mini's Today I Learned
  • Today I Learned
  • ๐Ÿ’ปComputer Science
    • ๐ŸงฑComputer Science
      • Computer Architecture
    • ๐Ÿฆ•OperatingSystem
      • System Structure
      • 01. ์šด์˜์ฒด์ œ ์—ญํ• 
      • 02. History๋กœ ์ดํ•ดํ•˜๋Š” ์šด์˜์ฒด์ œ
      • 03. ์šด์˜์ฒด์ œ ๊ตฌ์กฐ
      • 04. ํ”„๋กœ์„ธ์Šค ์Šค์ผ€์ฅด๋ง
      • 05. ์Šค์ผ€์ฅด๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜
      • 06. ํ”„๋กœ์„ธ์Šค ์ƒํƒœ์™€ ์Šค์ผ€์ฅด๋Ÿฌ
      • 07. ์ธํ„ฐ๋ŸฝํŠธ
      • 08. ํ”„๋กœ์„ธ์Šค์™€ ์ปจํ…์ŠคํŠธ ์Šค์œ„์นญ
      • 09. ํ”„๋กœ์„ธ์Šค๊ฐ„ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜(IPC ๊ธฐ๋ฒ•)
      • 10. Thread(์Šค๋ ˆ๋“œ)
      • 11. ๊ฐ€์ƒ๋ฉ”๋ชจ๋ฆฌ (Virtual Memory System)
      • 12. ํŒŒ์ผ์‹œ์Šคํ…œ ์ดํ•ด
      • 13. ๋ถ€ํŒ…์˜ ์ดํ•ด
      • 14. ๊ฐ€์ƒ๋จธ์‹ (Virtual Machine)
  • ๐Ÿ“ˆDatabase
    • MySQL CLI (Frequently used)
    • 1-Tier, 2Tier, 3Tier
    • Basic SQL
    • Built in function
    • Sub Query
    • View
    • Stored Program
    • index
    • Normalization(์ •๊ทœํ™”)
    • Transaction
    • Transaction
  • ๐ŸŒ Network
    • ๐Ÿ”Œ TCP&IP Basic
      • 01 TCP/IP ๋ง›๋ณด๊ธฐ
      • 02 TCP/IP ๊ฐœ์š”
      • 03 ํ†ต์‹  ์„œ๋น„์Šค์™€ ํ”„๋กœํ† ์ฝœ
      • 04 ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ณ„์ธต
      • 05 ํŠธ๋ Œ์ŠคํฌํŠธ ๊ณ„์ธต
      • 06 ๋„คํŠธ์›Œํฌ๊ณ„์ธต
      • 07 ๋ฐ์ดํ„ฐ ๊ณ„์ธต๊ณผ ๋ฌผ๋ฆฌ๊ณ„์ธต
      • 08 ๋ผ์šฐํŒ…
      • 09 ๋ณด์•ˆ
      • 10 HTTP ํ”„๋กœํ† ์ฝœ
    • ๐Ÿฟ๏ธHTTP ์™„๋ฒฝ ๊ฐ€์ด๋“œ ์š”์•ฝ
      • 1. HTTP: ์›น์˜ ๊ธฐ์ดˆ
      • 2. URL ๊ณผ ๋ฆฌ์†Œ์Šค
      • 3. HTTP Message
      • 4. Connection Management
      • 5. Overview of web server architectures
      • 6. Proxies
      • 7.Caching
      • 8. Integration Points: Gateways, Tunnels, and Relays
      • 9. Web Robots
      • 10. HTTP/2.0
      • 11. Client Identification and Cookies
      • 12. Basic Authentication
      • 13. Digest Authentication
      • 14. Secure HTTP
      • 15. Entities and Encodings
      • 16. Internationalization
      • 17. Content Negotiation and Transcoding
      • 18. Web Hosting
  • ๐ŸดLanguage
    • โ˜•JAVA
      • Comparable vs Comparator
  • ๐Ÿ› ๏ธ Framework
  • ๐ŸงฉDesign Pattern
    • ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„ ์›์น™ (SOLID)
    • Design Pattern ๊ฐœ์š”
    • Template method
    • Singleton
  • ๐ŸฅFrontEnd
    • HTML&CSS
      • 01 Intro
      • 02 HTML Basic
      • 03 Content Section
      • 04 Block and Inline Elements
      • 05 Text Contents
      • 06 Inline Elements
      • 07 Multi Media
      • 08 Table&Form&etc
      • 09 Global Attribute & etc
      • 10 css basic
      • 11 css config
      • 12 css unit
      • 13 Box Model
      • 14 Font & Text
      • 15 Float(๋„์›€) & Position
      • 16 Background
      • 17 Transitions & Transforms
      • 18 Animation & Multi Columns(๋‹ค๋‹จ)
      • 19 Flex
      • 20 Grid
    • Sass(SCSS)
    • TypeScript
    • Webpack Introduction
      • ์›นํŒฉ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ
      • CommonJS
      • ESM
      • WebpackConfig
        • 1. Handlebars
        • 2. Caching
        • 3. Minification & Mangling
        • 4. Mode(Development mode & Production Mode)
        • 5. ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋ชจ๋“ˆ๋กœ ๋‹ค๋ฃจ์–ด๋ณด๊ธฐ(file-loader)
        • 6. ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋ชจ๋“ˆ๋กœ ๋‹ค๋ฃจ์–ด๋ณด๊ธฐ(url-loader)
        • 7. SASS Loader
        • 8. Post CSS
        • 9. BLOWSERS LIST
        • 10. Stylelint
        • 11. Babel
      • Webpack-Practice
    • ETC
      • GIT
      • MarkDown
Powered by GitBook
On this page

Was this helpful?

  1. Language
  2. JAVA

Comparable vs Comparator

PreviousJAVANext๐Ÿ› ๏ธ Framework

Last updated 4 years ago

Was this helpful?

  • Comparable interface

    • ์ •๋ ฌ ๊ธฐ์ค€์„ ์ •์˜ํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜.

    • ์ •๋ ฌ ๋Œ€์ƒ ํด๋ž˜์Šค๋ฅผ ์ž๋ฐ”์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋„๋ก ๋ณ€๊ฒฝ.

    • Comparable ์ธํ„ฐํŽ˜์ด์Šค์˜ compareTo() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ, ์ธ์ž๋กœ ๋„˜์–ด์˜จ ๊ฐ™์€ ํƒ€์ž…์˜ ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ๋Œ€์†Œ ๋น„๊ต๊ฐ€ ๊ฐ€๋Šฅ.

    • return ๊ฐ’์œผ๋กœ Integer.compare(a, b), Double.compare(a,b) ๋“ฑ์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ๋ฐ, Integer.compare ํ•จ์ˆ˜์›ํ˜•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

      public static int compare(int x, int y){
          return (x>y) ? -1 : ((x==y)? 0 : 1);
      }
    • Integer.compare()์€ ๋‹จ์ˆœํžˆ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์˜ค๋ฆ„์ฐจ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋น„๊ตํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

    • ์ •๋ ฌ์ด ์ง„ํ–‰ ๋  ๋•Œ, ์ž๋ฆฌ๋ฐ”๊ฟˆ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ , return ๊ฐ’์ด ์–‘์ˆ˜์ผ ๋Œ€ ์ž๋ฆฌ๋ฐ”๊ฟˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

    • ๋‚ด๋ฆผ์ฐจ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” compare(int y , int x)๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆœ์„œ๋งŒ ๋ฐ”๊ฟ”์ฃผ๋ฉด๋œ๋‹ค.

    • ์˜ˆ์ œ

      //์˜ค๋ฆ„์ฐจ
      class A implements Comparable <A> {
          int x;
          A(int x){
              this.x = x;
          }
          public int compareTo(A other){
              return Integer.compare(this.x, other.x);
          }
      }
    • Integer.compare() ๋Œ€์‹  ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋„ ๋œ๋‹ค.

    • ์˜ˆ์ œ

      //์˜ค๋ฆ„์ฐจ
      class A implements Comparable<A>{
          int x;
          public int compareTo(A other){
              //์–‘์ˆ˜์ผ ๊ฒฝ์šฐ์—๋งŒ ์ž๋ฆฌ ๋ฐ”๊ฟˆ
              return this.x - other.x;
          }
      }
      //๋‚ด๋ฆผ์ฐจ
      class A implements Comparable<A>{
          int x;
          public int compareTo(A other){
              return other.x - this.x;
          }
      }
  • Comparator

    • ์ •๋ ฌ ๋Œ€์ƒ ํด๋ž˜์Šค์˜ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ

    • ํ˜น์€ ์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ์ฒด์— ์ด๋ฏธ ์กด์žฌํ•˜๊ณ ์žˆ๋Š” ์ •๋ ฌ ๊ธฐ์ค€๊ณผ ๋‹ค๋ฅธ ์ •๋ ฌ ๊ธฐ์ค€์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ.

    • Arrays.sort(), Collections.sort()์™€ ๊ฐ™์€ ์ •๋ ฌ ๋ฉ”์„œ๋“œ์˜ ์ถ”๊ฐ€ ์ธ์ž๋กœ ์‚ฌ์šฉํ•ด์„œ ์ •๋ ฌ๊ธฐ์ค€์„ ๋ˆ„๋ฝ๋œ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋‚˜, ๊ธฐ์กด์˜ ์ •๋ ฌ ๊ธฐ์ค€์„ ๋ฌด์‹œํ•˜๊ณ  ์ƒˆ๋กœ์šด ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ ๊ฐ์ฒด ์ •๋ ฌ์ด ๊ฐ€๋Šฅ.

    • ์˜ˆ์‹œ(Programmers, ์—์„œ ๊ฐ€์ ธ์˜ด.)

      Arrays.sort(numStr, new Comparator<String>() {
                  @Override
                  public int compare(String a, String b) {
                      //์—ฌ๊ธฐ์„œ compareTo๋Š” String์ด ๊ฐ€์ง„ ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ
                      return (a+b).compareTo(b+a);
                  }
              });
    • ๊ธฐ๋ณธ ์ •๋ ฌ ๊ธฐ์ค€๊ณผ ๋‹ค๋ฅธ ์ƒˆ๋กœ์šด ์ •๋ ฌ ๊ธฐ์ค€์„ ์„ธ์šธ ๋•Œ ์‚ฌ์šฉ. ์ฃผ๋กœ ์ต๋ช… ํด๋ž˜์Šค.

  • ์ถ”๊ฐ€

    • ๋žŒ๋‹ค์‹์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ (js์—์„œ ํ•จ์ˆ˜ํ˜•์— ์ต์ˆ™ํ•ด์ ธ์„œ, ์ฝ๊ธฐ ํŽธํ–ˆ๋‹ค!!)

    • Arrays.sort(numStr, (a, b) -> (b+a).compareTo(a + b));

์ฐธ์กฐ ๋งํฌ

๐Ÿด
โ˜•
๊ฐ€์žฅ ํฐ ์ˆ˜ ๋ฌธ์ œ
https://m.blog.naver.com/occidere/220918234464
https://www.daleseo.com/java-comparable-comparator/