λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Java·ο»ΏServlet·ο»ΏJSP

JAVA μ œλ„€λ¦­(Generics) ν΄λž˜μŠ€μ™€ λ©”μ†Œλ“œ

by Leica 2020. 7. 11.
λ°˜μ‘ν˜•

JAVA μ œλ„€λ¦­(Generics) ν΄λž˜μŠ€μ™€ λ©”μ†Œλ“œ

πŸ“ λ‹€λ£¨λŠ” λ‚΄μš©

  • μ œλ„€λ¦­μ˜ μ •μ˜
  • μ œλ„€λ¦­ 클래슀
  • μ œλ„€λ¦­ λ©”μ†Œλ“œ
  • μ œλ„€λ¦­ μ œν•œ(extends, super)
  • μ™€μΌλ“œ μΉ΄λ“œ
  • μ œλ„€λ¦­ 예 - Collections.sort()

 

μ œλ„€λ¦­(Generics) μ΄λž€?

μ œλ„€λ¦­μ€ 클래슀, λ©”μ†Œλ“œμ—μ„œ μ‚¬μš©ν•  데이터 νƒ€μž…μ„ λ‚˜μ€‘μ— ν™•μ •ν•˜λŠ” 기법이닀. λ‚˜μ€‘μ—λΌλŠ” 말은 ν΄λž˜μŠ€λ‚˜ λ©”μ†Œλ“œλ₯Ό μ„ μ–Έν•  λ•Œκ°€ μ•„λ‹Œ μ‚¬μš©ν•  λ•Œ, 즉 μΈμŠ€ν„΄μŠ€λ₯Ό 생성할 λ•Œλ‚˜ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ μ •ν•œλ‹€λŠ” μ˜λ―Έμ΄λ‹€.

μ œλ„€λ¦­μ˜ μ‚¬μš© 방법과 νŠΉμ§•μ€ λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜μ™€ ꡉμž₯히 μœ μ‚¬ν•œλ°, λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜κ°€ 'κ°’'κ³Ό κ΄€λ ¨λ˜μ–΄ μžˆλ‹€λ©΄ μ œλ„€λ¦­μ€ λ°μ΄ν„°μ˜ 'νƒ€μž…'κ³Ό 관련이 μžˆλ‹€.

 

λ”°λΌμ„œ ν΄λž˜μŠ€μ™€ λ©”μ†Œλ“œκ°€ λ‹€μ–‘ν•œ νƒ€μž…μ„ λ‹€λ£° 수 μžˆλŠ”λ° 이λ₯Ό μ œλ„€λ¦­μ„ μ‚¬μš©ν•˜μ§€ μ•Šκ³  κ΅¬ν˜„ν•˜κ³ μž ν•˜λ©΄ 방법은 두 가지이닀.

Object νƒ€μž…μ„ μ‚¬μš©ν•˜λ˜μ§€, λ™μΌν•œ λ©”μ»€λ‹ˆμ¦˜μ„ 가진 ν΄λž˜μŠ€μ™€ λ©”μ†Œλ“œλ₯Ό μ—¬λŸ¬ 개 μ •μ˜ν•˜λ˜μ§€.

일단 Objectλ₯Ό μ‚¬μš©ν•˜λ©΄ μ½”λ“œ 쀑볡은 막을 수 μžˆκ² μ§€λ§Œ 객체의 νƒ€μž…μ„ 컴파일 νƒ€μž„μ— 체크할 수 μ—†λ‹€λŠ” μ œμ•½μ‚¬ν•­μ΄ 생기고 이둜 인해 λŸ°νƒ€μž„μ— νƒ€μž…μœΌλ‘œ μΈν•œ 였λ₯˜κ°€ λ°œμƒν•  수 μžˆλ‹€.

 

μ œλ„€λ¦­μ„ μ‚¬μš©ν•˜λ©΄ Objectλ₯Ό μ‚¬μš©ν•  λ•Œμ™€ 달리 객체의 νƒ€μž…μ„ 컴파일 νƒ€μž„μ— 체크할 수 μžˆμ–΄μ„œ νƒ€μž… μ•ˆμ •μ„±(type safety)λ₯Ό 높이고 ν˜•λ³€ν™˜μ˜ λ²ˆκ±°λ‘œμ›€μ΄ μ€„μ–΄λ“ λ‹€λŠ” μž₯점이 μžˆλ‹€.

 

