最近開發中遇到問題,同事在傳給我ids時拼接爲String字符串格式,轉化成List,網上的轉化大致爲:
String[] strs = {"1","3","12","33"};
List<String> sList = Arrays.asList(strs);
而我要的是轉化後爲List<Integer>格式,網上的資料也很難找到關於直接轉化爲我需要格式的例子,最終還是圓滿的解決了個人的需求,直接上代碼:
List<String> idsStringList = Arrays.asList(ids.split(","));
List<Integer> idsList = new ArrayList<>();
CollectionUtils.collect(idsStringList, new Transformer() {
@Override
public Object transform(Object o) {
return Integer.valueOf(o.toString());
}
}, idsList);
ids="1,2,33,45,67"