Generic

μ œλ„€λ¦­μ€ μžλ°”μ—μ„œ μ•ˆμ •μ„±μ„ λ§‘κ³  μžˆλ‹€κ³  ν•  수 μžˆλ‹€. λ‹€μ–‘ν•œ 객체듀을 λ‹€λ£¨λŠ” λ©”μ„œλ“œλ‚˜ μ»¬λ ‰μ…˜ ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©ν•˜λŠ” κ²ƒμœΌλ‘œ, 컴파일 κ³Όμ •μ—μ„œ νƒ€μž…μ²΄ν¬λ₯Ό ν•΄μ£ΌλŠ” κΈ°λŠ₯이닀. 객체의 νƒ€μž…μ„ 컴파일 μ‹œμ— μ²΄ν¬ν•˜κΈ° λ•Œλ¬Έμ— 객체의 νƒ€μž… μ•ˆμ „μ„±μ„ 높이고 ν˜•λ³€ν™˜μ˜ λ²ˆκ±°λ‘œμ›€μ„ 쀄여쀀닀. μžμ—°μŠ€λŸ½κ²Œ μ½”λ“œλ„ 더 κ°„κ²°ν•΄μ§„λ‹€. 예λ₯Ό λ“€λ©΄,Collection 에 νŠΉμ • 객체만 좔가될 수 μžˆλ„λ‘, λ˜λŠ” νŠΉμ •ν•œ 클래슀의 νŠΉμ§•μ„ κ°–κ³  μžˆλŠ” κ²½μš°μ—λ§Œ 좔가될 수 μžˆλ„λ‘ ν•˜λŠ” 것이 μ œλ„€λ¦­μ΄λ‹€. 이둜 μΈν•œ μž₯점은 collection λ‚΄λΆ€μ—μ„œ λ“€μ–΄μ˜¨ 값이 λ‚΄κ°€ μ›ν•˜λŠ” 값인지 λ³„λ„μ˜ 둜직처리λ₯Ό κ΅¬ν˜„ν•  ν•„μš”κ°€ μ—†μ–΄μ§„λ‹€. λ˜ν•œ apiλ₯Ό μ„€κ³„ν•˜λŠ”λ° μžˆμ–΄μ„œ 보닀 λͺ…ν™•ν•œ μ˜μ‚¬μ „λ‹¬μ΄ κ°€λŠ₯ν•΄μ§„λ‹€.

Why Use Generics?

κ°„λ‹¨νžˆ λ§ν•΄μ„œ μ œλ„€λ¦­μ€ 클래슀, μΈν„°νŽ˜μ΄μŠ€ 및 λ©”μ„œλ“œλ₯Ό μ •μ˜ν•  λ•Œ νƒ€μž…(클래슀 및 μΈν„°νŽ˜μ΄μŠ€)이 맀개 λ³€μˆ˜κ°€ λ˜λ„λ‘ ν•©λ‹ˆλ‹€. λ©”μ„œλ“œ 선언에 μ‚¬μš©λ˜λŠ” 보닀 μΉœμˆ™ν•œ ν˜•μ‹ λ§€κ°œλ³€μˆ˜μ™€ λ§ˆμ°¬κ°€μ§€λ‘œ νƒ€μž… λ§€κ°œλ³€μˆ˜λŠ” μž…λ ₯이 λ‹€λ₯Έ λ™μΌν•œ μ½”λ“œλ₯Ό μž¬μ‚¬μš©ν•  수 μžˆλŠ” 방법을 μ œκ³΅ν•©λ‹ˆλ‹€. ν˜•μ‹ λ§€κ°œλ³€μˆ˜μ— λŒ€ν•œ μž…λ ₯은 값인 반면 νƒ€μž… λ§€κ°œλ³€μˆ˜μ— λŒ€ν•œ μž…λ ₯은 νƒ€μž…μ΄λΌλŠ” 차이점이 μžˆλ‹€.