νƒ€μž… μ•ˆμ •μ„±(type safety)
- μ˜λ„ν•˜μ§€ μ•Šμ€ νƒ€μž…μ˜ 객체가 μ €μž₯λ˜λŠ” 것을 λ§‰λŠ”λ‹€.
- μ €μž₯된 객체λ₯Ό κΊΌλ‚΄μ˜¬ λ•Œ λ‹€λ₯Έ νƒ€μž…μœΌλ‘œ 잘λͺ» ν˜•λ³€ν™˜ν•˜μ—¬ λ°œμƒν•  수 μžˆλŠ” 였λ₯˜λ₯Ό 쀄인닀.

 

μ œλ„€λ¦­μ˜ μž₯점
- νƒ€μž… μ•ˆμ •μ„±
- μ½”λ“œκ°€ 간결해진닀.

 

JAVA의 μ œλ„€λ¦­μ€ C++의 ν…œν”Œλ¦Ώ ν΄λž˜μŠ€μ™€ μœ μ‚¬ν•œ κ°œλ…μ΄λΌκ³  ν•œλ‹€.

 

μ œλ„€λ¦­ 클래슀

μ œλ„€λ¦­μ€ ν΄λž˜μŠ€μ™€ λ©”μ†Œλ“œμ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ”λ° μ œλ„€λ¦­ νƒ€μž…μ„ μ„ μ–Έν•œ 클래슀λ₯Ό μ œλ„€λ¦­ 클래슀라 ν•œλ‹€.

 

public class Box<T> {
    
    private T item;

    public T getItem() {
        return item;
    }

    public void setItem(T item) {
        this.item = item;
    }
}

ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©ν•˜λ €λ©΄ 클래슀 λͺ… μš°μΈ‘μ— <>λ₯Ό μ‚¬μš©ν•΄μ„œ μ„ μ–Έν•œλ‹€.

 

public class Box<M, I> {

    private M material;
    private I item;

    public M getMaterial() {
        return material;
    }

    public void setMaterial(M material) {
        this.material = material;
    }

    public I getItem() {
        return item;
    }

    public void setItem(I item) {
        this.item = item;
    }
}

λ˜ν•œ ,(콀마)둜 κ΅¬λΆ„ν•΄μ„œ μ—¬λŸ¬ 개λ₯Ό μ„ μ–Έν•  수 μžˆλ‹€.

 

Box<Paper, String> box = new Box<Paper, String>();
Box<Paper, String> box = new Box<>();   // JDK1.7λΆ€ν„° μƒλž΅ κ°€λŠ₯
Box box = new Box();    // Object둜 κ°„μ£Ό

μ œλ„€λ¦­ ν΄λž˜μŠ€λŠ” μ΄λ ‡κ²Œ μ‚¬μš©ν•  수 μžˆλ‹€.

JDK1.7λΆ€ν„° μƒμ„±μžμ˜ <>에 νƒ€μž…μ„ μƒλž΅ν•  수 μžˆλ‹€.

μ œλ„€λ¦­μ€ JDK1.5λΆ€ν„° λ„μž…λ˜μ—ˆλŠ”λ° ν•˜μœ„ ν˜Έν™˜μ„ μœ„ν•΄ νƒ€μž…μ„ μ§€μ •ν•˜μ§€ μ•Šκ³ λ„ 객체λ₯Ό 생성할 수 있으며 이런 경우 νƒ€μž…μ€ Object둜 κ°„μ£Όλœλ‹€.

μ£Όμ˜ν•΄μ•Ό ν•  점은 μ°Έμ‘°λ³€μˆ˜μ—μ„œ μ§€μ •ν•œ νƒ€μž…κ³Ό μƒμ„±μžμ—μ„œ μ§€μ •ν•œ νƒ€μž…μ€ λ°˜λ“œμ‹œ μΌμΉ˜ν•΄μ•Ό ν•œλ‹€.

두 νƒ€μž…μ΄ μ„œλ‘œ 상속관계에 μžˆλ‹€ 해도 νƒ€μž…μ΄ λ‹€λ₯΄λ©΄ 컴파일 μ—λŸ¬κ°€ λ°œμƒν•œλ‹€.

예λ₯Ό λ“€μ–΄ Cardboard ν΄λž˜μŠ€κ°€ Paper의 μžμ‹ 클래슀라고 ν•΄μ„œ Box<Paper> box = new Box<Cardboard>();λŠ” λΆˆκ°€λŠ₯ν•˜λ‹€.

 

μ œλ„€λ¦­ λ©”μ†Œλ“œ

μ œλ„€λ¦­μ€ 클래슀 λ ˆλ²¨μ—μ„œλ§Œ μ‚¬μš©ν•  수 μžˆλŠ”κ²Œ μ•„λ‹ˆλΌ λ©”μ†Œλ“œ λ ˆλ²¨μ—μ„œλ„ μ‚¬μš©ν•  수 μžˆλ‹€.

μ œλ„€λ¦­ νƒ€μž…μ„ μ„ μ–Έν•œ λ©”μ†Œλ“œλ₯Ό μ œλ„€λ¦­ λ©”μ†Œλ“œλΌ ν•œλ‹€.

클래슀의 μ œλ„€λ¦­ νƒ€μž…μ΄ μ „μ—­ λ³€μˆ˜μ²˜λŸΌ μ‚¬μš©λœλ‹€λ©΄ λ©”μ†Œλ“œμ˜ μ œλ„€λ¦­ νƒ€μž…μ€ ν•΄λ‹Ή λ©”μ†Œλ“œ μ•ˆμ—μ„œλ§Œ μ‚¬μš©ν•  수 μžˆλŠ” 지역성을 κ°–λŠ”λ‹€.

 

public class CoffeeMachine {

    public <T> Coffee makeCoffee(T capsule) {

        return new Coffee(capsule);
    }
}

μ œλ„€λ¦­ νƒ€μž…μ„ λ©”μ†Œλ“œμ—μ„œ μ„ μ–Έν•  λ•ŒλŠ” μ ‘κ·Όμ œν•œμžμ™€ λ°˜ν™˜νƒ€μž… 사이에 μ„ μ–Έν•œλ‹€.

 

CoffeeMachine coffeeMachine = new CoffeeMachine();
Colombian capsule = new Colombian();
coffeeMachine.<Colombian>makeCoffee(capsule);
coffeeMachine.makeCoffee(capsule);  // νƒ€μž… μΆ”μ • κ°€λŠ₯ν•˜λ―€λ‘œ μƒλž΅ κ°€λŠ₯

μ œλ„€λ¦­ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•  λ•ŒλŠ” λ©”μ†Œλ“œ λͺ… μ•žμ— <>둜 νƒ€μž…μ„ μ§€μ •ν•΄μ€˜μ•Ό ν•˜μ§€λ§Œ μ»΄νŒŒμΌλŸ¬κ°€ νƒ€μž…μ„ μΆ”μ •ν•  수 μžˆλŠ” κ²½μš°μ—” μƒλž΅ν•  수 μžˆλ‹€.

λŒ€λΆ€λΆ„μ˜ 경우 μ»΄νŒŒμΌλŸ¬κ°€ νƒ€μž…μ„ μΆ”μ •ν•  수 μžˆλ‹€.

 

μ œλ„€λ¦­ μ œν•œ

μ œλ„€λ¦­μ„ μ‚¬μš©ν•˜λ©΄ μ‚¬μš©ν•  λ•Œ νƒ€μž…μ„ μ§€μ •ν•˜κ²Œ λ˜λŠ”λ°, 'μ–΄λ–€ νƒ€μž…μ΄ 올 수 μžˆλŠ”μ§€'λ₯Ό μ œν•œν•˜μ§€ μ•ŠμœΌλ©΄ 말 κ·ΈλŒ€λ‘œ μ˜€λ§Œκ°€μ§€ νƒ€μž…μ΄ 올 수 μžˆλ‹€. λ”°λΌμ„œ μžλ°”λŠ” extends, super ν‚€μ›Œλ“œλ₯Ό 톡해 이λ₯Ό λ¬Έλ²•μ μœΌλ‘œ μ œν•œν•  수 μžˆλŠ” κΈ°λŠ₯을 μ œκ³΅ν•œλ‹€.

 

class BoxMaterial { }
class Paper { }
class Plastic { }

public class Box<M> {

    private M material;

    public static void main(String[] args) {

        Box<Water> box = new Box<>();   // νƒ€μž… M에 μ œν•œμ΄ μ—†μœΌλ―€λ‘œ μ–΄λ–€ νƒ€μž…μ΄λ“  지정 κ°€λŠ₯
    }
}

 

class BoxMaterial { }
class Paper extends BoxMaterial { }
class Plastic extends BoxMaterial { }

public class Box<M extends BoxMaterial> {

    private M material;

    public static void main(String[] args) {

        Box<Water> waterBox = new Box<>();   // λΆˆκ°€. BoxMaterialκ³Ό μžμ‹ νƒ€μž…λ§Œ κ°€λŠ₯
        Box<Paper> paperBox = new Box<>();
        Box<Plastic> plasticBox = new Box<>();
    }
}

