java中String字符串轉化成list格式

最近開發中遇到問題,同事在傳給我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"



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章