μ œλ„€λ¦­μ„ μ‚¬μš©ν•˜λŠ” μ½”λ“œλŠ” μ œλ„€λ¦­μ΄ μ•„λ‹Œ μ½”λ“œμ— λΉ„ν•΄ λ§Žμ€ 이점이 μžˆμŠ΅λ‹ˆλ‹€.

1. 컴파일 νƒ€μž„μ— 더 κ°•λ ₯ν•œ μœ ν˜• 검사

Java μ»΄νŒŒμΌλŸ¬λŠ” 일반 μ½”λ“œμ— κ°•λ ₯ν•œ νƒ€μž… 검사λ₯Ό μ μš©ν•˜κ³  μ½”λ“œκ°€ νƒ€μž… μ•ˆμ „μ„ μœ„λ°˜ν•˜λŠ” 경우 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚΅λ‹ˆλ‹€. 컴파일 νƒ€μž„ 였λ₯˜λ₯Ό μˆ˜μ •ν•˜λŠ” 것이 μ°ΎκΈ° μ–΄λ €μšΈ 수 μžˆλŠ” λŸ°νƒ€μž„ 였λ₯˜λ₯Ό μˆ˜μ •ν•˜λŠ” 것보닀 μ‰½μŠ΅λ‹ˆλ‹€.

2. casts 제거

μ œλ„€λ¦­μ΄ μ—†λŠ” λ‹€μŒ μ½”λ“œ μŠ€λ‹ˆνŽ«μ€ casting이 ν•„μš”ν•©λ‹ˆλ‹€.

List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);

μ œλ„€λ¦­μ΄ μ‚¬μš©ν•˜λ„λ‘ λ‹€μ‹œ μž‘μ„±ν•  λ•Œ μ½”λ“œλŠ” μΊμŠ€νŒ…μ΄ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

List<String> list = new ArrayList<String>();
list.add("hello");
String s = list.get(0);   // no cast

3. ν”„λ‘œκ·Έλž˜λ¨Έκ°€ 일반 μ•Œκ³ λ¦¬μ¦˜μ„ κ΅¬ν˜„ν•  수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€.

μ œλ„€λ¦­μ„ μ‚¬μš©ν•˜μ—¬ ν”„λ‘œκ·Έλž˜λ¨ΈλŠ” λ‹€μ–‘ν•œ μœ ν˜•μ˜ μ»¬λ ‰μ…˜μ—μ„œ μž‘λ™ν•˜κ³  μ‚¬μš©μž μ •μ˜ν•  수 있으며 μœ ν˜•μ΄ μ•ˆμ „ν•˜κ³  읽기 μ‰¬μš΄ μ œλ„€λ¦­ μ•Œκ³ λ¦¬μ¦˜μ„ κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Generic Types

μ œλ„€λ¦­ νƒ€μž…μ€ νƒ€μž…μ— λŒ€ν•΄ 맀개 λ³€μˆ˜ν™”λ˜λŠ” μ œλ„€λ¦­ 클래슀 λ˜λŠ” μΈν„°νŽ˜μ΄μŠ€μ΄λ‹€.

A Generic Version of the Box Class

/**
 * Generic version of the Box class.
 * @param <T> the type of the value being boxed
 */
public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}

Type Parameter Naming Conventions

The most commonly used type parameter names are:

  • E - Element (used extensively by the Java Collections Framework)

  • K - Key

  • N - Number

  • T - Type

  • V - Value

  • S,U,V etc. - 2nd, 3rd, 4th types

Multiple Type Parameters Example

public interface Pair<K, V> {
    public K getKey();
    public V getValue();
}

public class OrderedPair<K, V> implements Pair<K, V> {

    private K key;
    private V value;

    public OrderedPair(K key, V value) {
	this.key = key;
	this.value = value;
    }

    public K getKey()	{ return key; }
    public V getValue() { return value; }
}

Reference

https://github.com/JaeYeopHan/Interview_Question_for_Beginner/tree/master/Java#generic

https://docs.oracle.com/javase/tutorial/java/generics/types.html

https://docs.oracle.com/javase/tutorial/java/generics/why.html

Last updated

Was this helpful?