一些算法,1、查询手机号归属地。2、 网宿手机号码归属地查询接口结果存本地库。3、 流量套餐排序算法——比较器
import .*package
public class Classic
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
/**
*使用手机号码归属地接口,查询手机号码归属地。
*
*/
private static List<String> printlnispno(String[] phones) {
List<String> ispnos = new LinkedList<String>();
for (int i = 0; i < phones.length; i++) {
ispnos.add(queryispno(phones[i]).get("supplier"));
}
return ispnos;
}
private static Map<String, String> queryispno(String phone) {
Map<String, String> paramMap = new HashMap<String, String>();
try {
//此接口orderUrl归属坚石诚信科技有限公司,请勿在未授权的情况下使用
String orderUrl = "http://jsphone.janshcrp.com/phonenum/phonenum/phonenum?phone=" + phone;
String s1 = HttpClientUtil.httpPost(orderUrl, new HttpClientRequest());
paramMap = JsonUtil.readMapString(JsonUtil.obj2json(JsonUtil.readMapObject(s1).get("data")));
} catch (Exception e) {
e.printStackTrace();
}
return paramMap;
}
/**
* nie
* @param s1
* @param s2
* @return
* 网宿手机号码归属地查询接口结果存本地库
* 主要处理返回结果中省份和城市的切割问题
* 接口链接
* http://jsphone.janshcrp.com/phonenum/phonenum/phonenum?phone=13034199557
* 核心算法如下
*/
public static String[] subProviceCity(String str1, String str2) {
char[] stringArr1 = str1.toCharArray();
char[] stringArr2 = str2.toCharArray();
int l1 = stringArr1.length;
int l2 = stringArr2.length;
int len = l1 - 2 > l2 ? l2 : l1 - 2;
char[] stringArr = new char[len];
int i = 0;
for (; i < len; i++) {
if (stringArr2[i] == stringArr1[i]) {
stringArr[i] = stringArr2[i];
} else {
break;
}
}
return new String[] { String.valueOf(stringArr).trim(), str1.substring(i) };
}
/**
* 流量套餐排序算法
* 比较器
* @author gll
*
*/
/**
public class MyCompartor implements Comparator<String> {}
*/
public int compare(String s1, String s2) {
String s1dw = s1.substring(s1.length() - 1);
String s2dw = s2.substring(s2.length() - 1);
if (s1dw.compareTo(s2dw) == 0) {
int length = s1.length() > s2.length() ? s1.length() : s2.length();
if (StringUtils.leftPad(s1, length, "0").compareTo(StringUtils.leftPad(s2, length, "0")) > 0) {
return 1;
} else {
return -1;
}
} else if (s1dw.compareTo(s2dw) > 0) {
return -1;
} else {
return 1;
}
}
}