generic method inside non-generic
Wed May 29 2024 10:00:58 GMT+0000 (Coordinated Universal Time)
Saved by
@signup
public class GenericMethod {
public static <K, R, V> boolean compare(Pack<K, R> p1, Pack<K, V> p2) {
return p1.getKey().equals(p2.getKey()) && p1.getValue().equals(p2.getValue());
}
public static void main(String[] args) {
Pack<Integer, String> p1 = new Pack<>(1, "pack");
Pack<Integer, String> p2 = new Pack<>(2, "abc");
Pack<Integer, String> p3 = new Pack<>(1, "pack");
System.out.println("p1 & p2 are same?: " + compare(p1, p2));
System.out.println("p1 & p3 are same?: " + compare(p1, p3));
}
}
class Pack<K, V> {
private K key;
private V value;
public Pack(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return this.key;
}
public V getValue() {
return this.value;
}
}
content_copyCOPY
Comments