M extends BoxMaterialκ³Ό 같이 μ„ μ–Έν•¨μœΌλ‘œμ¨ Mμ—λŠ” λͺ¨λ“  νƒ€μž…μ΄ μ•„λ‹Œ BoxMaterialκ³Ό κ·Έ μžμ‹ 클래슀만 올 수 μžˆλ‹€.

extends 뒀에 μ˜€λŠ” νƒ€μž…μ΄ μΈν„°νŽ˜μ΄μŠ€μ—¬λ„ λ™μΌν•˜κ²Œ extends ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.

 

class BoxMaterial implements Hard { }
class Paper extends BoxMaterial { }
class Plastic extends BoxMaterial { }
public interface Hard { }

public class Box<M extends BoxMaterial & Hard> {

    private M material;

    public static void main(String[] args) {

        Box<Paper> paperBox = new Box<>();
        Box<Plastic> plasticBox = new Box<>();
    }
}

extends 뒀에 μ—¬λŸ¬ νƒ€μž…μ„ μ§€μ •ν•˜λ €λ©΄ '&'λ₯Ό μ‚¬μš©ν•œλ‹€.

M extends BoxMaterial & HardλŠ” Box의 material ν•„λ“œκ°€ BoxMaterialμ΄λ©΄μ„œ Hardλ₯Ό κ΅¬ν˜„ν•œ νƒ€μž…λ§Œ 지정할 수 μžˆλ„λ‘ μ œν•œν•œ 것이닀.

νƒ€μž…μ„ μ œν•œν•¨μœΌλ‘œμ¨ 잘λͺ»λœ νƒ€μž…μ„ μ§€μ •ν•˜λ©΄ νƒ€μž… μ—λŸ¬λ₯Ό 컴파일 νƒ€μž„μ— κ²€μΆœν•΄μ„œ λŸ°νƒ€μž„ μ—λŸ¬λ₯Ό λ°©μ§€ν•œλ‹€.

 

class BoxMaterial {

    public String printInfo() {
        return "";
    }
}

public class Paper extends BoxMaterial {

    @Override
    public String printInfo() {
        return "쒅이";
    }
}

public class Plastic extends BoxMaterial {

    @Override
    public String printInfo() {
        return "ν”ŒλΌμŠ€ν‹±";
    }
}

public class Box<M extends BoxMaterial> {

    private M material;

    public Box(M material) {
        this.material = material;
    }

    public void printInfo() {
        System.out.println(material.printInfo() + " μƒμžμž…λ‹ˆλ‹€.");
    }

    public static void main(String[] args) {

        Box<Paper> paperBox = new Box<>(new Paper());
        paperBox.printInfo();
        Box<Plastic> plasticBox = new Box<>(new Plastic());
        plasticBox.printInfo();
    }
}

λ˜ν•œ νƒ€μž…μ„ μ œν•œν•˜μ§€ μ•ŠμœΌλ©΄ Box 클래슀 λ‚΄μ—μ„œ material ν•„λ“œλ₯Ό μ΄μš©ν•΄ ν˜ΈμΆœν•  수 μžˆλŠ” λ©”μ†Œλ“œλŠ” Object의 λ©”μ†Œλ“œλ°–μ— μ—†μ§€λ§Œ νƒ€μž…μ„ μ œν•œν•˜λ©΄ ν•΄λ‹Ή νƒ€μž…μ˜ λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

 

extendsλ₯Ό μ‚¬μš©ν•¨μœΌλ‘œμ¨ ν•΄λ‹Ή νƒ€μž…μ˜ μžμ† νƒ€μž…λ§Œ 올 수 μžˆλ„λ‘ ν•˜λ―€λ‘œ μƒν•œ(upper bound)λ₯Ό μ œν•œν•œλ‹€λ©΄ superλŠ” λ°˜λŒ€λ‘œ ν•˜ν•œ(lower bound)λ₯Ό μ œν•œν•œλ‹€. 즉 T super BoxMaterial은 BoxMaterial의 쑰상 νƒ€μž…λ§Œ 올 수 μžˆλ„λ‘ μ œν•œν•œλ‹€. κ·Έ μ™Έμ˜ 차이점은 μ—†λ‹€.

 

μ™€μΌλ“œ μΉ΄λ“œ

class Fruit {

    String name;

    @Override
    public String toString() {
        return name;
    }
}

class Apple extends Fruit {

