java-List与List相互转换

需求:

现在有一个List<Integer>集合,需要把它转换成List<Long>集合。 声明:不想使用循环

实现方式

使用com.alibaba.fastjson包下的 JSONArray类实现

代码示例

public static void main(String[] args) {
          
   
        List<Integer> listInt = new ArrayList<>();
        listInt.add(1);
        listInt.add(2);
        listInt.add(3);
        List<Long> listLong = JSONArray.parseArray(listInt.toString(),Long.class);
        System.out.println(listLong);
}

同理,使用该方式List<Long>转List<Integer>也是OK的。

经验分享 程序员 微信小程序 职场和发展