- The concept of a collection
In real life: a lot of things come together
A set 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 attributes
- The role of the collection
- Within the class, organize the data (for the same properties as the action and meaning, put them in a collection)
- Simple and fast search for large numbers of entries
- A collection interface that provides a series of ordered elements that can be quickly inserted or deleted in a sequence
- There is a collection interface that provides a mapping relationship that can be used to quickly find the corresponding unique object by keyword (key), which can be any type
- Fixed length of array, variable set length
- An array can only access elements by subscript, the subscript is an integer, the type is fixed, and the collection may find the specific object being mapped by any type (subscript)
- Java Collection Framework Architecture
The collection and map interfaces are two root interfaces.
The collection has three sub-interfaces, the list (sequence), queue, and set (set) interfaces, where list and queue-stored elements are ordered and repeatable, and the elements stored in set are unordered and non-repeatable. List and set are more commonly used in these three sub-interfaces. List has a very common and very important implementation class ArrayList (array sequence), queue has a very important implementation class LinkedList (linked list), LinkedList class is also the implementation class of the list interface, Set also has a very important implementation class HashSet (hash set).
The map interface also has a number of sub-interfaces, commonly used is its implementation class, which has a very important implementation of the class HashMap (hash table).
A separate object is stored in the collection. and map inside, with a key a value two objects for a map to store data, such a mapping is an instance of the entry class, entry class (key-value pairs) is an internal class of map, key and value can be any type of object.
- Collection interfaces, sub-interfaces, and implementation classes
The collection interface is the parent interface of the list, queue, and set interfaces, and defines the operations that can be used to manipulate the list, set and queue additions and deletions.
- List interface and its implementation class--arraylist
A list is a set of elements that are ordered and repeatable, known as sequences, where you can precisely control where each element is inserted, or delete a position element; ArrayList is an array sequence, and the underlying is implemented by an array.
Take the student elective course as an example to introduce the use of list:
1.1 Add
Course.java
Package com.test.collection; /** @author*/Publicclass Course {public String ID; Public String name; Public Course (string ID, string name) { this. id = ID; this. Name = name;} }
Student.java
Packagecom.test.collection;ImportJava.util.HashSet;ImportJava.util.Set;/*** Student class *@authorAdministrator **/ Public classStudent { PublicString ID; PublicString name; PublicSet courses;//Selected Courses PublicStudent (string ID, string name) { This. ID =ID; This. Name =name; This. courses =NewHashSet ();//Instantiate sourses (set is interface, interface cannot be instantiated directly) }}
Listtest.java
Packagecom.test.collection;Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.Iterator;Importjava.util.List;/*** Alternative Course classes *@authorAdministrator **/ Public classListtest {/*** List to store alternative courses*/ PublicList Coursestoselect; Publiclisttest () { This. Coursestoselect =NewArrayList (); } /*** Used to add alternative courses to Coursestoselect*/ Public voidTestadd () {Course C1=NewCourse ("1", "Data Structure");//create an instance of a course objectCoursestoselect.add (C1);//call the Add method to add a course to the list of alternative coursesCourse temp = (Course) coursestoselect.get (0);//objects are stored in the collection as object, ignoring the type before the object, and the type conversion required when removingSystem.out.println ("added course:" + temp.id + ":" +temp.name); Course C2=NewCourse ("2", "C language"); Coursestoselect.add (0, C2);//another Add () method that is overloaded in the list to specify where the element is addedCourse Temp2 = (Course) coursestoselect.get (0); System.out.println ("Added course:" + temp2.id + ":" +temp2.name); Coursestoselect.add (C1);//elements in list can be repeatedCourse temp0 = (Course) coursestoselect.get (2); System.out.println ("added course:" + temp0.id + ":" +temp0.name); //The following method throws an array subscript out of bounds exception//Course C3 = New Course ("3", "Java"); //Coursestoselect.add (4, C3);course[] Course= {NewCourse ("3", "Discrete Mathematics"),NewCourse ("4", "Assembly language")}; Coursestoselect.addall (Arrays.aslist (course)); Course Temp3= (Course) coursestoselect.get (3); Course Temp4= (Course) coursestoselect.get (4); System.out.println ("Added two courses:" + temp3.id + ":" + Temp3.name +temp4.id+ ":" +temp4.name); Course[] Course2= {NewCourse ("5", "Advanced mathematics"),NewCourse ("6", "Computer network")}; Coursestoselect.addall (2, Arrays.aslist (COURSE2)); Course TEMP5= (Course) coursestoselect.get (2); Course TEMP6= (Course) coursestoselect.get (3); System.out.println ("Added two courses:" + temp5.id + ":" + Temp5.name +temp6.id+ ":" +temp6.name); } Public Static voidMain (string[] args) {Listtest LT=Newlisttest (); Lt.testadd (); }}
Execution Result:
Added lessons: 1: Data structures
Added lessons: 2: C language
Added lessons: 1: Data structures
Two courses added: 3: Discrete Math 4: assembly language
Two courses added: 5: Advanced Mathematics 6: Computer networks
1.2 Queries
Packagecom.test.collection;Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.Iterator;Importjava.util.List;/*** Alternative Course classes *@authorAdministrator **/ Public classListtest {/*** List to store alternative courses*/ PublicList Coursestoselect; Publiclisttest () { This. Coursestoselect =NewArrayList (); } /*** method to get the elements in list*/ Public voidTestget () {intSize =coursestoselect.size (); System.out.println ("The following course options are available:"); for(inti = 0; i < size; i++) {Course C=(Course) coursestoselect.get (i); System.out.println ("Course:" + c.id + ":" +c.name); } } /*** Access to collection elements via iterators*/ Public voidTestiterator () {Iterator it=Coursestoselect.iterator (); System.out.println ("There are the following course choices (accessed via iterators):"); while(It.hasnext ()) {Course C=(Course) it.next (); System.out.println ("Course:" + c.id + ":" +c.name); } } /*** Access to collection elements through the For each method*/ Public voidTestforeach () {System.out.println ("There are the following course choices (accessed through for each):"); for(Object obj:coursestoselect) {Course C=(Course) obj; System.out.println ("Course:" + c.id + ":" +c.name); } } Public Static voidMain (string[] args) {Listtest LT=Newlisttest (); Lt.testget (); Lt.testiterator (); Lt.testforeach (); }}
Execution Result:
The following course options are available:
Course: 2: C language
Course: 1: Data structures
Course: 5: Advanced Mathematics
Course: 6: Computer networks
Course: 1: Data structures
Course: 3: Discrete Mathematics
Course: 4: assembly language
1.3 Modifications
Public class listtest {publicvoid testmodify () { new Course ("7", "Gross"); Coursestoselect. Set (4, C); }}
After modification, the following courses can be selected:
Course: 2: C language
Course: 1: Data structures
Course: 5: Advanced Mathematics
Course: 6: Network Technology
Course: 7: Mao
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) Cour Sestoselect.get (4), (Course) coursestoselect.get (5 RemoveAll (Arrays.aslist (courses)); System.out.println ( "deleted course"
After deletion, you have the following courses to choose from:
Course: 2: C language
Course: 1: Data structures
Course: 5: Advanced Mathematics
Course: 6: Network Technology
Course: 4: assembly language
java-Collection Frame List