Java - Collection Framework List

  • The Concept of Sets

In real life: a lot of things come together

Sets in Mathematics: the sum of things with common attributes

Collection classes in Java: A tool class, like a container, that stores any number of objects with common properties

  • The Role of Sets
  1. Inside the class, organize the data (put them into a collection for properties that have the same function and meaning)
  2. Simple and fast search for large numbers of entries
  3. Some collection interfaces provide a series of orderly elements, and can quickly insert or delete related elements in the sequence.
  4. Some set interfaces provide mapping relationships, and can quickly find the corresponding unique object by key word, which can be of any type.
  • Compared with arrays
  1. Array length is fixed, set length is variable
  2. Arrays can only access elements by subscripts, which are integer and fixed in type, while some collections can find specific objects mapped by any type (subscripts).
  • Java Collection Framework Architecture

Collection and Map interfaces are two root interfaces.

Collection has three sub-interfaces: List (sequence), Queue (queue) and Set (set). The elements stored in List and Queue are orderly and repeatable, and the elements stored in Set are disorderly and non-repeatable. List and Set are commonly used in these three sub-interfaces. List has a very common and important implementation class ArrayList (array sequence), Queue has a very important implementation class LinkedList (linked list), LinkedList class is also an implementation class of List interface, Set also has a very important implementation class HashSet (hash set).

Map interface also has many sub-interfaces, commonly used is its implementation class, which has a very important implementation class HashMap (hash table).

Stored in Collection is a separate object. Within a Map, data is stored by two objects, one Key and one Value, as one mapping. Such a mapping is an instance of the Entry class. The Entry class (key-value pair) is an internal class of the Map, and the Key and Value can be any type of object.

  

  • Collection interface, sub-interface and implementation class

Collection interface is the parent interface of List, Queue and Set interfaces; it defines operations that can be used to manipulate addition, deletion and modification checks of List, Set and Queue.

  1. List Interface and Its Implementation Class-ArrayList

List is an orderly and repeatable set of elements, called a sequence; it can precisely control the insertion position of each element, or delete a location element; ArrayList is an array sequence, and the bottom layer is implemented by an array.

Taking students'course selection as an example, this paper introduces the use of List:

1.1 Add

  Course.java

package com.test.collection;

/**
 * Courses
 * @author Administrator
 *
 */
public class Course {
    public String id;
    public String name;
    public Course(String id, String name){
        this.id = id;
        this.name = name;
    }
}

  Student.java

package com.test.collection;

import java.util.HashSet;
import java.util.Set;
/**
 * Student category
 * @author Administrator
 *
 */
public class Student {
    public String id;
    public String name;
    public Set courses;//Selected courses
    public Student(String id, String name) {
        this.id = id;
        this.name = name;
        this.courses = new HashSet();//instantiation sourses(Set It's an interface, which can't be instantiated directly.
    }
}

  ListTest.java

package com.test.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Alternative courses
 * @author Administrator
 *
 */
public class ListTest {
    /**
     * List for storing alternative courses
     */
    public List coursesToSelect;
    public ListTest() {
        this.coursesToSelect = new ArrayList();
    }
    
    /**
     * Adding alternative courses to coursesToSelect
     */
    public void testAdd() {
        Course c1 = new Course("1", "data structure");//Creating Examples of Course Objects
        coursesToSelect.add(c1);//call add Method, add courses to alternative courses List in
        Course temp = (Course) coursesToSelect.get(0);//Objects stored in collections become Object,Ignore the type before the object, and type conversion is needed when extracting it.
        System.out.println("Courses were added:" + temp.id + ":" + temp.name);
        
        Course c2 = new Course("2", "C language");
        coursesToSelect.add(0, c2);//List Another overload add()Method to specify where elements are added
        Course temp2 = (Course) coursesToSelect.get(0);
        System.out.println("Courses were added:" + temp2.id + ":" + temp2.name);
        
        coursesToSelect.add(c1);//List Medium elements can be repeated
        Course temp0 = (Course) coursesToSelect.get(2);
        System.out.println("Courses were added:" + temp0.id + ":" + temp0.name);
        
        //The following method throws an array subscript crossover exception
        //Course c3 = new Course("3", "Java");
        //coursesToSelect.add(4, c3);
        
        Course[] course = {new Course("3", "discrete mathematics"), new Course("4", "assembly language")};
        coursesToSelect.addAll(Arrays.asList(course));
        Course temp3 = (Course) coursesToSelect.get(3);
        Course temp4 = (Course) coursesToSelect.get(4);
        System.out.println("Two courses were added:" + temp3.id + ":" + temp3.name + 
                temp4.id + ":" + temp4.name);
        
        Course[] course2 = {new Course("5", "Advanced mathematics"), new Course("6", "computer network")};
        coursesToSelect.addAll(2, Arrays.asList(course2));
        Course temp5 = (Course) coursesToSelect.get(2);
        Course temp6 = (Course) coursesToSelect.get(3);
        System.out.println("Two courses were added:" + temp5.id + ":" + temp5.name + 
                temp6.id + ":" + temp6.name);
        
    }
    
