栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

看完这篇文章,再也不怕别人问你数组转list,list转map

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

看完这篇文章,再也不怕别人问你数组转list,list转map

一、导读

本文主要内容包括数组转换成List,List转成数组,List转成map的几种方式。

好文章 记得 收藏+点赞+关注 !!!

二、代码

public class TransformationTest {
    public static void main(String[] args) {
        //数组转list 方式一
        String[] s = new String[]{"hello", "java"};
        List list = Arrays.asList(s);
        System.out.println(list);
        System.out.println("-------------------------------");
        //list.add("world");这种方式转换的list 不能对List增删,只能查改,会报错UnsupportedOperationException



为什么会这样?
因为Arrays.asList(s)返回值是java.util.Arrays类中一个私有静态内部类java.util.Arrays.ArrayList,它并非java.util.ArrayList类。
java.util.Arrays.ArrayList类具有 set(),get(),contains()等方法,但是不具有添加add()或删除remove()方法,所以调用add()方法会报错。

        //数组转list 方式二   通过集合工具类Collections.addAll()方法
        ArrayList arrayList = new ArrayList<>(s.length);
        Collections.addAll(arrayList, s);
        arrayList.add("world");
        System.out.println(arrayList);
        System.out.println("-------------------------------");

        //list转数组 方式一
        String[] strings = new String[arrayList.size()];
        arrayList.toArray(strings);
        System.out.println(Arrays.toString(strings));
        System.out.println("-------------------------------");
        //list转数组 方式二   支持泛型的toArray方法
        String[] ss = arrayList.toArray(new String[arrayList.size()]);
        System.out.println("转换完的数组为:" + Arrays.toString(ss));


List转Map:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {

    
    public Integer id;

    
    public String name;
}
        //新建一个ArrayList存放学生对象
        ArrayList studentList = new ArrayList<>();
        studentList.add(new Student(1, "佳明"));
        studentList.add(new Student(2, "张伟"));
        studentList.add(new Student(3, "张三"));

        //list转map 方式一
        Map studentMap = new HashMap<>();
        for (Student student : studentList) {
            studentMap.put(student.getId(), student);
        }
        //遍历输出studentMap
        studentMap.forEach((K, V) -> System.out.println(K + "," + V));

        //list转map 方式二
        //编号作为key,名字作为value
        //(k1, k2) -> k2)作用是 指定key覆盖规则  防止hash冲突
        Map studentMap2 = studentList.stream().collect(Collectors.toMap(Student::getId, Student::getName, (k1, k2) -> k2));
        //遍历输出studentMap
        studentMap2.forEach((K, V) -> System.out.println(K + "," + V));
        System.out.println("-------------------------------");

        //编号作为key,对象作为value
        Map studentMap3 = studentList.stream().collect(Collectors.toMap(Student::getId, Function.identity(), (k1, k2) -> k2));
        studentMap3.forEach((K, V) -> System.out.println(K + "," + V));
        System.out.println("-------------------------------");

        //list转map 方式三 使用第三方库——Guava库
        convertListToMapByGuava(studentList).forEach((K, V) -> System.out.println(K + "," + V));
    }

    public static Map convertListToMapByGuava(List studentList) {
        return Maps.uniqueIndex(studentList, Student::getId);
    }

}

OK,表演到此结束!


推荐阅读:

  • 由银行填表时返回上一步引发的对回溯算法的思考(java实现回溯算法)
  • 上班摸鱼时引发的对多线程的思考
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号