Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics in Collection

Tags:

java

generics

Here is my program. I am not sure why I am getting a compile time error.

import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
    List< ? extends Number > list = new ArrayList<Integer>();

    list.add(6); // Compile Time Error

    System.out.println(list);

  }
}

But the following program works fine

import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
    List< ? super Number > list = new ArrayList<Number>();

    list.add(6); 

    System.out.println(list);

    }
}

Error from Eclipse:

Here is the error description from Eclipse:

The method add(int, capture#1-of ? extends Number) in the type List is not applicable for the arguments (int)

like image 686
Vikash Joshi Avatar asked Sep 24 '12 14:09

Vikash Joshi


People also ask

Is it a good idea to use generics in collections?

By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.

What is difference between generics and collections?

Generics are similar to collections, but implemented using Type parameters. Generic collections accept a type parameter and accept the elements of only those type for which the generic collection is instantiated. These enforce strict type checks.

Are generics only limited to collections?

Just because collections are the most representative example of generics usage, you are not limited to them.

What is the generic and non-generic collections?

A Generic collection is a class that provides type safety without having to derive from a base collection type and implement type-specific members. A Non-generic collection is a specialized class for data storage and retrieval that provides support for stacks, queues, lists and hashtables.


1 Answers

It's because what you are doing in the first case isn't type safe. You have declared list as a List of "some subclass of Number", and then tried to insert an Integer into it. There is absolutely no guarantee that Integer is compatible with the actual run-time type of the underlying list. The compiler stops you here because what you are doing doesn't make any sense.

Consider, as an extreme example:

List< ? extends Object > list = new ArrayList<Integer>();
list.add("Hello, World!");

If this were to work, you would have a List<Integer> with a String in it!

If you really want it to work, you have to tell the compiler you know what you are doing by casting:

((List<Integer>)list).add(6);

But even then you will still get a warning about type safety.

The second case works because the list is guaranteed to be "some superclass of Number". Integer is a subclass of Number, so it can be implicitly converted to any superclass (including Number itself), so there is no risk that the value is incompatible with the actual type of the list.

For further information, you may want to read up on the difference between covariance and contravariance.

like image 93
verdesmarald Avatar answered Oct 06 '22 00:10

verdesmarald