org.apache.commons.lang3.StringUtils.replaceChars(String, String, String)
两个数组相等,就是意味着他们有相同的长度,相同的元素,以及相同的顺序
apache的commons-net包下面有ntp的实现.
* StringUtils.replaceChars("abcba", "bc", "yz") = "ayzya"
* StringUtils.replaceChars("abcba", "bc", "y") = "ayya"
* StringUtils.replaceChars("abcba", "bc", "yzx") = "ayzya"
public static String replaceChars(final String str, final String searchChars, String replaceChars) {
if (isEmpty(str) || isEmpty(searchChars)) {
return str;
}
if (replaceChars == null) {
replaceChars = EMPTY;
}
boolean modified = false;
final int replaceCharsLength = replaceChars.length();
final int strLength = str.length();
final StringBuilder buf = new StringBuilder(strLength);
for (int i = 0; i < strLength; i++) {
final char ch = str.charAt(i);
final int index = searchChars.indexOf(ch);
if (index >= 0) {
modified = true;
if (index < replaceCharsLength) {
buf.append(replaceChars.charAt(index));
}
} else {
buf.append(ch);
}
}
if (modified) {
return buf.toString();
}
return str;
}
比较两个List是否相等(相同元素)两个数组相等,就是意味着他们有相同的长度,相同的元素,以及相同的顺序
1. 检查是否指向同一个地址(不同引用);
2. 非空检查;
3. 长度检查;
4. 顺序和对应的元素相等检查。(依次比较,只要一个不等就返回false)
- public static boolean equals(int[] a, int[] a2) {
- if (a==a2) return true;
- if (a==null || a2==null) return false;
- int length = a.length;
- if (a2.length != length) return false;
- for (int i=0; i<length; i++)
- if (a[i] != a2[i]) return false;
- return true;
- }
- public boolean containsAll(Collection<?> c) {
- for (Object e : c)
- if (!contains(e))
- return false;
- return true;
- }
public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) {
if(a.size() != b.size()) {
return false;
}
final CardinalityHelper<Object> helper = new CardinalityHelper<Object>(a, b);
if(helper.cardinalityA.size() != helper.cardinalityB.size()) {
return false;
}
for( final Object obj : helper.cardinalityA.keySet()) {
if(helper.freqA(obj) != helper.freqB(obj)) {
return false;
}
}
return true;
}
Old impl - org.apache.commons.collections.CollectionUtils.isEqualCollection()
private static Integer INTEGER_ONE = new Integer(1); // reuse same object
- private static final Integer INTEGER_ONE = 1;
- public static boolean isEqualCollection(Collection a, Collection b){
- if (a.size() !=b.size()) { // size是最简单的相等条件
- return false;
- }
- Map mapa = getCardinalityMap(a);
- Map mapb = getCardinalityMap(b);
- // 转换map后,能去掉重复的,这时候size就是非重复项,也是先决条件
- if (mapa.size() !=mapb.size()) {
- return false;
- }
- Iterator it =mapa.keySet().iterator();
- while (it.hasNext()) {
- Object obj = it.next();
- // 查询同一个obj,首先两边都要有,而且还要校验重复个数,就是map.value
- if (getFreq(obj,mapa) != getFreq(obj, mapb)) {
- return false;
- }
- }
- return true;
- }
- /**
- * 以obj为key,可以防止重复,如果重复就value++
- * 这样实际上记录了元素以及出现的次数
- */
- public static Map getCardinalityMap(Collection coll) {
- Map count = new HashMap(); // change
- for (Iterator it =coll.iterator(); it.hasNext();) {
- Object obj =it.next();
- Integer c =(Integer) count.get(obj);
- if (c == null)
- count.put(obj, INTEGER_ONE);
- else {
- count.put(obj, newInteger(c.intValue() + 1));
- }
- }
- return count;
- }
- private static final int getFreq(Objectobj, Map freqMap) {
- Integer count =(Integer) freqMap.get(obj);
- if (count != null) {
- return count.intValue();
- }
- return 0;
- }
apache的commons-net包下面有ntp的实现.
- NTPUDPClient timeClient = new NTPUDPClient();
- String timeServerUrl = "time-a.nist.gov";
- InetAddress timeServerAddress = InetAddress.getByName(timeServerUrl);
- TimeInfo timeInfo = timeClient.getTime(timeServerAddress);
- TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- System.out.println(dateFormat.format(timeStamp.getDate()));
public static <T extends Comparable<? super T>> int compare(final T c1, final T c2, final boolean nullGreater) {
if (c1 == c2) {
return 0;
} else if (c1 == null) {
return nullGreater ? 1 : -1;
} else if (c2 == null) {
return nullGreater ? -1 : 1;
}
return c1.compareTo(c2);
}