Tuesday, 31 January 2017

Generic : GenericClassWithInterfaceUsingKeyValue


interface Pairs<K,V> {
   public K getKey();
   public V getValue();
}
public class GenericClassWithInterfaceUsingKeyValue<K,V> implements Pairs<K,V> {

    K k;
    V v;
    GenericClassWithInterfaceUsingKeyValue(K k, V v)
    {
        this.k = k;
        this.v = v;
    }
    @Override
    public K getKey() {
        return k;
    }

    @Override
    public V getValue() {
        return v;
    }
   
    public static void main(String[] args)
    {
        Class1<String, Integer> c1 = new Class1<>("One", 1);
        Class1<String, Integer> c2 = new Class1<>("Two", 2);
        Class1<String, Integer> c3 = new Class1<>("Three", 3);
       
        System.out.print("Key : "+c1.getKey());
        System.out.println(", Value : "+c1.getValue());
       
        System.out.print("Key : "+c2.getKey());
        System.out.println(", Value : "+c2.getValue());
       
        System.out.print("Key : "+c3.getKey());
        System.out.println(", Value : "+c3.getValue());
       
       
    }
   
}

No comments:

Post a Comment