Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
Guava - Floats Class
Floats is a utility class for primitive type float.
Class Declaration
Following is the declaration for com.google.common.primitives.Floats class −
@GwtCompatible(emulated = true)
public final class Floats
extends Object
| Sr.No | Field & Description |
|---|---|
| 1 |
static int BYTES The number of bytes required to represent a primitive float value. |
| Sr.No | Method & Description |
|---|---|
| 1 |
static List<Float> asList(float... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 |
static int compare(float a, float b) Compares the two specified float values using Float.compare(float, float). |
| 3 |
static float[] concat(float[]... arrays) Returns the values from each provided array combined into a single array. |
| 4 |
static boolean contains(float[] array, float target) Returns true if target is present as an element anywhere in array. |
| 5 |
static float[] ensureCapacity(float[] array, int minLength, int padding) Returns an array containing the same values as the array, but guaranteed to be of a specified minimum length. |
| 6 |
static int hashCode(float value) Returns a hash code for value; equal to the result of invoking ((Float) value).hashCode(). |
| 7 |
static int indexOf(float[] array, float target) Returns the index of the first appearance of the value target in array. |
| 8 |
static int indexOf(float[] array, float[] target) Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence. |
| 9 |
static boolean isFinite(float value) Returns true if value represents a real number. |
| 10 |
static String join(String separator, float... array) Returns a string containing the supplied float values, converted to strings as specified by Float.toString(float), and separated by separator. |
| 11 |
static int lastIndexOf(float[] array, float target) Returns the index of the last appearance of the value target in array. |
| 12 |
static Comparator<float[]> lexicographicalComparator() Returns a comparator that compares two float arrays lexicographically. |
| 13 |
static float max(float... array) Returns the greatest value present in array, using the same rules of comparison as Math.min(float, float). |
| 14 |
static float min(float... array) Returns the least value present in array, using the same rules of comparison as Math.min(float, float). |
| 15 |
static Converter<String,Float> stringConverter() Returns a serializable converter object that converts between strings and floats using Float.valueOf(java.lang.String) and Float.toString(). |
| 16 |
static float[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a float value in the manner of Number.floatValue(). |
| 17 |
static Float tryParse(String string) Parses the specified string as a single-precision floating point value. |
Methods Inherited
This class inherits methods from the following class −
- java.lang.Object
Example - Converting Array of Floats to List and vice versa
GuavaTester.java
package com.tutorialspoint;
import java.util.List;
import com.google.common.primitives.Floats;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testFloats();
}
private void testFloats() {
float[] floatArray = {1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f};
//convert array of primitives to array of objects
List<Float> objectArray = Floats.asList(floatArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
floatArray = Floats.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< floatArray.length ; i++) {
System.out.print(floatArray[i] + " ");
}
System.out.println("]");
}
}
Output
Run the GuavaTester and verify the output −
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] [ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ]
Example - Checking Presence of Element in Array of Floats
GuavaTester.java
package com.tutorialspoint;
import com.google.common.primitives.Floats;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testFloats();
}
private void testFloats() {
float[] floatArray = {1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f};
//check if element is present in the list of primitives or not
System.out.println("5.0 is in list? " + Floats.contains(floatArray, 5.0f));
//return the index of element
System.out.println("5.0 position in list " + Floats.indexOf(floatArray, 5.0f));
}
}
Output
Run the GuavaTester and verify the output −
5.0 is in list? true 5.0 position in list 4
Example - Getting Minimum/Maximum from Array of Floats
GuavaTester.java
package com.tutorialspoint;
import com.google.common.primitives.Floats;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testFloats();
}
private void testFloats() {
float[] floatArray = {1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f};
//Returns the minimum
System.out.println("Min: " + Floats.min(floatArray));
//Returns the maximum
System.out.println("Max: " + Floats.max(floatArray));
}
}
Output
Run the GuavaTester and verify the output −
Min: 1.0 Max: 9.0