Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 830
Jenix Avatar asked Dec 29 '14 05:12

Jenix


People also ask

What is collection emptyList?

The emptyList() method of Java Collections class is used to get a List that has no elements. These empty list are immutable in nature.

What is the difference between collections emptyList () and creating new instance of collection?

emptyList is immutable so there is a difference between the two versions so you have to consider users of the returned value. Returning new ArrayList<Foo> always creates a new instance of the object so it has a very slight extra cost associated with it which may give you a reason to use Collections.

Can we add elements to collections emptyList?

The emptyList() method of Java Collections returns the list with no elements. This method is immutable. That is, we can not do any modifications after creating this method.

How do you create an empty list of strings in java?

List list = new ArrayList(); Create a List and specify the type of elements it can holds.


2 Answers

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:

  • Generic Methods
  • Type Inference
like image 153
fgb Avatar answered Sep 28 '22 21:09

fgb


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

Collections 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 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>.

like image 43
Sotirios Delimanolis Avatar answered Sep 28 '22 19:09

Sotirios Delimanolis