Created
June 18, 2016 00:24
-
-
Save koduki/a308203462231dbdf3758ae72eca755f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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