Guava – Chars Class

  • Post author:
  • Post category:Guava
  • Post comments:0 Comments
Chars

Chars is a utility class for primitive type char.

Class Declaration

Following is the declaration for com.google.common.primitives.Chars class −

@GwtCompatible(emulated = true)
   public final class Chars
      extends Object

Fields

Sr.NoField & Description
1static int BYTESThe number of bytes required to represent a primitive char value.

Methods

Sr.NoMethod & Description
1static List<Character> asList(char… backingArray)Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static char checkedCast(long value)Returns the char value that is equal to value, if possible.
3static int compare(char a, char b)Compares the two specified char values.
4static char[] concat(char[]… arrays)Returns the values from each provided array combined into a single array.
5static boolean contains(char[] array, char target)Returns true if target is present as an element anywhere in array.
6static char[] ensureCapacity(char[] array, int minLength, int padding)Returns an array containing the same values as array, but guaranteed to be of a specified minimum length.
7static char fromByteArray(byte[] bytes)Returns the char value whose big-endian representation is stored in the first 2 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getChar().
8static char fromBytes(byte b1, byte b2)Returns the char value whose byte representation is the given 2 bytes, in big-endian order; equivalent to Chars.fromByteArray(new byte[] {b1, b2}).
9static int hashCode(char value)Returns a hash code for value; equal to the result of invoking ((Character) value).hashCode().
10static int indexOf(char[] array, char target)Returns the index of the first appearance of the value target in array.
11static int indexOf(char[] array, char[] target)Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.
12static String join(String separator, char… array)Returns a string containing the supplied char values separated by separator.
13static int lastIndexOf(char[] array, char target)Returns the index of the last appearance of the value target in array.
14static Comparator<char[]> lexicographicalComparator()Returns a comparator that compares two char arrays lexicographically.
15static char max(char… array)Returns the greatest value present in array.
16static char min(char… array)Returns the least value present in array.
17static char saturatedCast(long value)Returns the char nearest in value to value.
18static char[] toArray(Collection<Character> collection)Copies a collection of Character instances into a new array of primitive char values.
19static byte[] toByteArray(char value)Returns a big-endian representation of value in a 2-element byte array; equivalent to ByteBuffer.allocate(2).putChar(value).array().

Methods Inherited

This class inherits methods from the following class −

  • java.lang.Object

Example of Chars Class

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java

import java.util.List;
import com.google.common.primitives.Chars;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testChars();
   }

   private void testChars() {
      char[] charArray = {'a','b','c','d','e','f','g','h'};

      //convert array of primitives to array of objects
      List<Character> objectArray = Chars.asList(charArray);
      System.out.println(objectArray.toString());

      //convert array of objects to array of primitives
      charArray = Chars.toArray(objectArray);
      System.out.print("[ ");
      
      for(int i = 0; i< charArray.length ; i++) {
         System.out.print(charArray[i] + " ");
      }
      
      System.out.println("]");
      
      //check if element is present in the list of primitives or not
      System.out.println("c is in list? " + Chars.contains(charArray, 'c'));

      //return the index of element
      System.out.println("c position in list " + Chars.indexOf(charArray, 'c'));

      //Returns the minimum		
      System.out.println("Min: " + Chars.min(charArray));

      //Returns the maximum		
      System.out.println("Max: " + Chars.max(charArray));	
   }
}

Verify the Result

Compile the class using javac compiler as follows −

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

[a, b, c, d, e, f, g, h]
[ a b c d e f g h ]
c is in list? true
c position in list 2
Min: a
Max: h

Previous Page:-Click Here

Leave a Reply