Java-Set Frame Set

Source: Internet
Author: User

    • Set interface and its implementation class--hashset

A set is a set of elements that are unordered and cannot be duplicated, called a set.

The hashset-hash set is an important implementation class for set.

    • Use of Set

HashSet does not have a list-like set () method to replace the element at the specified position with the specified element, because the elements of the set collection are unordered. It is also not possible to get the element at the specified location by accessing the index.

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;  Public set<course>Courses//Selected Courses     PublicStudent (string ID, string name) { This. ID =ID;  This. Name =name;  This. courses =New hashset<course>();//Instantiate Sourses interface (set is interface, interface cannot be instantiated directly)    }}

Settest.java

 Packagecom.test.collection;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List;ImportJava.util.Scanner; Public classSettest { PublicList<course>Coursestoselect;  Publicsettest () {Coursestoselect=NewArraylist<course>(); }     Public voidTestadd () {Course C1=NewCourse ("1", "Data Structure");//create an instance of a course objectCourse C2 =NewCourse ("2", "C language"); Course C3=NewCourse ("3", "Discrete Mathematics"); Course C4=NewCourse ("4", "Assembly language"); Course[] Course={c1, C2, C3, C4};    Coursestoselect.addall (Arrays.aslist (course)); }         Public voidTestforeach () {System.out.println ("You can choose from the following courses: (through for each)");  for(Object obj:coursestoselect) {Course C=(Course) obj; System.out.println ("Course:" + c.id + ":" +c.name); }    }         Public voidTestforeachforset (Student Student) {
System.out.println ("Total selected" + student.courses.size () + "door course! ");
for(Course c:student.courses) {System.out.println ("Selected course:" + c.id + ":" +c.name); } } Public Static voidMain (string[] args) {Settest St=Newsettest (); St.testadd (); St.testforeach (); Student Student=NewStudent ("1", "Li Lei"); Scanner Console=NewScanner (system.in); for(inti = 0; I < 3; i++) {System.out.println ("Please enter the course number:"); String CourseID=Console.next (); for(Course c:st.coursestoselect) {if(C.id.equals (CourseID)) {Student.courses.add (c);
//student.courses.add (null); }}} st.testforeachforset (student); }}

Execution Result:

you can choose from the following courses: (through for each) course: 1 : Data structure Course: 2 : C language courses: 3 : Discrete Mathematics course: 4 : assembly Language Please enter the course number: 1 Please enter the course number: 3 Please enter the course number: 4
A total of 3 courses were selected!
The course was selected: 3 : Discrete Mathematics selected the course: 1 : The data structure selected the course: 4: assembly language

Note: In set, adding an object, no matter how many times it is added, will eventually keep only one of the objects (the reference), and it retains the one that was added for the first time.

Java-Set Frame Set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.