方法 一,把数组for 下一个转
String[] yq1012= {"123", "234", "345"};
Long[] num = new Long[yq1012.length];
for (int idx = 0; idx < yq1012.length; idx++) {
num[idx] = Long.parseLong(yq1012[idx]);
}
方法 二,
public static void main(String[] args) {
String stringArray[] = { "1", "2", "3", "4", "5", "6", "7", "a" };
long time1 = System.currentTimeMillis();
stringToLong(stringArray);
long time2 = System.currentTimeMillis();
System.out.println("stringToLong:" + (time2 - time1));
long time3 = System.currentTimeMillis();
stringToLong_2(stringArray);
long time4 = System.currentTimeMillis();
System.out.println("stringToLong_2:" + (time4 - time3));
}
public static long[] stringToLong(String stringArray[]) {
if (stringArray == null || stringArray.length < 1) {
return null;
}
long longArray[] = new long[stringArray.length];
for (int i = 0; i < longArray.length; i++) {
try {
longArray[i] = Long.valueOf(stringArray[i]);
} catch (NumberFormatException e) {
longArray[i] = 0;
continue;
}
}
return longArray;
}
public static long[] stringToLong_2(String stringArray[]) {
if (stringArray == null)
return null;
return (long[]) ConvertUtils.convert(stringArray, long.class);
}
方法 三
Set<Long> idsSet = new HashSet<Long>();
//过滤没有详情的id
for(Entry<Long, UserProfile> entry : userProfileMap.entrySet()){
UserProfile userProfile = entry.getValue();
if (userProfile == null) {
continue;
}
idsSet.add(userProfile.getUserId());
}
Long[] idsLong = idsSet.toArray(new Long[0]);
int idsLen = idsLong.length;
long[] idslong = new long[idsLen];
for(int i = 0; i < idsLen; i++){
idslong[i] = idsLong[i];
}