What is <K, V> in a hash map and how to use it in my class?
2 answers
He called generics, and here is how you can use it:
public class MyClass<MyType> { private MyType myItem; public MyClass(MyType item) { myItem = item; } public MyType getMyItem() { return myItem; } } Usually the type name ( MyType in this case) is T for "type" and K and V for "key" and "value", but I just make it easier to understand.
Then you can:
MyClass<String> m = new MyClass<String>("potato"); System.out.println(m.getMyItem()); // prints "potato" +3