Explain the syntax of Collections.<String>emptyList()

JavaGenericsCollections

Java Problem Overview


I just studied about generic programming, the List<E> interface, and ArrayList, so I can understand the statement below.

ArrayList<String> list = new ArrayList<String>();

But I don't understand the next statement which I saw while surfing the web.

List<String> list2 = Collections.<String>emptyList();
  1. What is Collections? Why isn't it Collections<E> or Collections<String>?
  2. Why is <String> placed before the method name emptyList?

(Isn't emptyList<String>() correct for Generic?)

  1. What does the statement mean?

Java Solutions


Solution 1 - Java

That line creates an empty list of strings by calling a static method with a generic type parameter.

Inside the Collections class, there is a static method emptyList declared like:

public static final <T> List<T> emptyList() {
    return (List<T>) EMPTY_LIST;
}

This has a generic type parameter T. We call call this method by using:

List<String> list = Collections.emptyList();

and T is infered to be a String because of the type of list.

We can also specify the type of T by putting it in angle brackets when calling emptyList. This may be needed if we want a more specific type than is inferred:

List<? extends Object> list = Collections.<String>emptyList();

emptyList<String>() is not correct because that placement is only valid when creating instances of generic classes, not calling methods. When using new there are two possible type parameters, the ones before the class name are for the constructor only, and the ones after the class name are for the whole instance, so with the class:

class MyClass<A> {
    public <B> MyClass(A a, B b) {
        System.out.println(a + ", " + b);
    }
}

We can call its constructor where A is String and B is Integer like:

MyClass<String> a = new <Integer>MyClass<String>("a", 3);

or by using type inference:

MyClass<String> a = new MyClass<>("a", 3);

See also:

Solution 2 - Java

> What is Collections? Why isn't it Collections<E> or Collections<String>?

[Collections][1] is a JDK class.

> This class consists exclusively of static methods that operate on or > return collections. It contains polymorphic algorithms that operate on > collections, "wrappers", which return a new collection backed by a > specified collection, and a few other odds and ends.

It's not generic and cannot be instantiated.

> Why is <String> placed before the method name emptyList?

[Collections#emptyList][2] is a generic method. Here, we are explicitly specifying a type argument, String.

> (Isn't emptyList<String>() correct for Generic?)

No, in Java, generic type arguments for methods come before the method name.

> What does the statement mean?

We are invoking the static emptyList method and assigning its return value to a variable of type List<String>. [1]: http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html [2]: http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#emptyList--

Solution 3 - Java

In a nutshell, this creates an empty, immutable list of strings.

Let's look at the expression bit by bit.

Collections is the name of a class. From the Javadoc:

> This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.

emptyList() is the name of a static method defined in the Collections class (Javadoc). It is a generic method, and the <String> in Collections.<String>emptyList() specifies the generic type argument.

The method returns a List<T>, which in this case is List<String>: a list of strings. More specifically, it returns an empty, immutable list of strings.

Solution 4 - Java

1.Collection is an interface and collections is a static class consists exclusively of static methods that operate on or return collections. Hence we cannot use Collections<E> or Collections<String>.

2.<String> before emptyList(used to get the empty list that is immutable) denotes that only String values can be added to it. Like:

list2.add("A");
list2.add("B");

3.The statement means that it will create a immutable empty list of type List.

You can check out this link: https://stackoverflow.com/questions/1796275/difference-between-java-collection-and-collections

Solution 5 - Java

Firstly, Collections is a helper library of static methods that operate on various types of collections. If you've done any sort of C++ programming, it's very synonymous to the <algorithm> library of functions.

In this case, you're invoking the static method emptyList(), which returns an empty list of a particular type. Since these methods still require a type, but Collections' methods can refer to many types, you have to specify the type upon invocation.

In order to call a generic method, you must call it with the parameter list (i.e. <String>) before the method name, but after the dot.

List<String> someList = Collections.<String>emptyList();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJenixView Question on Stackoverflow
Solution 1 - JavafgbView Answer on Stackoverflow
Solution 2 - JavaSotirios DelimanolisView Answer on Stackoverflow
Solution 3 - JavaNPEView Answer on Stackoverflow
Solution 4 - JavaLeoView Answer on Stackoverflow
Solution 5 - JavaQix - MONICA WAS MISTREATEDView Answer on Stackoverflow