Java Intermediate Advanced Collection Framework


Write in front

Hello everyone, today I will continue to update Java Intermediate Advanced. The reason why it is updated every other day is because I took a day to collect and organize the knowledge points to be shared. When collecting, I personally value whether the knowledge points themselves are worth sorting out, and then linking up these sorted knowledge points with a "line". First, it is convenient for oneself to remember and use; second, share with others to make others look more organized. "At the end of the article, there is a hierarchical and organized link of my original knowledge points, and the structure of the knowledge points is clearer."

Java Intermediate Advanced Collection Framework

JAVA collection framework

1. ArrayList

1.1 The difference between ArrayList and array


Limitations of arrays:

① If you want to store multiple objects, you can use arrays, but arrays have limitations. If it exceeds the length of the array, it will not fit, and the array will be wasted if it is not full.

Code:


 1//先声明一个学生类(Student.java)
 2public class Student {
 3   public String name;
 4   public Student () {
 5   }
 6// 增加一个初始化name的构造方法
 7public Student (String name) {
 8   this.name = name;
 9}
10// 重写toString方法
11public String toString() {
12   return name;
13   }
14}
15//数组的局限性,声明测试类(Test.java)
16//声明长度是5的数组
17Student Student [] = new Student [5];
18//不用的数组就浪费了
19Student [0] = new Student ("张三");
20//放不下要报错
21Student [20] = new Student ("李四");

ArrayList stores objects:

① In order to solve the limitations of arrays, the concept of container classes is introduced. The most common container class is ArrayList.

② The capacity of the ArrayList container will increase automatically with the increase of objects

③Just keep adding student objects to the container, don't worry about the boundary problem of the array.

Code:


1//容器类 ArrayList,用于存放对象
2ArrayList student = new ArrayList();
3student .add( new Student ("张三"));
4System.out.println(student .size());
5//只需要不断往容器里增加学生即可,不用担心会出现数组的边界问题。
6student .add( new Student ("李四"));
7System.out.println(student .size());

1.2 Common methods

① add "two methods"

☛Add the object directly, and add the object at the end: student.add(new Student("name"));

☛Add objects at the specified location: student.add(3, "李四");

Code:


 1ArrayList students= new ArrayList();
 2// 把5个对象加入到ArrayList中
 3for (int i = 0; i < 5; i++) {
 4studenst.add(new Student("student" + i));
 5}
 6System.out.println(studenst);
 7// 在指定位置增加对象
 8Student stu = new Student("李四");
 9studenst.add(3, stu );
10System.out.println(studenst.toString());

② contains to determine whether it exists

☛ Determine whether an object is in the container through the method contains.

☛ Judgment criteria: Whether it is the same object, not whether the name is the same.

Code:


 1ArrayList students= new ArrayList();
 2// 初始化5个对象
 3for (int i = 0; i < 5; i++) {
 4students.add(new Student("student" + i));
 5}
 6Student stu = new Student ("李四");
 7students.add(stu );
 8System.out.println(students);
 9// 判断一个对象是否在容器中
10// 判断标准: 是否是同一个对象,而不是name是否相同
11System.out.print("虽然一个新的对象名字也叫 student1,但是contains的返回是:");
12System.out.println(students.contains(new Student("student1")));
13System.out.print("而对stu 的判断,contains的返回是:");
14System.out.println(students.contains(stu));
15}

③ get Get the object at the specified location

☛Get the object at the specified position through get. If the input subscript is out of bounds, an error will be reported.

Code:


 1ArrayList students= new ArrayList();
 2// 初始化5个对象
 3for (int i = 0; i < 5; i++) {
 4students.add(new Student("student" + i));
 5}
  Studentstu= new Student("李四");
 7students.add(stu);
 8//获取指定位置的对象
 9System.out.println(students.get(5));
10//如果超出了范围,依然会报错
11System.out.println(students.get(6));

④ indexOf gets the position of the object

☛ indexOf is used to determine the position of an object in the ArrayList.

☛ Like contains, the judgment criterion is whether the objects are the same, not whether the name values ​​of the objects are equal.

Code:


 1ArrayList students= new ArrayList();
 2// 初始化5个对象
 3for (int i = 0; i < 5; i++) {
 4students.add(new Student("student" + i));
 5}
 6Student stu = new Student("李四");
 7students.add(stu );
 8System.out.println(students);
 9System.out.println("stu所处的位置:"+students.indexOf(stu));
10System.out.println("新的学生,但是名字是\"student1\"所处的位置:"+students.indexOf(new Student("student1")));

⑤ remove delete

☛ remove is used to delete objects from the ArrayList: students.remove(2);

☛ remove can delete the elements of ArrayList according to the subscript (object): students.remove(stu);

Code:


 1ArrayList students= new ArrayList();
 2// 初始化5个对象
 3for (int i = 0; i < 5; i++) {
 4students.add(new Student("student" + i));
 5}
 6Student stu= new Student("李四");
 7students.add(stu);
 8System.out.println(students);
 9students.remove(2);
10System.out.println("删除下标是2的对象");
11System.out.println(students);
12System.out.println("删除李四");
  students.remove(stu);
14System.out.println(students);

⑥ set replacement

☛ set is used to replace the element at the specified position

Code:


 1ArrayList students= new ArrayList();
 2// 初始化5个对象
 3for (int i = 0; i < 5; i++) {
 4students.add(new Student("student" + i));
 5}
 6Student stu= new Student ("李四");
 7students.add(stu);
 8System.out.println(students);
 9System.out.println("把下标是5的元素,替换为\"student5\"");
10students.set(5, new Student("student5"));
11System.out.println(students);
12}

