Java 实用技巧
利用Pair返回两种类型的数据
平时写方法时候想返回两个类型的数据可以利用Pair
查看他的源码,其实就是传入两个泛型
public class Pair<F, S> {
public final F first;
public final S second;
/**
* Constructor for a Pair.
*
* @param first the first object in the Pair
* @param second the second object in the pair
*/
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
}
用法如下:
Pair<Integer, String> pair = new Pair<>(1, "One");
Integer key = pair.first;
String value = pair.second;
Powered by Jekyll | Theme 3-Jekyll | Hosted on Github