Open In App
Related Articles

Java Guava | Floats.asList() method with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

The Floats.asList() method of Guava’s Floats Class accepts a float array as a parameter and returns a list which has the fixed size. The returned list is backed by the float 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<Float> asList(float… backingArray)

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

Return Value: The method Floats.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 Floats.asList() method
  
import com.google.common.primitives.Floats;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a Float array
        float arr[] = { 1.2f, 2.3f, 3.4f, 4.5f, 5.6f };
  
        // Using Floats.asList() method to wrap
        // the specified primitive Float array
        // as a List of the Float type
        List<Float> myList = Floats.asList(arr);
  
        // Displaying the elements in List
        System.out.println(myList);
    }
}


Output:

[1.2, 2.3, 3.4, 4.5, 5.6]

Example 2:




// Java code to show implementation of
// Guava's Floats.asList() method
  
import com.google.common.primitives.Floats;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a Float array
        float arr[] = { 3.2f, 5.5f, 7.6f };
  
        // Using Floats.asList() method to wrap
        // the specified primitive Float array
        // as a List of the Float type
        List<Float> myList = Floats.asList(arr);
  
        // Displaying the elements in List
        System.out.println(myList);
    }
}


Output:

[3.2, 5.5, 7.6]

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


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 01 Feb, 2019
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials