Ints asList() function in Java


The asList() method of the Ints class in Java Guava’s returns a fixed-size list backed by the specified array. The syntax is as follows −

public static List<Integer>
   asList(int[] arr)

Here, arr is the array to back the list.

Let us see an example to implement the asList() method of the Ints class −

Example

import com.google.common.primitives.Ints;
import java.util.List;
class Demo {
   public static void main(String[] args) {
      int arr[] = { 20, 30, 40, 60, 80, 90, 120, 150 };
      System.out.println("Array elements = ");
      for(int i = 0; i < arr.length; i++) {
         System.out.println(arr[i]);
      }
      List<Integer> list = Ints.asList(arr);
      System.out.println("List = "+ list);
   }
}

Output

Array =
20
30
40
60
80
90
120
150
List = [20, 30, 40, 60, 80, 90, 120, 150]

Updated on: 23-Sep-2019

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements