You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
928 B
43 lines
928 B
public class Pair<K, V> {
|
|
private K first;
|
|
private V second;
|
|
|
|
public Pair(K first, V second) {
|
|
this.first = first;
|
|
this.second = second;
|
|
}
|
|
|
|
public K getFirst() {
|
|
return first;
|
|
}
|
|
|
|
public V getSecond() {
|
|
return second;
|
|
}
|
|
|
|
public void setFirst(K first) {
|
|
this.first = first;
|
|
}
|
|
|
|
public void setSecond(V second) {
|
|
this.second = second;
|
|
}
|
|
|
|
public void swap() {
|
|
Object temp = first;
|
|
first = (K) second;
|
|
second = (V) temp;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "(" + first + ", " + second + ")";
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
Pair<String, Integer> pair = new Pair<>("Hello", 42);
|
|
System.out.println("Before swap: " + pair);
|
|
pair.swap();
|
|
System.out.println("After swap: " + pair);
|
|
}
|
|
}
|