    public Apple() {
        this.name = "사과";
    }
}

class Banana extends Fruit {

    public Banana() {
        this.name = "λ°”λ‚˜λ‚˜";
    }
}

class FruitCup<T extends Fruit> {

    List<T> fruits;

    public FruitCup() {
        fruits = new ArrayList<>();
    }

    public void addFruit(T fruit) {
        fruits.add(fruit);
    }

    public List<T> getFruits() {
        return fruits;
    }
}

public class Juicer {

    public static Juice makeJuice(FruitCup<Fruit> fruitCup) {
        return new Juice(fruitCup.getFruits());
    }

    public static void main(String[] args) {

        FruitCup<Fruit> fruitCup = new FruitCup<>();
        fruitCup.addFruit(new Apple());
        fruitCup.addFruit(new Banana());

        System.out.println(Juicer.makeJuice(fruitCup).toString());
    }
}

 

πŸ–₯ μ‹€ν–‰ κ²°κ³Ό

Juice{fruits=[사과, λ°”λ‚˜λ‚˜]}

Process finished with exit code 0

 

μœ„ μ½”λ“œμ—μ„œ Juicer 클래슀의 makeJuice λ©”μ†Œλ“œλŠ” FruitCup<Fruit> νƒ€μž…μ˜ λ§€κ°œλ³€μˆ˜λ₯Ό λ°›λŠ”λ‹€.

FruitCup<Apple> appleCup = new FruitCup<>();
appleCup.addFruit(new Apple());
System.out.println(Juicer.makeJuice(appleCup).toString());  // λΆˆκ°€

Apple, BananaλŠ” Fruit의 μžμ†μ΄μ§€λ§Œ FruitCup<Apple>, FruitCup<Banana>λŠ” makeJuice의 인자둜 전달할 수 μ—†λ‹€.

λ©”μ†Œλ“œμ—μ„œ μ§€μ •ν•œ FruitCup<Fruit> νƒ€μž…λ§Œμ΄ μΈμžκ°€ 될 수 μžˆλ‹€.

 

makeJuice λ©”μ†Œλ“œκ°€ Fruit 뿐만 μ•„λ‹ˆλΌ Fruit의 μžμ‹μΈ Apple, Banana와 같은 μ œλ„€λ¦­ νƒ€μž…μ˜ FruitCup을 λ§€κ°œλ³€μˆ˜λ‘œ λ°›μœΌλ €λ©΄ λ©”μ†Œλ“œ λ ˆλ²¨μ—μ„œ μ œλ„€λ¦­ νƒ€μž…μ„ μ„ μ–Έν•˜λ˜μ§€(μ œλ„€λ¦­ λ©”μ†Œλ“œ), 'μ™€μΌλ“œ μΉ΄λ“œ'λ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

μ œλ„€λ¦­μ—μ„œ μ™€μΌλ“œ μΉ΄λ“œλŠ” '?' 기호λ₯Ό μ‚¬μš©ν•˜λ©° 의미 κ·ΈλŒ€λ‘œ μ–΄λ–€ νƒ€μž…λ„ 될 수 μžˆλ‹€.

'?'만 μ‚¬μš©ν•˜λ©΄ Object νƒ€μž…κ³Ό λ‹€λ₯Όκ²Œ μ—†μœΌλ―€λ‘œ μ œλ„€λ¦­ μ œν•œμ—μ„œ μ‚¬μš©ν•˜λŠ” extends, super ν‚€μ›Œλ“œλ₯Ό ν•¨κ»˜ μ‚¬μš©ν•œλ‹€.

 

<? extends T>
T와 κ·Έ μžμ† νƒ€μž…λ§Œ κ°€λŠ₯(upper bound)

<? super T>
T와 κ·Έ 쑰상 νƒ€μž…λ§Œ κ°€λŠ₯(lower bound)

<?>
μ œν•œ 없이 λͺ¨λ“  νƒ€μž…μ΄ κ°€λŠ₯. <? extneds Object>와 λ™μΌν•œ ν‘œν˜„

 

μ™€μΌλ“œ μΉ΄λ“œλ₯Ό μ‚¬μš©ν•΄ λ³€κ²½ν•œ μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

class Fruit {

    String name;

    @Override
    public String toString() {
        return name;
    }
}

class Apple extends Fruit {

    public Apple() {
        this.name = "사과";
    }
}

class Banana extends Fruit {

    public Banana() {
        this.name = "λ°”λ‚˜λ‚˜";
    }
}

class FruitCup<T extends Fruit> {

    List<T> fruits;

    public FruitCup() {
        fruits = new ArrayList<>();
    }

    public void addFruit(T fruit) {
        fruits.add(fruit);
    }

    public List<T> getFruits() {
        return fruits;
    }
}

public class Juicer {

    public static Juice makeJuice(FruitCup<? extends Fruit> fruitCup) {
        return new Juice(fruitCup.getFruits());
    }

    public static void main(String[] args) {

        FruitCup<Fruit> fruitCup = new FruitCup<>();
        fruitCup.addFruit(new Apple());
        fruitCup.addFruit(new Banana());
        System.out.println(Juicer.makeJuice(fruitCup).toString());

        FruitCup<Apple> appleCup = new FruitCup<>();
        appleCup.addFruit(new Apple());
        appleCup.addFruit(new Apple());
        appleCup.addFruit(new Apple());
        System.out.println(Juicer.makeJuice(appleCup).toString());
    }
}

 

πŸ–₯ μ‹€ν–‰ κ²°κ³Ό

Juice{fruits=[사과, λ°”λ‚˜λ‚˜]}
Juice{fruits=[사과, 사과, 사과]}

Process finished with exit code 0

 

μΆ”κ°€λ‘œ μ™€μΌλ“œ μΉ΄λ“œλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  지넀릭 λ©”μ†Œλ“œλ‘œ λ³€κ²½ν•˜λ©΄ λ‹€μŒκ³Ό κ°™λ‹€.

public static Juice makeJuice(FruitCup<? extends Fruit> fruitCup) {
    return new Juice(fruitCup.getFruits());
}

⬇️

public static <T extends Fruit> Juice makeJuice(FruitCup<T> fruitCup) {
        return new Juice(fruitCup.getFruits());
}

μ–΄λ–€ 방법을 μ‚¬μš©ν•˜λ“  λ™μΌν•œ λ™μž‘μ„ ν•œλ‹€.

 

μ œλ„€λ¦­μ˜ 예 - Collections.sort()

μžλ°”μ˜ CollectionsλŠ” μš”μ†Œλ₯Ό μ •λ ¬ν•˜λŠ” static λ©”μ†Œλ“œ sort()λ₯Ό μ œκ³΅ν•˜λŠ”λ°, 이 λ©”μ†Œλ“œκ°€ μ œλ„€λ¦­ νƒ€μž…μ„ μ‚¬μš©ν•˜λŠ” μ œλ„€λ¦­ λ©”μ†Œλ“œμ˜ 예 쀑 ν•˜λ‚˜μ΄λ‹€.

 

Collection.sort(List, Comparator)의 μ„ μ–ΈλΆ€

static <T> void sort(List<T> list, Comparator<? super T> c)

List<T>와 Comparator<? super T> νƒ€μž…μ˜ 두 개의 λ§€κ°œλ³€μˆ˜λ₯Ό λ°›λŠ” sort() λ©”μ†Œλ“œμ΄λ‹€.

첫 번째 λ§€κ°œλ³€μˆ˜λŠ” μ •λ ¬ λŒ€μƒμ΄κ³  두 번째 λ§€κ°œλ³€μˆ˜λŠ” μ •λ ¬ν•  방법이 μ •μ˜λœ Comparator이닀.

ComparatorλŠ” <? super T>λΌλŠ” μ œλ„€λ¦­ νƒ€μž…μ΄ 선언돼 μžˆλŠ”λ°, μ΄λŠ” μ •λ ¬ λŒ€μƒ μš”μ†Œλ“€μ˜ νƒ€μž… ν˜Ήμ€ κ·Έ 쑰상 νƒ€μž…μ˜ Comparatorλ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ ν•œλ‹€λŠ” λœ»μ΄λ‹€.

 

Collection.sort(List)의 μ„ μ–ΈλΆ€

public static <T extends Comparable<? super T>> void sort(List<T> list)

List<T> ν•œ 개λ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ λ°›λŠ” sort() λ©”μ†Œλ“œμ΄λ‹€.

νƒ€μž… Tλ₯Ό μš”μ†Œλ‘œ ν•˜λŠ” Listλ₯Ό λ„˜κΈ°λ©΄ λ˜λŠ”λ°, T λ˜λŠ” κ·Έ 쑰상 ν΄λž˜μŠ€κ°€ Comparable을 κ΅¬ν˜„ν•΄μ•Ό ν•œλ‹€λŠ” μ˜λ―Έμ΄λ‹€.

λ°˜μ‘ν˜•

λŒ“κΈ€