Arrays.asList(T… a)

初始化一个内部实现的ArrayList,此对象不支持修改,即不支持修改,删除和增加功能。

  1. import org.apache.commons.lang.StringUtils;
  2. List<SysUser> list = userService.list(null);
  3. //List转Array[Object] 不推荐
  4. Object[] objects = list.toArray();
  5. System.out.println("objects----:" + Arrays.toString(objects));
  6. //List转Array[E] 推荐使用 数组长度可设置为list.size()
  7. SysUser[] sysUsers = list.toArray(new SysUser[0]);
  8. System.out.println("sysUsers----:" + Arrays.toString(sysUsers));
  9. //使用流处理方式获得Array
  10. Integer[] integers = list.stream().map(SysUser::getId).toArray(Integer[]::new);
  11. System.out.println("integers----:" + Arrays.toString(integers));
  12. //Arrays.asList(sysUsers) 返回一个受指定数组支持的固定大小的列表.所以不能做 Add Remove 等操作
  13. //结合运用ArrayList的构造方法是目前来说最完美的作法,代码简洁,效率高:
  14. ArrayList<SysUser> sysUsers1 = new ArrayList<>(Arrays.asList(sysUsers));
  15. System.out.println("sysUsers1----:" + sysUsers1.toString());
  16. String join = StringUtils.join(list, ",");
  17. System.out.println("join----:" + join);
  18. //StringUtils中的拼接方法
  19. String join1 = StringUtils.join(integers, ",");
  20. System.out.println("join1----:" + join1);
  1. [
  2. {
  3. "address": "",
  4. "birthday": "2019-07-12",
  5. "createTime": "2019-07-10T14:41:33",
  6. "delFlag": "删除",
  7. "id": 1,
  8. "sex": "女人",
  9. "updateTime": "2019-07-14T19:36:57",
  10. "userName": "王五"
  11. },
  12. {
  13. "address": "北京市",
  14. "birthday": "2014-07-10",
  15. "createTime": "2019-07-10T14:41:33",
  16. "delFlag": "正常",
  17. "id": 2,
  18. "sex": "男人",
  19. "updateTime": "2019-07-13T00:26:35",
  20. "userName": "张三"
  21. },
  22. {
  23. "address": "河南郑州",
  24. "birthday": "2019-07-12",
  25. "createTime": "2019-07-10T14:41:33",
  26. "delFlag": "正常",
  27. "id": 3,
  28. "sex": "男人",
  29. "updateTime": "2019-07-14T19:36:58",
  30. "userName": "张小明"
  31. }
  32. ]