1. 程式人生 > >java中String字串轉化成list<Integer>格式

java中String字串轉化成list<Integer>格式

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