Skip to content
Related Articles

Related Articles

Improve Article

Java Guava | Bytes.asList() method with Examples

  • Last Updated : 01 Feb, 2019

The Bytes.asList() method of Guava’s Bytes Class accepts a byte array as a parameter and returns a list which has the fixed size. The returned list is backed by the byte array which is passed as the argument. It means the changes made in the array passed as parameter will be reflected back in the list returned by the method and vice-versa.

Syntax:

public static List<Byte> asList(byte… backingArray)

Parameter: The method accepts a mandatory parameter backingArray which is a byte array that is used to back the list.

Return Value: The method Bytes.asList() returns a fixed-size list which is backed by the array passed as the argument to the method. In other words, the method returns the list view of the array.



Below examples illustrate the implementation of above method:

Example 1:




// Java code to show implementation of
// Guava's Bytes.asList() method
  
import com.google.common.primitives.Bytes;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a Byte array
        byte arr[] = { 1, 2, 3, 4, 5 };
  
        // Using Bytes.asList() method to wrap
        // the specified primitive Byte array
        // as a List of the Byte type
        List<Byte> myList = Bytes.asList(arr);
  
        // Displaying the elements in List
        System.out.println(myList);
    }
}
Output:
[1, 2, 3, 4, 5]

Example 2:




// Java code to show implementation of
// Guava's Bytes.asList() method
  
import com.google.common.primitives.Bytes;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a Byte array
        byte arr[] = { 3, 5, 7 };
  
        // Using Bytes.asList() method to wrap
        // the specified primitive Byte array
        // as a List of the Byte type
        List<Byte> myList = Bytes.asList(arr);
  
        // Displaying the elements in List
        System.out.println(myList);
    }
}
Output:
[3, 5, 7]

Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/primitives/Bytes.html#asList(byte…)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :