7

I have been trying to simply get a name for what the <> part of the declaration is called, to no luck. Can anyone tell me what it's called, how I can use it in my own class? For instance I may want to try and make my own kind of collection, and use new MyThing<String> for instance. Any help is appreciated, thanks!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
user2507230
  • 562
  • 1
  • 3
  • 12
  • 1
    Do you mean `generics`? See http://docs.oracle.com/javase/tutorial/java/generics/index.html – PM 77-1 Jul 21 '13 at 01:00
  • @StephenC I didn't know it was called generics. – user2507230 Jul 21 '13 at 01:14
  • Or this - http://stackoverflow.com/questions/490091/java-generics – Stephen C Jul 21 '13 at 01:15
  • @Stephen This should stay open, because it does use the word "generics" which makes it a different question ("what is this called? How do I do it?") – tckmn Jul 21 '13 at 01:16
  • 1
    @user2507230 - Well now you do! (But seriously, any good Java textbook written in the last 5 to 8 years will cover this topic. It is fundamental to Java since Java 5.) – Stephen C Jul 21 '13 at 01:17
  • @Doorknob - what is the likelihood that someone else (who doesn't have the term "generic" in his Java vocabulary) will come across this Q&A? IMO, the answer is "pretty small". – Stephen C Jul 21 '13 at 01:19
  • @Stephen Pretty high - the first search that comes to mind is " in hashmap java" or "how to use in hashmap in my own class" – tckmn Jul 21 '13 at 01:20
  • You mean like this one? http://stackoverflow.com/questions/10887209/understanding-hashmapk-v. Which is the first hit when I search for " in hashmap java". See ... duplicates, and duplicates. – Stephen C Jul 21 '13 at 01:23
  • @Stephen Read the body. That question has *nothing* to do with generics. – tckmn Jul 21 '13 at 01:24
  • I beg to differ. While the OP doesn't use the word "generic", this has everything to do with generics. He says *"I have been trying to simply get a name for what the `<>` part of the declaration is called, to no luck."* – Stephen C Jul 21 '13 at 01:26
  • @Stephen the question there is "how is containsKey() supposed to tell me if the value associated with the key supplied is null?" the question here is "what is this and how can I use it myself?" – tckmn Jul 21 '13 at 01:27
  • @Doorknob - I accuse you of "selective reading" :-). Anyway, we shall see if this Question survives. – Stephen C Jul 21 '13 at 01:28

2 Answers2

6

That's the generic type declaration of the class. For example, to specify that a map is going to use strings as keys and integers as values:

Map<String, Integer> mymap = new HashMap<String, Integer>();
Óscar López
  • 232,561
  • 37
  • 312
  • 386
4

It's called generics, and here's how you can use it:

public class MyClass<MyType> {
    private MyType myItem;
    public MyClass(MyType item) {
        myItem = item;
    }
    public MyType getMyItem() {
        return myItem;
    }
}

Conventionally the name of the type (MyType in this case) is T for "type" and K and V for "key" and "value", but I'm just making it easier to understand.

You could then do:

MyClass<String> m = new MyClass<String>("potato");
System.out.println(m.getMyItem()); // prints "potato"
tckmn
  • 57,719
  • 27
  • 114
  • 156