Skip to content

Instantly share code, notes, and snippets.

@koduki
Created June 18, 2016 00:24
Show Gist options
  • Save koduki/a308203462231dbdf3758ae72eca755f to your computer and use it in GitHub Desktop.
Save koduki/a308203462231dbdf3758ae72eca755f to your computer and use it in GitHub Desktop.
Tuple2<String, Integer> x = new Tuple2<>("A", 10);
Tuple2<Integer, String> y = $(1, "B");
Map<Integer, String> xs = map($(1, "A"), $(2, "B"), $(3, "C"));
public class Collections {
public static <K, V> java.util.Map<K, V> map(Tuple.Tuple2<K, V>... xs) {
java.util.Map<K, V> map = new HashMap<>(xs.length);
for (Tuple.Tuple2<K, V> x : xs) {
map.put(x._1(), x._2());
}
return map;
}
}
public class Tuple {
public static <T1, T2> Tuple2<T1, T2> $(T1 _1, T2 _2) {
return new Tuple2<>(_1, _2);
}
public static class Tuple2<T1, T2> implements Serializable {
public T1 _1;
public T2 _2;
public Tuple2(T1 _1, T2 _2) {
this._1 = _1;
this._2 = _2;
}
public T1 _1() {
return _1;
}
public T2 _2() {
return _2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 59 * hash + Objects.hashCode(this._1);
hash = 59 * hash + Objects.hashCode(this._2);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Tuple2<?, ?> other = (Tuple2<?, ?>) obj;
if (!Objects.equals(this._1, other._1)) {
return false;
}
if (!Objects.equals(this._2, other._2)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Tuple2{" + "_1=" + _1 + ", _2=" + _2 + '}';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment