Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //interface Pair
- public interface Pair<K, V> {
- public K getKey();
- public V getValue();
- }
- //class OrderPair
- public class OrderPair<K, V> implements Pair<K, V> {
- private K key;
- private V value;
- //Constructor generic
- public OrderPair(K key, V value) {
- this.key = key;
- this.value = value;
- }
- public K getKey() { return key; }
- public V getValue() { return value; }
- // Generic methods
- public void setKey(K key) { this.key = key; }
- public void setValue(V value) { this.value = value; }
- }
- //class Util
- public class Util {
- // Generic static method
- public static <K, V> boolean compare(OrderPair<K, V> p1, OrderPair<K, V> p2)
- {
- return p1.getKey().equals(p2.getKey()) && p1.getValue().equals(p2.getValue());
- }
- public static void main(String[] args)
- {
- OrderPair<String, Integer> p1 = new OrderPair<String, Integer>("Even ", 8);
- OrderPair<String, String> p2 = new OrderPair<String, String>("hello", " world");
- //boolean same = Util.<Integer, String>compare(p1, p2);
- //sau:
- //boolean same = Util.compare(p1, p2);
- System.out.println(p1.getKey()+p1.getValue());
- System.out.println(p2.getKey()+p2.getValue());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement