Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java : Understanding Arrays.asList(T...array) method for primitive types

I wrote following code and was surprised to see the output:

    Integer a = 211;
    int b = 211;

    int[] array = {210,211,212};

    System.out.println(Arrays.asList(array).contains(a));
    System.out.println(Arrays.asList(array).contains(b));

Output:

false
false

I found this question and some other questions linked to it and learned that asList method doesn't Autobox stuffs. I checked the returned type in eclipse javadoc preview:

enter image description here

I couldn't quite understand this return type. int[] is an object and not a primitive so its fine. I'm sure that I'm not getting List<Integer> (something which I expected) but I'm not sure how to use the thing which is being returned. My questions are:

    1. How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?
    2. In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?
    3. What good is this method for primitives if things are so uncertain?
like image 955
ares Avatar asked Sep 17 '14 12:09

ares


2 Answers

There are obviously 3 questions here so lets tackle them one by one:

  1. How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?

Well, List methods will work exactly as expected, a List<T> is a list of types T. Here T is an int[] so a List<int[]> will contains arrays as each element:

[{1, 2}, {3, 4}, {1, 6}]

So get(i) will return the ith element. In the case of Arrays.asList the List contains a single element, namely the int[] so:

int[] array = {210,211,212};
List<int[]> list = Arrays.asList(array);

Will be

[{210, 211, 212}]

And so

list.get(0)[0] == 210

In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?

String is an Object, not a primitive type. The difference follows from that.

  1. What good is this method for primitives if things are so uncertain?

Things are not uncertain. This method results in defined and predictable behaviour. It's just not very useful for primitives. This is (yet another) side effect of combining Java's type system with generics.

Note with Java 8 the conversion of an int[] to a List<Integer> is very simple:

List<Integer> list = Arrays.stream(array).
        boxed().
        collect(toList());
like image 195
Boris the Spider Avatar answered Oct 15 '22 18:10

Boris the Spider


You are not getting a Lit or a List (which can't be), you're getting a List of arrays of integer.

So your list does not contain 211, it contains an array that then contains 211.

The array is not "unrolled" into the list, it is added "as is" to a newly created list.

So :

System.out.println(Arrays.asList(array).contains(array)); // Will return true
System.out.println(Arrays.asList(a).contains(a)); // Will return true
like image 32
Simone Gianni Avatar answered Oct 15 '22 19:10

Simone Gianni