Guava - Chars 类
Chars 是基本类型 char 的实用程序类。
类声明
以下是 com.google.common.primitives.Chars 类的声明 −
@GwtCompatible(emulated = true)
public final class Chars
extends Object
Fields
| Sr.No | 字段和描述 |
|---|---|
| 1 |
static int BYTES 表示原始 char 值所需的字节数。 |
方法
| Sr.No | 方法及描述 |
|---|---|
| 1 |
static List<Character> asList(char... backingArray) 返回由指定数组支持的固定大小列表,类似于 Arrays.asList(Object[])。 |
| 2 |
static char checkedCast(long value) 如果可能,返回等于 value 的 char 值。 |
| 3 |
static int compare(char a, char b) 比较两个指定的字符值。 |
| 4 |
static char[] concat(char[]... arrays) 返回每个提供的数组中组合成单个数组的值。 |
| 5 |
static boolean contains(char[] array, char target) 如果目标作为元素出现在数组中的任何位置,则返回 true。 |
| 6 |
static char[] ensureCapacity(char[] array, int minLength, int padding) 返回一个包含与数组相同值的数组,但保证具有指定的最小长度。 |
| 7 |
static char fromByteArray(byte[] bytes) 返回 char 值,其大端表示存储在字节的前 2 个字节中; 相当于 ByteBuffer.wrap(bytes).getChar()。 |
| 8 |
static char fromBytes(byte b1, byte b2) 返回 char 值,其字节表示形式是给定的 2 个字节,按大端顺序; 等价于 Chars.fromByteArray(new byte[] {b1, b2})。 |
| 9 |
static int hashCode(char value) 返回值的哈希码; 等于调用 ((Character) value).hashCode() 的结果。 |
| 10 |
static int indexOf(char[] array, char target) 返回值目标在数组中第一次出现的索引。 |
| 11 |
static int indexOf(char[] array, char[] target) 返回数组中指定目标第一次出现的起始位置,如果没有出现,则返回 -1。 |
| 12 |
static String join(String separator, char... array) 返回一个字符串,其中包含所提供的由分隔符分隔的字符值。 |
| 13 |
static int lastIndexOf(char[] array, char target) 返回值目标在数组中最后一次出现的索引。 |
| 14 |
static Comparator<char[]> lexicographicalComparator() 返回一个比较器,按字典顺序比较两个字符数组。 |
| 15 |
static char max(char... array) 返回数组中存在的最大值。 |
| 16 |
static char min(char... array) 返回数组中存在的最小值。 |
| 17 |
static char saturatedCast(long value) 返回值最接近的字符。 |
| 18 |
static char[] toArray(Collection<Character> collection) 将字符实例集合复制到原始字符值的新数组中。 |
| 19 |
static byte[] toByteArray(char value) 返回 2 元素字节数组中值的大端表示; 相当于 ByteBuffer.allocate(2).putChar(value).array()。 |
继承的方法
该类继承了以下类的方法 −
- java.lang.Object
Example of Chars Class
使用您在 C:/> Guava 中选择的任何编辑器创建以下 java 程序。
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));
}
}
验证结果
使用javac编译器编译类,如下所示 −
C:\Guava>javac GuavaTester.java
现在运行 GuavaTester 查看结果。
C:\Guava>java GuavaTester
查看结果。
[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
guava_primitive_utilities.html

