JEP 186: Collection Literals

Remi Forax forax at univ-mlv.fr
Thu Jan 23 16:58:11 PST 2014


On 01/23/2014 09:53 PM, Zhong Yu wrote:
> I think we should almost*never*  declare a weaker type on
> non-publicized variables, like
>      List<Thing> things = new ArrayList<>();
> instead, we should almost always declare the most specific type, like
>      ArrayList<Thing> things = new ArrayList<>();
> I don't want to start an argument about this issue here;

I agree.

The main issue is with a code like this
    List<Thing> things = new ArrayList<>();
    for(int i=0; i< things.size(); i++) {
      ...   // i is used here
    }

because it can be changed to the code below without any warnings
    List<Thing> things = new LinkedList<>();
    for(int i=0; i< things.size(); i++) {
      ...  // i is used here
    }

The complexity of the first code is O(n) the second one is O(n2).

cheers,
Rémi



More information about the lambda-dev mailing list