0

So I see people putting <> after declaring a collection. I know that it is used to specify which data type the collection contains. I haven't seen it used in any other cases so I was just wondering what it is called and if their are any other ways of using this technique? Thanks

1

2 Answers 2

1

Angled brackets/Generics are used for defining the data type that can be stored in the collection. Without generics, you can store entities of type Object, which means anything as all the classes extend from Object. But in business scenario we may not need such a generic collection and would like to avoid putting different kind of objects in a collection. For example if you have a collection of names, you may not want numbers to be stored in such a collection. To restrict this you can define collection with , this will mandate the coder to store only the String type values in the collection.

0

Java Generics. You call the thing inside the angled brackets a type parameter.

From Java Tutorials: JDK 5.0 introduces several new extensions to the Java programming language. One of these is the introduction of generics.

See http://docs.oracle.com/javase/tutorial/extra/generics/intro.html

Not the answer you're looking for? Browse other questions tagged or ask your own question.