【工作】Guava开发必备代码库

JavaGuava

# Joiner & Splitter

// url路径参数解析
Map<String, String> join = Splitter.on("&").withKeyValueSeparator("=").split("id=1&name=lcs");
String split = Joiner.on("&").withKeyValueSeparator("=").join(join);
1
2
3
// skipNulls会过滤掉空的参数,包括数组中的null值。
Joiner.on("|").skipNulls().join(stringList);
// usefornull会以参数替换空的参数,包括数组中的null值。
Joiner.on("|").useForNull("no value").join(stringList);
1
2
3
4
// join参数可以很复杂
String result = Joiner.on("_").join("user_common_count", "name", new Object[]{"nick","adidas",10000})
1
2

# MD5

public class HashUtils {
	private static final HashFunction FUNCTION = Hashing.md5();
	private static final String SALT = "abcd";

	public static String encryPassword(String password){
	   HashCode hashCode =	FUNCTION.hashString(password + SALT, Charset.forName("UTF-8"));
	   return hashCode.toString();
	}
}
1
2
3
4
5
6
7
8
9

# Cache

private final Cache<String, String> cache =
    CacheBuilder.newBuilder().maximumSize(100).expireAfterAccess(15, TimeUnit.MINUTES)
        .removalListener(new RemovalListener<String, String>() {

          @Override
          public void onRemoval(RemovalNotification<String, String> notification) {
            ...
          }
        }).build();
1
2
3
4
5
6
7
8
9

# File

String text = CharStreams.toString(new InputStreamReader(new FileInputStream("d:/sample.txt"), "UTF-8"));
1