Jos*_*Lee 84
标准框架中没有Pair,但Apache Commons Lang非常接近"标准",它有一对.
gav*_*koa 60
Map.Entry
Java 1.6和upper有两个Map.Entry
接口实现,将一个键与一个值配对:
例如
Map.Entry < Month, Boolean > pair =
new AbstractMap.SimpleImmutableEntry <>(
Month.AUGUST ,
Boolean.TRUE
)
;
Run Code Online (Sandbox Code Playgroud)
pair.toString():AUGUST = true
我需要存储对时使用它(如大小和对象集合).
这件作品来自我的生产代码:
public Map<L1Risk, Map.Entry<int[], Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>>>>
getEventTable(RiskClassifier classifier) {
Map<L1Risk, Map.Entry<int[], Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>>>> l1s = new HashMap<>();
Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>> l2s = new HashMap<>();
Map<L3Risk, List<Event>> l3s = new HashMap<>();
List<Event> events = new ArrayList<>();
...
map.put(l3s, events);
map.put(l2s, new AbstractMap.SimpleImmutableEntry<>(l3Size, l3s));
map.put(l1s, new AbstractMap.SimpleImmutableEntry<>(l2Size, l2s));
}
Run Code Online (Sandbox Code Playgroud)
代码看起来很复杂,但不是Map.Entry,而是限制为对象数组(大小为2)并丢失类型检查...
Bas*_*lew 35
对类:
public class Pair<K, V> {
private final K element0;
private final V element1;
public static <K, V> Pair<K, V> createPair(K element0, V element1) {
return new Pair<K, V>(element0, element1);
}
public Pair(K element0, V element1) {
this.element0 = element0;
this.element1 = element1;
}
public K getElement0() {
return element0;
}
public V getElement1() {
return element1;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
Pair<Integer, String> pair = Pair.createPair(1, "test");
pair.getElement0();
pair.getElement1();
Run Code Online (Sandbox Code Playgroud)
永恒,只有一对!
pom*_*tee 16
这里有很多实现,但总是缺少一些东西,覆盖等于和哈希方法.
这是这个类的更完整版本:
/**
* Container to ease passing around a tuple of two objects. This object provides a sensible
* implementation of equals(), returning true if equals() is true on each of the contained
* objects.
*/
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;
}
/**
* Checks the two objects for equality by delegating to their respective
* {@link Object#equals(Object)} methods.
*
* @param o the {@link Pair} to which this one is to be checked for equality
* @return true if the underlying objects of the Pair are both considered
* equal
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) {
return false;
}
Pair<?, ?> p = (Pair<?, ?>) o;
return Objects.equals(p.first, first) && Objects.equals(p.second, second);
}
/**
* Compute a hash code using the hash codes of the underlying objects
*
* @return a hashcode of the Pair
*/
@Override
public int hashCode() {
return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
}
/**
* Convenience method for creating an appropriately typed pair.
* @param a the first object in the Pair
* @param b the second object in the pair
* @return a Pair that is templatized with the types of a and b
*/
public static <A, B> Pair <A, B> create(A a, B b) {
return new Pair<A, B>(a, b);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
151066 次 |
最近记录: |