Interface
Marker Interface
λ§μ»€ μΈν°νμ΄μ€μ νΉμ§μ λ€μκ³Ό κ°λ€.
- λ©μλ μ μΈμ΄ μλ μΈν°νμ΄μ€μ΄λ€.
- λνμ μΌλ‘λ Serializableκ³ΌCloneableμ΄ μλ€.
μ΄λ Έν μ΄μ μΌλ‘λ μ¬μ©κ°λ₯νλ€.
Cloneable
ν΄λμ€κ° Cloneable μΈν°νμ΄μ€λ₯Ό ꡬνν¨μΌλ‘μ¨ Object.clone() λ©μλκ° ν΄λΉ ν΄λμ€μ μΈμ€ν΄μ€μ λν νλ 볡μ¬λ³Έμ λ§λ€μ μμμ λνλΈλ€.
Cloneable μΈν°νμ΄μ€μλ clone λ©μλκ° ν¬ν¨λμ΄ μμ§ μμΌλ©° clone μΈν°νμ΄μ€λ₯Ό ꡬννλ€λ μ¬μ€λ§μΌλ‘λ κ°μ²΄μ 볡μ¬λ₯Ό 보μ₯ν  μ μλ€.
μ΄λ Object ν΄λμ€μ μ μλμ΄ μλ μλ λ©μλλ₯Ό public μΌλ‘ μ¬μ μ νμ¬ μ¬μ©ν΄μΌ νλ€.
protected native Object clone() throws CloneNotSupportedException;
μλ³Έ Object ν΄λμ€μ clone() λ©μλλ protected λ©μλμ΄κΈ° λλ¬Έμ μΈλΆ ν¨ν€μ§ ν΄λμ€μμ νΈμΆν  μ μλ€.
λ°°μ΄μ `clone` λ©μλ
μ΄μ μμΈμ μΌλ‘ λ°°μ΄μ clone μ λ°νμ νμ
κ³Ό μ»΄νμΌμ νμ
 λͺ¨λκ° μλ³Έ λ°°μ΄κ³Ό λμΌν λ°°μ΄μ λ°ννλ€.
μ¬μ€μ clone λ©μλλ₯Ό μλ²½νκ² μ¬μ©νλ μ μΌν μ μ΄λ€.
Map
Spac
Interface Map <K, V>
Type Parameters
- K
- ν€μ μ ν
 
- V
- κ°μ μ ν
 
Implementing Classes
HashMap HashTable EnumMap ConcurrentHashMap TreeMap WeakHashMap
Since
ν€λ₯Ό κ°μ λ§€ννλ κ°μ²΄ μ λλ€.
μ€λ³΅λ ν€λ₯Ό κ°μ λ§€νν μ μμΌλ©° κ° ν€λ μ΅λ νκ°μ κ°μλ§ λ§€νμ΄ κ°λ₯ ν©λλ€.
Map μΈν°νμ΄μ€λ μΈκ°μ§μ 컬λ μ
 λ·°λ₯Ό μ κ³΅ν©λλ€. (ν€ μ§ν©, κ°μ μ§ν©, ν€ & κ°μ μ§ν©)
TreeMap ν΄λμ€μ κ°μ μΌλΆ Map μΈν°νμ΄μ€μ ꡬν체λ€μ μμλ₯Ό 보μ₯νλ€.
AutoCloseable
Since
ν₯μΉλ μμΈμ²λ¦¬λ¬ΈμΌλ‘ μ
μΆλ ₯ μ²λ¦¬μ μμΈκ° λ°μνλ©΄ JVM μ΄ μλμΌλ‘ close λ©μλλ₯Ό νΈμΆνμ¬ μμμ λ°λ©μμΌ μ€λλ€.
νμΌ λλ μμΌ νΈλ€ λ±μ μμλ€μ μ’
λ£ν  λκΉμ§ 보κ΄νλ κ°μ²΄μ΄λ€.
AutoCloseable κ°μ²΄μ close λ©μλλ try-with-resources λΈλμ μ’
λ£ν  λ μλμΌλ‘ νΈμΆ λλ€.
μ΄ κ΅¬μ‘°λ 리μμ€μ κ³ κ° λ° λ€λ₯Έ μμΈλ€κΉμ§ λ°μν μ μλ μλ¬λ€μ ν΄μλ₯Ό μ¦κ°μ μΌλ‘ 보μ₯νλ€.
μ΄λ try() ꡬ문μμλ AutoCloseable μΈν°νμ΄μ€λ₯Ό ꡬνν κ°μ²΄μ¬μΌ νλ€.
try-finally 보λ€λ try-with-resources λ₯Ό μ¬μ©
public class MyResource implements AutoCloseable {
  public void close() throws Exception {
    System.out.println("Closeing!");
  }
}
try (MyResource res = new MyResource()){
  // use the code
}
Comparable
μ λ ¬ μνμ κΈ°λ³Έμ μΌλ‘ μ μ©μ΄ λλ μ λ ¬κΈ°μ€ λ©μλλ₯Ό μ μνλ μΈν°νμ΄μ€
Comparable μΈν°νμ΄μ€λ₯Ό ꡬνμμ compareTo() λ₯Ό μ€λ²λΌμ΄λ νμ¬ κ΅¬ννλ€.
compareTo() λ©μλμ μμ± κ·μΉμ λ€μκ³Ό κ°λ€.
- <: μμ λ°ν
- >: μμ λ°ν
- ==: 0 λ°ν
μ΄ν Arrays.sort() νΉμ Collections.sort() λ₯Ό ν΅νμ¬ μ λ ¬
// xμ’νκ° μ¦κ°νλ μ, xμ’νκ° κ°μΌλ©΄ yμ’νκ° κ°μνλ μμΌλ‘ μ λ ¬νλΌ.
class Point implements Comparable<Point> {
    int x, y;
    @Override
    public int compareTo(Point p) {
        if(this.x > p.x) {
            return 1; // xμ λν΄μλ μ€λ¦μ°¨μ
        }
        else if(this.x == p.x) {
            if(this.y < p.y) { // yμ λν΄μλ λ΄λ¦Όμ°¨μ
                return 1;
            }
        }
        return -1;
    }
}
List<Point> pointList = new ArrayList<>();
pointList.add(new Point(x, y));
Collections.sort(pointList);
Default Method μ§μJava SE 8+
Interface μμλ λ©μλ ꡬνμ΄ κ°λ₯νκ² λμλ€.
μ°Έμ‘° λ³μλ‘ ν¨μλ₯Ό νΈμΆν μ μλ€.
implements ν ν΄λμ€μμ μ¬μ μκ° κ°λ₯νλ€.
interface MyInterface {
  default void defaultMethod() {
    System.out.println("Default Method");
  };
}
Static Method μ§μJava SE 8+
Interface μμλ λ©μλ ꡬνμ΄ κ°λ₯νλ€.
λ°λμ ν΄λμ€ λͺ μΌλ‘ λ©μλλ₯Ό νΈμΆν΄μΌ νλ€. (static μ±κ²©μ κ°μ§κΈ° λλ¬Έ)
μ¬ μ μκ° λΆκ°λ₯ νλ€.
interface MyInterface {
  static void staticMethod() {
    System.out.println("Static Method");
  };
}
