
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 - Bytes Class
Bytes is a utility class for primitive type byte.
Class Declaration
Following is the declaration for com.google.common.primitives.Bytes class −
@GwtCompatible public final class Bytes extends Object
Sr.No | Method & Description |
---|---|
1 |
static List<Byte> asList(byte... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
2 |
static byte[] concat(byte[]... arrays) Returns the values from each provided array combined into a single array. |
3 |
static boolean contains(byte[] array, byte target) Returns true if the target is present as an element anywhere in array. |
4 |
static byte[] ensureCapacity(byte[] array, int minLength, int padding) Returns an array containing the same values as the array, but guaranteed to be of a specified minimum length. |
5 |
static int hashCode(byte value) Returns a hash code for value; equal to the result of invoking ((Byte) value).hashCode(). |
6 |
static int indexOf(byte[] array, byte target) Returns the index of the first appearance of the value target in array. |
7 |
static int indexOf(byte[] array, byte[] target) Returns the start position of the first occurrence of the specified target within the array, or -1 if there is no such occurrence. |
8 |
static int lastIndexOf(byte[] array, byte target) Returns the index of the last appearance of the value target in array. |
9 |
static byte[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a byte value in the manner of Number.byteValue(). |
Methods Inherited
This class inherits methods from the following class −
- java.lang.Object
Example - Converting Byte Array to List and vice versa
GuavaTester.java
package com.tutorialspoint; import java.util.List; import com.google.common.primitives.Bytes; public class GuavaTester { public static void main(String args[]) { GuavaTester tester = new GuavaTester(); tester.testBytes(); } private void testBytes() { byte[] byteArray = {1,2,3,4,5,5,7,9,9}; //convert array of primitives to array of objects List<Byte> objectArray = Bytes.asList(byteArray); System.out.println(objectArray.toString()); //convert array of objects to array of primitives byteArray = Bytes.toArray(objectArray); System.out.print("[ "); for(int i = 0; i< byteArray.length ; i++) { System.out.print(byteArray[i] + " "); } System.out.println("]"); } }
Output
Run the GuavaTester and verify the output −
[1, 2, 3, 4, 5, 5, 7, 9, 9] [ 1 2 3 4 5 5 7 9 9 ]
Example - Checking element in a Byte Array
GuavaTester.java
package com.tutorialspoint; import com.google.common.primitives.Bytes; public class GuavaTester { public static void main(String args[]) { GuavaTester tester = new GuavaTester(); tester.testBytes(); } private void testBytes() { byte[] byteArray = {1,2,3,4,5,5,7,9,9}; byte data = 5; //check if element is present in the list of primitives or not System.out.println("5 is in list ? " + Bytes.contains(byteArray, data)); data = 6; System.out.println("6 is in list ? " + Bytes.contains(byteArray, data)); } }
Output
Run the GuavaTester and verify the output −
5 is in list ? true 6 is in list ? false
Example - Getting Index and Last Index of Element in Byte Array
GuavaTester.java
package com.tutorialspoint; import com.google.common.primitives.Bytes; public class GuavaTester { public static void main(String args[]) { GuavaTester tester = new GuavaTester(); tester.testBytes(); } private void testBytes() { byte[] byteArray = {1,2,3,4,5,5,7,9,9}; byte data = 5; //Returns the index System.out.println("Index of 5: " + Bytes.indexOf(byteArray,data)); //Returns the last index maximum System.out.println("Last index of 5: " + Bytes.lastIndexOf(byteArray,data)); } }
Output
Run the GuavaTester and verify the output −
Index of 5: 4 Last index of 5: 5