使用 lambda 表达式进行集合类型转换 您所在的位置:网站首页 java8数组转list 使用 lambda 表达式进行集合类型转换

使用 lambda 表达式进行集合类型转换

2023-07-07 22:08| 来源: 网络整理| 查看: 265

目标类型 List转 ListList list1 = Lists.newArrayList(); List list2 = list1.stream().map(string -> { return "stream().map()处理之后:" + string; }).collect(Collectors.toList()); 转 Map

转换属性 Map,值为属性

List allList = Lists.newArrayList(); Map map = allList.stream(). collect(Collectors.toMap(SystemTabMenu::getParentid, SystemTabMenu::getParentid));

转换属性 Map,值为对象

List allList = Lists.newArrayList(); Map map = allList.stream(). collect(Collectors.toMap(SystemTabMenu::getParentid, v -> v));

分组汇总

List allList = Lists.newArrayList(); Map resultMap = allList.stream(). collect(Collectors.groupingBy(UploadDataStatistics::getDataType, Collectors.summingInt(UploadDataStatistics::getSuccessnum))); 转 MapList allList = Lists.newArrayList(); Map map = allList.stream(). collect(Collectors.groupingBy(e -> e.getParentid())); 转数组 Integer []List list = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); Integer[] integers = list.toArray(new Integer[0]); 转数组 int []List list = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); int[] arr1 = list.stream().mapToInt(Integer::valueOf).toArray();

想要转换成 int [] 类型,就得先转成 IntStream。

通过 mapToInt () 把 Stream 调用 Integer::valueOf 来转成 IntStream

通过 IntStream 中默认 toArray () 转成 int []。

过滤出一个元素List users = Lists.newArrayList(); User match = users.stream().filter((user) -> user.getId() == 1).findAny().get(); 求最小值int min = list.stream().mapToInt(t->t).min().getAsInt(); 转拼接字符串List list = Arrays.asList(0, 1, 2); String ids = list.stream().map(String::valueOf).collect(Collectors.joining(",")); 目标类型 Map转 Listmap.entrySet().stream().map(e -> e.getValue() + e.getKey()).collect(Collectors.toList()); 取 Value 最大的 KeyMap map = new HashMap(); String maxKey = map.entrySet() .stream() .sorted(Map.Entry.comparingByValue((t1, t2) -> t2 - t1)) .map(e -> e.getKey()).collect(Collectors.toList()).get(0); 收集 MapMap map = new TreeMap(); map.computeIfAbsent(key, k -> new ArrayList()); map.get(key).add(value); 转拼接字符串Map infoIds = new TreeMap(); String ids = infoIds.keySet().stream().map(String::valueOf).collect(Collectors.joining(",")); 目标类型数组截取数组int[] data = {0, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; int[] newData = Arrays.copyOfRange(data,2,7); // {2,3,4,5,6} 转 List

Integer [] 转 List:

List list = Arrays.asList(integers1);

int [] 转 List

int[] data = {0, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; List list = Arrays.stream(data).boxed().collect(Collectors.toList()); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Arrays.stream (arr) 可以替换成 IntStream.of (arr)。

使用 Arrays.stream 将 int [] 转换成 IntStream。 使用 IntStream 中的 boxed () 装箱。将 IntStream 转换成 Stream。 使用 Stream 的 collect (),将 Stream 转换成 List,因此正是 List。 转包装类数组

int [] 转 Integer []

int[] data = {0, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; Integer[] integers1 = Arrays.stream(data).boxed().toArray(Integer[]::new); // {0, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }

Arrays.stream (arr) 可以替换成 IntStream.of (arr)。

使用 Arrays.stream 将 int [] 转换成 IntStream。 使用 IntStream 中的 boxed () 装箱。将 IntStream 转换成 Stream。 使用 Stream 的 toArray,传入 IntFunction generator。这样就可以返回 Integer 数组。不然默认是 Object []。


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有