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

前端 未结 30 1483
一个人的身影
一个人的身影 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 03:04

    It depends on what you want to use it for. The typical reason to do so is to iterate over maps, for which you simply do this (Java 5+):

    Map map = ... ; // just an example
    for (Map.Entry entry : map.entrySet()) {
      System.out.printf("%s -> %s\n", entry.getKey(), entry.getValue());
    }
    

提交回复
热议问题