Java集合可以通过以下几种方式进行创建:
使用new关键字创建集合对象:
List<Integer> list = new ArrayList<>();
Set<String> set = new HashSet<>();
Map<String, Integer> map = new HashMap<>();
使用集合工具类创建集合对象:
List<Integer> list = Arrays.asList(1, 2, 3);
Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c"));
Map<String, Integer> map = new HashMap<>(Map.of("key1", 1, "key2", 2));
使用集合工厂方法创建集合对象:
List<Integer> list = List.of(1, 2, 3);
Set<String> set = Set.of("a", "b", "c");
Map<String, Integer> map = Map.of("key1", 1, "key2", 2);
注意:集合对象创建后,可以根据需要进行添加、删除、修改等操作。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:java如何创建map集合