Java learning note 27 (Collection framework I: ArrayList review, Collection interface method)

Collection: collection is a container provided in java, which can be used to store multiple data

The difference between a set and an array:

1. The length of the array is fixed and the length of the set is variable

2. The elements stored in the collection must be reference type data

 

A review of the ArrayList collection

Example:

package demo;

public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

    public Person() {
    }

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

}
package demo;

import java.util.ArrayList;

public class ArrayListDemo {
    public static void main(String[] args) {
        // ArrayList Review
        // Store basic data
        ArrayList<Integer> array = new ArrayList<>();
        array.add(1);// Automatic packing
        array.add(2);
        array.add(3);
        array.add(4);
        array.add(5);
        for (int i = 0; i < array.size(); i++) {
            System.out.println(array.get(i));
        }

        // Store custom classes Person object
        ArrayList<Person> arrayPerson = new ArrayList<Person>();
        arrayPerson.add(new Person("a", 20));
        arrayPerson.add(new Person("b", 18));
        arrayPerson.add(new Person("c", 19));
        for (int i = 0; i < arrayPerson.size(); i++) {
            System.out.println(arrayPerson.get(i));
            //Call class overridden toString Method
        }
    }
}
/*Output:
2
3
4
5
Person [name=a, age=20]
Person [name=b, age=18]
Person [name=c, age=19]
*/

 

Collection interface is the root interface of collection. Its methods should be studied through its implementation class,

List inherits Collection, and ArrayList implements the list interface. Some methods of Collection can be studied through ArrayList:

Example:

package demo;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionDemo {
    public static void main(String[] args) {
        function1();
        function2();
        function3();
    }

    public static void function1() {
        // Using polymorphism
        Collection<String> c1 = new ArrayList<String>();
        // Store element, ordered set
        c1.add("abc");
        c1.add("def");
        boolean b1 = c1.contains("abc");
        System.out.println(b1);// true,Does the collection contain this element
        c1.size();// Size of collection, number of stored elements
        c1.clear();// Empty the elements in the collection. The collection itself exists
    }

    public static void function2() {
        // Get an array of stored objects
        Collection<String> c1 = new ArrayList<String>();
        c1.add("abc");
        c1.add("def");
        c1.add("ghi");
        Object[] obj1 = c1.toArray();
        for (int i = 0; i < obj1.length; i++) {
            System.out.println(obj1[i]);
        }
    }

    public static void function3() {
        // Delete element
        Collection<String> c1 = new ArrayList<String>();
        c1.add("abc");
        c1.add("def");
        c1.add("ghi");
        boolean b1 = c1.remove("abc");
        System.out.println(b1);// true
        // Whether to delete successfully. If the element does not exist, deletion fails
        System.out.println(c1);// [def, ghi]
        // If there are duplicate elements, delete the first one
    }
}

Tags: Java

Posted on Mon, 04 May 2020 20:52:43 -0400 by mooler