Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there an extra <E> in this generic method?

I learned java generics some time ago, but now I'm learning collections and found some code that I don't understand. Here is the code:

static <E> List<E> nCopies(int n, E value)

It is from class java.util.Collections.

My question is why there is:

<E> List<E>

and not only

List<E>

Obviously I am missing something, can someone clarify this for me?

like image 881
Иван Бишевац Avatar asked Nov 14 '11 01:11

Иван Бишевац


People also ask

What does E mean in generics?

A type variable, <T>, can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable. The most commonly used type parameter names are: E - Element (used extensively by the Java Collections Framework) K - Key. N - Number.

What is generic type E in Java?

For example, the type java. util. List<E> is a generic type: a list that holds elements of some type represented by the placeholder E . This type has a method named add() , declared to take an argument of type E , and a method named get() , declared to return a value of type E .

How do you write a generic method?

All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example). Each type parameter section contains one or more type parameters separated by commas.

What are generic methods?

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

Why do we use generic methods?

The way to do deal with these problems is to use generic methods. Just like type declarations, method declarations can be generic—that is, parameterized by one or more type parameters. We can call this method with any kind of collection whose element type is a supertype of the element type of the array.

What is the generic name <E> in Java?

The generic name <E> stands for Element and is commonly used in the Java Collections Framework. In the example below, we create a static method called newListWithElements () that takes a generic type E parameter with the variable arguments operator ... called listElements.

How to avoid confusion between generic class and generic method names?

If a generic method appears inside a generic class, it's a good idea to avoid using the same names for the type parameters of the method and class, to avoid confusion. The same applies to nested generic classes.

What is generics in Java?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc, and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types. An entity such as class, interface, or method that operates on a parameterized type is called ...


1 Answers

You use the <E> to typify the method you are defining.

The most common example of generics is to have a typified class like this:

public class SomeClass<E> {
    ...
}

Then, when you are creating a new object of that class you define the type directly like this:

new SomeClass<String>();

That way any method in that class that refers to <E>, will treat <E> as a String, for that instance.

Now consider a static method (which is not bound to any particular instance of a class), in order to typify that method you have use another kind of typification which applies to methods, like this:

static <E> List<E> nCopies(int n, E value)

You use the <E> before the return type to say "this particular method will consider some E when it executes". What <E> will be is decided when you invoke the method:

nCopies(3, "a");

In this example <E> will be a String, so the return type will be a List<String>.

Finally, you can even mix them both:

public class SomeClass<E> {
    public <F> void doSomething(E e, F f) {
        ...
    }
}

In this case, if you have an instance of SomeClass, the E in the doSomething method will always be String (for that instance), but the F can be anything you want it to be.

like image 196
Francisco Paulo Avatar answered Sep 22 '22 17:09

Francisco Paulo