Home > Java > javaTutorial > Does Java Need a Pair Equivalent Like C 's Pair?

Does Java Need a Pair Equivalent Like C 's Pair?

Barbara Streisand
Release: 2024-12-31 19:19:14
Original
435 people have browsed it

Does Java Need a Pair Equivalent Like C  's Pair?

Pair Equivalent in Java

Java lacks an equivalent to C 's Pair. The absence of such a construct has been a subject of debate, with Hunter Gratzner arguing against its inclusion due to the lack of semantics it conveys regarding the relationship between the two values.

Instead of reimplementing Pair, best practices suggest:

  • Creating simple, dedicated classes: Mike recommends this approach, where specific classes are defined for each intended usage, e.g., Position(x,y), Range(begin,end), or Entry(key,value).

Avoidance of the convoluted AbstractMap.SimpleEntry:

  • Java 1.6 introduced AbstractMap.SimpleEntry, which resembles a pair. However, its complexity makes it a less desirable solution.

In summary, it is generally advisable to opt for dedicated classes over a generic Pair construct to convey clear semantics and avoid potential ambiguities.

The above is the detailed content of Does Java Need a Pair Equivalent Like C 's Pair?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template