将字符串或数字倒序输出,以使这些呈散列分布,用于作为hbase rowkey的一部分,避免region的读写热点
public class StringUtil {
public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
System.out.println(reverseLong(78945612399l));
}
System.out.println(System.currentTimeMillis() - start);
}
/**
* 将一个数字倒序输出, 先将数字转换为字符串,然后利用reverse函数
*
* @param num
* @return
*/
public static String reverseNumberByStr(long num) {
StringBuffer res = new StringBuffer(Long.toString(num));
res.reverse();
return res.toString();
}
/**
* 将一个long型数字倒序输出
*
* @param n
* @return
*/
public static long reverseLong(long n) {
long reverse = 0;
long part = 0;
while (n > 0) {
part = n % 10;
reverse = reverse * 10 + part;
n /= 10;
}
return reverse;
}
/**
* 将字符串倒序输出,使用reverse函数,效率很高
*
* @param str
* @return
*/
public static String reverseString(String str) {
StringBuffer res = new StringBuffer(str);
res.reverse();
return res.toString();
}
/**
* 将字符串倒序输出,使用charAt,效率不及reverseString
*
* @param str
* @return
*/
public static String reverseStrByCharAt(String str) {
String res = "";
for (int i = str.length() - 1; i > -1; i--) {
res += str.charAt(i);
}
return res;
}
}