    public static void main(String[] args) {
        ListTest lt = new ListTest();
        lt.testAdd();
    }
}

Implementation results:

Additional Courses: 1: Data Structure
Additional Courses: 2:C Language
Additional Courses: 1: Data Structure
Two courses were added: 3: Discrete Mathematics 4: Assembly Language
Two courses were added: 5: Advanced Mathematics 6: Computer Network

1.2 Query

package com.test.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Alternative courses
 * @author Administrator
 *
 */
public class ListTest {
    /**
     * List for storing alternative courses
     */
    public List coursesToSelect;
    public ListTest() {
        this.coursesToSelect = new ArrayList();
    }
    
    /**
     * Method of obtaining elements in List
     */
    public void testGet() {
        int size = coursesToSelect.size();
        System.out.println("The following courses are available:");
        for (int i = 0; i < size; i++) {
            Course c = (Course) coursesToSelect.get(i);
            System.out.println("Courses:" + c.id + ":" + c.name);
        }
    }
    
    /**
     * Accessing Collection Elements through Iterators
     */
    public void testIterator() {
        Iterator it = coursesToSelect.iterator();
        System.out.println("There are the following course choices(Access through iterators): ");
        while (it.hasNext()) {
            Course c = (Course) it.next();
            System.out.println("Courses:" + c.id + ":" + c.name);
        }
    }
    
    /**
     * Accessing collection elements through the for each method
     */
    public void testForEach() {
        System.out.println("There are the following course choices(adopt for each Visit): ");
        for (Object obj : coursesToSelect) {
            Course c = (Course) obj;
            System.out.println("Courses:" + c.id + ":" + c.name);
        }
    }
    
    public static void main(String[] args) {
        ListTest lt = new ListTest();
        lt.testGet();
        lt.testIterator();
        lt.testForEach();
    }
}

Implementation results:

The following courses are available:
Course: 2:C Language
Course: 1: Data Structure
Course: 5: Advanced Mathematics
Course: 6: Computer Network
Course: 1: Data Structure
Course: 3: Discrete Mathematics
Course: 4: Assembly Language

1.3 Amendment

public class ListTest {public void testModify() {
        Course c = new Course("7", "Mao Yuan");
        coursesToSelect.set(4, c);
    }
}

After the revision, the following courses can be selected:
Course: 2:C Language
Course: 1: Data Structure
Course: 5: Advanced Mathematics
Course: 6: Network Technology
Course: 7: Summary
Course: 3: Discrete Mathematics
Course: 4: Assembly Language

1.4 Delete

public class ListTest {public void testDelete() {
        /*coursesToSelect.remove(4);*/
        /*Course c = (Course) coursesToSelect.get(4);
        coursesToSelect.remove(c); */
        Course[] courses = {(Course) coursesToSelect.get(4), (Course) coursesToSelect.get(5)};
        coursesToSelect.removeAll(Arrays.asList(courses));
        System.out.println("Delete the course");
    }
}

After deletion, the following courses can be selected:
Course: 2:C Language
Course: 1: Data Structure
Course: 5: Advanced Mathematics
Course: 6: Network Technology
Course: 4: Assembly Language

Tags: Java Assembly Language network C

Posted on Thu, 11 Jul 2019 12:18:15 -0700 by morphboy23