Java에 Pair가 없는 이유 (추측)

Posted by epicdev Archive : 2012. 3. 27. 04:00

출처:  http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java 

요약: Pair라는 것은 어떠한 자료의 추상적인 형태만을 나타내는 것이지, 실제데이터의 "의미"를 전달하는 명칭이 아니므로

(예를 들면 Pair의 first나 second가 의미하는것은 실제 데이터와 관계가 없다라는 것. 코딩할 때 변수명을 a, b 같은 것들로 사용하지 말라는 것과 같은 맥락)

Pair를 사용하는 것보다, 실제 데이터의 "의미"를 전달 할 수 있는 Entry나 Position이나 Range 등을 사용하는 것이 올바르기 때문이다.


In this thread on comp.lang.java.help, Hunter Gratzner gives some arguments against the presence of a Pair construct in Java. The main argument is that a class Pair doesn't convey any semantics about the relationship between the two values (how do you know what "first" and "second" mean ?).

A better practice is to write a very simple class, like the one Mike proposed, for each application you would have made of the Pair class. Map.Entry is an example of a pair that carry its meaning in its name.

To sum up, in my opinion it is better to have a class Position(x,y), a class Range(begin,end)and a class Entry(key,value) rather than a generic Pair(first,second) that doesn't tell me anything about what it's supposed to do.