⑦ size Get size

☛ size is used to get the size of the ArrayList

Code:


 1ArrayList students= new ArrayList();
 2// 初始化5个对象
 3for (int i = 0; i < 5; i++) {
 4students.add(new Students("student" + i));
 5}
 6Students stu= new Students("李四");
 7students.add(stu);
 8System.out.println(students);
 9System.out.println("获取ArrayList的大小:");
10System.out.println(students.size());

⑧ toArray is converted to an array

☛ toArray can convert an ArrayList object into an array.

☛ Note: If you want to convert to a Student array, you need to pass an object of the Student array type to toArray(), so that the toArray method knows which type of array you want to convert to, otherwise you can only convert to an Object array

Code:

 1ArrayList students= new ArrayList();
 2// 初始化5个对象
 3for (int i = 0; i < 5; i++) {
 4students.add(new Students("student" + i));
 5}
 6Student stu= new Student ("李四");
 7students.add(stu);
 8System.out.println(students);
 9Student st[] = (Student[])students.toArray(new Student []{});
10System.out.println("数组:" +st);

⑨ addAll adds all objects in another container

☛ addAll adds all objects in another container

Code:


 1ArrayList students= new ArrayList();
 2// 初始化5个对象
 3for (int i = 0; i < 5; i++) {
 4students.add(new Student("student" + i));
 5}
 6System.out.println("ArrayList heros:\t" + students);
 7//把另一个容器里所有的元素,都加入到该容器里来
 8ArrayList anotherstudents = new ArrayList( );
 9anotherstudents .add(new Student("student a"));
10anotherstudents .add(new Student("student b"));
11anotherstudents .add(new Student("student c"));
12System.out.println("anotherstudents students:\t" + anotherstudents );
13students.addAll(anotherstudents );
14System.out.println("把另一个ArrayList的元素都加入到当前 ArrayList:");
15System.out.println("ArrayList students:\t" + students);

⑩ clear

☛ clear Clear an ArrayList

Code:


1ArrayList students= new ArrayList();
2// 初始化5个对象
3for (int i = 0; i < 5; i++) {
4students.add(new Student("student" + i));
5}
6System.out.println("ArrayList students:\t" + students);
7System.out.println("使用clear清空");
8students.clear();
9System.out.println("ArrayList students:\t" + students);

1.3 List interface

ArrayList 和 List:

① ArrayList implements the interface List.

② The common way of writing will declare the reference as the interface List type.

③ Note: It is java.util.List, not java.awt.List.

Code:


 1import java.util.ArrayList;
 2import java.util.List;
 3import charactor.Student;
 4public class TestCollection {
 5public static void main(String[] args) {
 6//接口引用指向子类对象(多态)
 7List students = new ArrayList();
 8students .add( new Student("李四"));
 9System.out.println(students .size());
10}
11}

Methods of the List interface:

① Because ArrayList implements the List interface, the methods of the List interface ArrayList are all implemented.

② There is a detailed explanation in the chapter of ArrayList common methods

1.4 List interface

Generic:

① Don't specify a generic container, you can store any type of element

② A generic container is specified, which can only store elements of the specified type and its subclasses

Shorthand for generic:

① In order not to make the compiler warn, you need to use generics before and after

1List<Student> gen = new ArrayList<Student>();
② However, JDK7 provides a generic abbreviation that can slightly reduce the amount of code

1List<Student> gen = new ArrayList<>();

1.5 Traverse

Traverse with a for loop:

① Through the previous study, I know that you can use size() and get() to get the size and the element at the specified position respectively, combined with the for loop, you can traverse the contents of the ArrayList

Code:


 1List<Student> students= new ArrayList<Student>();
 2// 放5个Student进入容器
 3for (int i = 0; i < 5; i++) {
 4students.add(new Student("student name " + i));
 5}
 6// 第一种遍历 for循环
 7System.out.println("--------for 循环-------");
 8for (int i = 0; i < students.size(); i++) {
 9Student s = students.get(i);
10System.out.println(s);

Iterator traversal:

① Use Iterator to traverse the elements in the collection

Code:


 1List<Student> students= new ArrayList<Student>();
 2//放5个students进入容器
 3for (int i = 0; i < 5; i++) {
 4students.add(new students("student name " +i));
 5}
 6//第二种遍历,使用迭代器
 7System.out.println("--------使用while的iterator-------");
 8Iterator<Student> it= students.iterator();
 9//从最开始的位置判断"下一个"位置是否有数据
10//如果有就通过next取出来,并且把指针向下移动
11//直达"下一个"位置没有数据
12while(it.hasNext()){
13Student s = it.next();
14System.out.println(s);
15}
16//迭代器的for写法
17System.out.println("--------使用for的iterator-------");
18for (Iterator<Student> iterator = students.iterator(); iterator.hasNext();) {
19Student student= (Student ) iterator.next();
20System.out.println(student);
21}

Use enhanced for loop:

① It is very convenient to use the enhanced for loop to traverse the elements in ArrayList, which is the first choice of many developers.

② However, the enhanced for loop also has its shortcomings: it cannot be used to initialize the ArrayList, and it is impossible to know which element it is currently. When only singular elements need to be printed, it cannot be done. The subscript variable must be customized again.

Code:


 1List<Student> students= new ArrayList<Student>();
 2// 放5个Student进入容器
 3for (int i = 0; i < 5; i++) {
 4students.add(new Student("student name " + i));
 5}
 6// 第三种,增强型for循环
 7System.out.println("--------增强型for循环-------");
 8for (Student s : students) {
 9System.out.println(s);
10}

Guess you like

Origin blog.51cto.com/15064450/2602791