构建不可变的 Map
ImmutableMap<String, Integer> immutableMap = ImmutableMap.of("k1", 1, "k2", 2);
ImmutableMap<String, Integer> immutableMap2 = new ImmutableMap.Builder<String, Integer>().put("k1", 1).put("k2", 2).build();
List 转换为 List 或 List
final Iterable<Integer> ages = Ints.stringConverter().convertAll(Splitter.on(',').trimResults().omitEmptyStrings().splitToList(ageArr));
Longs.stringConverter()...
Floats.stringConverter()...
Shorts.stringConverter()...
Doubles.stringConverter()...
获取 list 的前 N 个元素
List<Integer> list = FluentIterable.from(p.getList()).limit(2).toList();
分割 string
Splitter.on(',').trimResults().omitEmptyStrings().splitToList("your string")
或
Splitter.on(CharMatcher.anyOf(",;")).trimResults().splitToList("your string");
List to String
Joiner.on(",").skipNulls().join(your list)
Cache
public static final Cache<Integer, Boolean> cache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, TimeUnit.SECONDS).build();
或
LoadingCache<Integer, Boolean> loaddingCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, TimeUnit.SECONDS).build(new CacheLoader...)
Range
// >=0 <= 18
Range range = Range.closed(0, 18);
// 根据自己的情况来处理即可. closed 表示是闭区间, closeOpen 表示是 (] 区间.
//判断
Range range = Range.closed(0, 18);
for (int i = 0; i < 20; i++) {
System.out.println(range.contains(i) + ", 是否包含 " + i);
}
字符串处理
//只保留数字
String d = CharMatcher.digit().retainFrom("123412helloworld");
//删除数字
String d = CharMatcher.digit().removeFrom("123412helloworld");