字节数组(byte[])转16进制字符串(HexString)

public static String bytesToHexString(byte[] bytes) {
    StringBuffer stringBuffer = new StringBuffer();
    String temp = null;
    for (int i = 0; i < bytes.length; i++) {
        temp = Integer.toHexString(bytes[i] & 0xFF);
        if (temp.length() == 1) {
            stringBuffer.append("0");
        }
        stringBuffer.append(temp);
    }
    return stringBuffer.toString().toUpperCase();
}

参考链接:

关于byte[ ] & 0xFF的问题

继续阅读字节数组(byte[])转16进制字符串(HexString)

【转】Android遍历Map的两种常用方法

转载自:android遍历map的两种常用方法

Map一般用来保存具有映射关系的数据,Map里保存着两组数据:key(键)和value(值),它们可以是任何引用类型的数据,但key不能重复。所以通过指定的key就可以取出对应的value。

遍历方式一、当键和值都需要用到时所采用的方法。

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
}

遍历方式二、当只使用键或者值时所采用的方法。

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//遍历map中的键
for (Integer key : map.keySet()) {
  System.out.println("Key = " + key);
}
//遍历map中的值
for (Integer value : map.values()) {
  System.out.println("Value = " + value);
}

根据具体的使用场景选择特定的遍历方法可以加快map的查找速度。

JAVA中正则表达式匹配IP地址的写法

private boolean isIP(String ip) {
    if (ip == null || "".equals(ip)) {
        return false;
    }
    String regex = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)\\.){3}"
            + "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)$";
    return ip.matches(regex);
}

参考链接:

Java中常用的正则表达式判断,如IP地址、电话号码、邮箱等

java中正则表达式匹配ip地址的写法