What is the equivalent of the C++ Pair in Java?

前端 未结 30 1027
一个人的身影
一个人的身影 2020-11-22 02:29

Is there a good reason why there is no Pair in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own.<

30条回答
  •  梦如初夏
    2020-11-22 02:50

    The shortest pair that I could come up with is the following, using Lombok:

    @Data
    @AllArgsConstructor(staticName = "of")
    public class Pair {
        private F first;
        private S second;
    }
    

    It has all the benefits of the answer from @arturh (except the comparability), it has hashCode, equals, toString and a static “constructor”.

提交回复
热议问题