java8--- ListMap数组互转

Posted 雪洗中关村

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java8--- ListMap数组互转相关的知识,希望对你有一定的参考价值。

 

 

import constant.Collect;  
import entity.User;  
  
import java.util.*;  
import java.util.function.Function;  
import java.util.stream.Collectors;  
import java.util.stream.Stream;  
  
public class CollectionMy {  
  
  
    public static void main(String[] args) {  
//        String[] arrays = {"hello"," , ","world"};  
//  
//        System.out.println(arrayToList1(arrays));  
//        System.out.println(arrayToList2(arrays));  
//        System.out.println(listToArray1(arrayToList2(arrays)));  
//        System.out.println(listToArray2(arrayToList2(arrays)));  
//  
//        Map<Integer, User> result = listToMap1(Collect.listEntiyChina);  
//        System.out.println(result);  
//        Map<Integer, User> result = listToMap2(Collect.listEntiyChina);  
//        System.out.println(result);  
//        sortList(Collect.listEntiyJapan);  
//  
        Map<Integer, List<User>> result = listToMap4(Collect.listEntiyJapan);  
        System.out.println(result);  
  
  
  
        Map<String,Integer> mapRepeat = new HashMap<>();  
  
    }  
  
    /** 
     * Java8 数组转为List 
     * @param arrays 
     * @return 
     */  
    public static List<String> arrayToList1(String[] arrays) {  
        List<String> result = Stream.of(arrays).collect(Collectors.toList());  
  
        return result;  
    }  
  
  
    /** 
     * Java8 List转为数组 
     * @param list 
     * @return 
     */  
    public static String[] listToArray1( List<String> list) {  
        String[] result = list.stream().toArray(String[]::new);  
        Arrays.stream(result).forEach(str -> System.err.println(str));  
        return result;  
    }  
  
    /** 
     * Java8 List转为Map 
     *  ID 必须为唯一性 
     * @param list 
     * @return 
     */  
    public static Map<Integer, User> listToMap1(List<User> list) {  
        Map<Integer, User> result = list.stream().collect(Collectors.toMap(e -> e.getId(), Function.identity()));  
        return result;  
    }  
    /** 
     * Java8 List转为Map 
     *  ID 必须为唯一性 
     * @param list 
     * @return 
     */  
    public static Map<Integer, User> listToMap2(List<User> list) {  
        Map<Integer, User> result = list.stream().collect(Collectors.toMap(User::getId, user->user));  
        return result;  
    }  
    /** 
     * Java8 List转为Map 
     *  ID重复分组 
     * @param list 
     * @return 
     */  
    public static Map<Integer, List<User>> listToMap3(List<User> list) {  
        Map<Integer, List<User>> result = list.stream().collect(Collectors.groupingBy(User::getId));  
        return result;  
    }  
  
    /** 
     * Java8 List转为Map 
     *  根据年龄排序后,再根据ID重复分组 
     * @param list 
     * @return 
     */  
    public static Map<Integer, List<User>> listToMap4(List<User> list) {  
        Collections.sort(list, Comparator.comparing(User::getId).thenComparing(User::getAge));  
        list.forEach(System.out::println);  
        System.out.println("----------------");  
        Map<Integer, List<User>> result = list.stream().collect(Collectors.groupingBy(User::getId));  
        return result;  
    }  
  
    public static void sortList(List<User> list) {  
        Collections.sort(list, Comparator.comparing(User::getId).thenComparing(User::getAge));  
        list.forEach(System.out::println);  
    }  
  
    public static List<String> arrayToList2(String[] arrays) {  
        List<String> result = Arrays.asList(arrays);  
        return result;  
    }  
  
  
    public static String[] listToArray2( List<String> list) {  
        String[] result = list.toArray(new String[list.size()]);  
        return result;  
    }  

 

—————————————— https://www.iteye.com/blog/bugyun-2433872

以上是关于java8--- ListMap数组互转的主要内容,如果未能解决你的问题,请参考以下文章

listmap数组 转换

java8语法糖List<Integer>,List<String>,String之间互转

SpringBoot——Thymeleaf中使用th:each遍历数组ListMap

SpringBoot——Thymeleaf中使用th:each遍历数组ListMap

相信我,使用 Stream 真的可以让代码更优雅!

java8 stream的用法总结