Templates allow the creation of functions and classes with one or more types to be specified later. For instance, you can create a stack of items of type T, then instantiate it as a stack of integer, or strings, or whatever you want, as often as you want.
Java generics were taken from C++ templates, and use a similar syntax. (With, as usual, several differences.) They are also less general, since Java's type parameters may represent only non-primitive types, while C++'s may be of any type.
After templates were added to C++, a large number of data structure template classes to the standard libraries. These were derived from something called the Standard Template Library developed at Hewlett-Packard. This has freed a lot of C++ programmers from needing to reinvent the tree.