Using Guava to Convert between Primitives Array and Wrapper List


Usually we nee convert between primitives array to wrapper list, for example: convert int[] to List.
Solution 1: Convert them manully
We can write code to convert them manually like below:
 public void intArrayIntListManully() {
    int[] aInts = {1, 2, 3};
    // int array to List
    List aList = new ArrayList(aInts.length);
    for (int i : aInts) {
      aList.add(i);
    }
    System.out.println(aList);
    // this throws UnsupportedOperationException
    // aList.add(4);
    // aList.remove(0);

    // List to int array
    aInts = new int[aList.size()];
    for (int i = 0; i < aList.size(); i++) {
      aInts[i] = aList.get(i);
    }
    System.out.println(aInts.length);
  }
But it's kind of annoy to have to write this boilerplate code  again and again.
Solution 2: Using Guava primitives
We can use utils in Guava com.google.common.primitives to easily convert between them: the code is cleaner, also its performance is better.

public void intArrayIntListViaGuava() {
  int[] aInts = {1, 2, 3};
  List<Integer> aList = Ints.asList(aInts);
  System.out.println(aList);
  
  aInts = Ints.toArray(aList);
  System.out.println(aInts.length);
}
If we check Guava's implementation, Ints.asList doesn't create one ArrayList then add all ints to that list, instead, it creates IntArrayAsList which holds a reference to the original int array, its get method creates and returns the wrapper object.
We can't add new element or remove element to the IntArrayAsList.
  public static List<Integer> asList(int[] backingArray)
  {
    if (backingArray.length == 0) {
      return Collections.emptyList();
    }
    return new IntArrayAsList(backingArray);
  }
  
  public static int[] toArray(Collection<? extends Number> collection)
  {
    if (collection instanceof IntArrayAsList) {
      return ((IntArrayAsList)collection).toIntArray();
    }

    Object[] boxedArray = collection.toArray();
    int len = boxedArray.length;
    int[] array = new int[len];
    for (int i = 0; i < len; ++i)
    {
      array[i] = ((Number)Preconditions.checkNotNull(boxedArray[i])).intValue();
    }
    return array;
  }
 private static class IntArrayAsList extends AbstractList<Integer>    implements RandomAccess, Serializable
  {
    final int[] array;
    final int start;
    final int end;
    IntArrayAsList(int[] array)
    {
      this(array, 0, array.length);
    }

    IntArrayAsList(int[] array, int start, int end) {
      this.array = array;
      this.start = start;
      this.end = end;
    }
    public Integer get(int index) {
      Preconditions.checkElementIndex(index, size());
      return Integer.valueOf(this.array[(this.start + index)]);
    } 
  }
Solution 3: Using Apache Commons Lang
If your project doesn't use Guava, you may use Apache Commons Lang:
public void intArrayIntListViaCommonsLang() {
  int[] aInts = {1, 2, 3};
  List<Integer> aList = Arrays.asList(ArrayUtils.toObject(aInts));
  System.out.println(aList);
  
  aInts = ArrayUtils.toPrimitive(aList.toArray(new Integer[aList.size()]));
  System.out.println(aInts.length);
}
Reference
Guava Primitives Explained

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)