JAVA单排日记-2020/1/7-JDK9对集合的优化

在这里插入图片描述

import java.util.List;
import java.util.Map;
import java.util.Set;

public class DemoJDK9 {
    public static void main(String[] args) {
        Set<Integer> one = Set.of(1,2,3,4);
        System.out.println(one);

        List<Integer> two = List.of(1,2,3,4);
        System.out.println(two);

        Map<String,Integer> three = Map.of("a",1,"b",2,"c",3);
        System.out.println(three);
    }
}

在这里插入图片描述

异常

import java.util.List;
import java.util.Map;
import java.util.Set;

public class DemoJDK9 {
    public static void main(String[] args) {
        Set<Integer> one = Set.of(1, 2, 3, 1);
        元素重复 IllegalArgumentException

        List<Integer> two = List.of(1, 2, 3, 1);
        two.add(6);
        返回值不能改 UnsupportedOperationException

        Map<String, Integer> three = Map.of("a", 1, "a", 2, "c", 3);
        元素重复 IllegalArgumentException

    }
}

发布了90 篇原创文章 · 获赞 1 · 访问量 2050

猜你喜欢

转载自blog.csdn.net/wangzilong1995/article/details/103882498