Java集合可以通過(guò)以下幾種方式進(jìn)行創(chuàng)建:
使用new關(guān)鍵字創(chuàng)建集合對(duì)象:
List<Integer> list = new ArrayList<>();
Set<String> set = new HashSet<>();
Map<String, Integer> map = new HashMap<>();
使用集合工具類創(chuàng)建集合對(duì)象:
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));
使用集合工廠方法創(chuàng)建集合對(duì)象:
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);
注意:集合對(duì)象創(chuàng)建后,可以根據(jù)需要進(jìn)行添加、刪除、修改等操作。