Java集合可以通过以下几种方式进行创建:
-
使用new关键字创建集合对象:
List
list = new ArrayList<>(); Set set = new HashSet<>(); Map map = new HashMap<>(); -
使用集合工具类创建集合对象:
List
list = Arrays.asList(1, 2, 3); Set set = new HashSet<>(Arrays.asList("a", "b", "c")); Map map = new HashMap<>(Map.of("key1", 1, "key2", 2)); -
使用集合工厂方法创建集合对象:
List
list = List.of(1, 2, 3); Set set = Set.of("a", "b", "c"); Map map = Map.of("key1", 1, "key2", 2);
注意:集合对象创建后,可以根据需要进行添加、删除、修改等操作。