In addition to the list, the set interface is often used, and the elements stored in the set interface are unordered and non-repeatable, so they are called datasets;
The set interface is unordered, so it does not provide a set method like List to modify elements, find, add, delete is no problem
The following is a simple example of a student's course selection using set:
Course class, Course.java
1 /*2 * Course class, which specifies the course structure for passing elements of this type to the collection3 */4 5 Public classCourse {6 PublicString ID;7 PublicString name;8 PublicCourse (String id,string name) {9 //construction method with parameterTen This. ID =ID; One This. Name =name; A } - - PublicCourse () { the //parameterless constructor for subclass initialization implicit construction method - } -}
Student class, including number, attribute, selected course (stored with Set set), Student.java
1 ImportJava.util.HashSet;2 ImportJava.util.Set;3 4 /*5 * Student Class6 */7 8 Public classStudent {9 PublicString ID;Ten PublicString name; One PublicSet<course> courses;//defines a collection of set types for holding selected courses A - PublicStudent (String id,string name) { - This. ID =ID; the This. Name =name; - This. courses =NewHashset<course> ();//instantiating a reference to a set type by using the HashSet method - } -}
Similarly, generics are used to specify the type of set set that holds the course attributes selected by the student.
The class that operates the set set, Settest.java
1 Importjava.util.ArrayList;2 Importjava.util.Arrays;3 Importjava.util.List;4 ImportJava.util.Scanner;5 6 Public classSettest {7 8 PublicList<course>Coursestoselect;9 Publicsettest () {Ten This. Coursestoselect =NewArraylist<course>(); One } A Public voidTestadd () { -Course CR1 =NewCourse ("1", "Data Structure"); -Coursestoselect.add (CR1);//Add property to list, position 0 the -Course Cr2 =NewCourse ("2", "C language"); -Coursestoselect.add (1, CR2); - + -Course[] Course = {NewCourse ("3", "Discrete Mathematics"),NewCourse ("4", "Assembly Language")};//Create an array of course classes to hold multiple objects +Coursestoselect.addall (Arrays.aslist (course));//Transfer the array to list to the Coursetoselect property A atCourse[] Course2 = {NewCourse ("5", "Advanced mathematics"),NewCourse ("6", "Algorithmic analysis")}; -Coursestoselect.addall (4, Arrays.aslist (COURSE2)); - } - - /* - * Iterate through the elements in the list in */ - Public voidTestforeach () { toSYSTEM.OUT.PRINTLN ("All courses:"); + for(Course CR: This. Coursestoselect) { -System.out.println ("Course:" + cr.id + ":" +cr.name); the } * } $ Panax Notoginseng Public Static voidMain (string[] args) { -Settest st =Newsettest (); the St.testadd (); + St.testforeach (); A the //Create student Objects +Student stu =NewStudent ("201201100838", "Xiao Ying"); -SYSTEM.OUT.PRINTLN ("Welcome!" "+ stu.name +" study Number: "+stu.id); $ $ //Receive Course input -Scanner console =NewScanner (system.in); - the for(inti = 0;i < 3;i++){ -System.out.println ("Please enter Course ID:");WuyiString CourseID =Console.next (); the //Traversal comparison - for(Course cr:st.coursesToSelect) { Wu if(Cr.id.equals (CourseID)) { -Stu.courses.add (CR);//Add the selected course About $ - //repeated additions do not take effect, which is equivalent to not being added, and multiple identical object references retain only one - Stu.courses.add (CR); - A //stu.courses.add (NULL); //passing in null values is also possible + } the } - } $Console.close ();//Close Input the intnum =stu.courses.size (); theSystem.out.println ("Total selected" + num + "courses, the list is as follows:"); the St. Setforeach (Stu); the - in the } the About /* the * Traversal of elements in set can only be done with the Foreach method or iterator method, not with the Get method, because the arrangement of elements in set is unordered and cannot be queried by index the * and eventually each output is unordered the */ + - Public voidSetforeach (Student stu) { the for(Course cr:stu.courses) {BayiSystem.out.println (cr.id + ":" +cr.name); the } the } -}
The above program can realize the simple function of elective course, first initialize the alternative course into list, then make the input alignment, deposit in the student class set, and then use the foreach to traverse out the result of the set, the output of the selected course content is as follows:
The set interface is basically consistent with the list, and the salient feature is that the elements in the set are unordered and non-repeatable, and it is important to note that the object is repeatedly added in the example, but without any effect, only one is reserved in the set when multiple duplicate objects are added, and a null null pointer is also available. The element in set cannot be found with a GET method, but can only be traversed by a foreach or Iterator method, and according to the result, the order of output is different.
Use of Set collections in Java