2 changed files with 29 additions and 0 deletions
@ -0,0 +1,29 @@ |
|||||
|
package w8; |
||||
|
|
||||
|
public class Pair<k,v> { |
||||
|
private k key; |
||||
|
private v value; |
||||
|
public Pair(k key,v value){ |
||||
|
this.key=key; |
||||
|
this.value=value; |
||||
|
} |
||||
|
public k getkey(){ |
||||
|
return key; |
||||
|
} |
||||
|
public v getvalue(){ |
||||
|
return value; |
||||
|
} |
||||
|
public static <k,v> Pair<v,k> swap(Pair<k,v> pair){ |
||||
|
return new Pair<>(pair.getvalue(),pair.getkey()); |
||||
|
} |
||||
|
} |
||||
|
class PairTest { |
||||
|
public static void main(String[] args) { |
||||
|
Pair<Integer,String> pair=new Pair<>(1,"a"); |
||||
|
System.out.println(pair.getkey()); |
||||
|
System.out.println(pair.getvalue()); |
||||
|
Pair<String,Integer> pair2=Pair.swap(pair); |
||||
|
System.out.println(pair2.getkey()); |
||||
|
System.out.println(pair2.getvalue()); |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 329 KiB |
Loading…
Reference in new issue