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; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter