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);
注意:集合对象创建后,可以根据需要进行添加、删除、修改等操作。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/823783.html