集合的体系
Collection的结构体系
- List系列集合:添加的元素是
有序的、可重复、有索引
。 - Set系列集合:无序、不重复、无索引
- HashSet:无序、不重复、无索引
- LinkedHashSet:
有序
、不重复、无索引 - TreeSet:
按照大小默认升序排序
、不重复、无索引
Collection常见API
Collection<String> collection = new ArrayList<>();
boolean add = collection.add("Java");
collection.add("Python");
System.out.println(collection);
boolean remove = collection.remove("Java");
System.out.println(collection);
boolean contains = collection.contains("Python");
System.out.println(contains);
int size = collection.size();
System.out.println(size);
collection.clear();
System.out.println(collection);
boolean empty = collection.isEmpty();
System.out.println(empty);
Object[] objects = collection.toArray();
System.out.println(Arrays.toString(objects));
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
ArrayList<String> list2 = new ArrayList<>();
list2.add("Java");
list2.add("Python");
list2.add("C++");
list.addAll(list2);
System.out.println(list);