Java-collection framework Set, java framework set
- Set interface and its implementation class -- HashSet
Set is a Set of unordered elements that cannot be repeated. It is called a Set.
HashSet-Hash Set is an important implementation class of Set.
HashSet does not use the set () method like List to replace the elements at the specified position with the specified elements, because the elements in the Set set Set are unordered. Similarly, you cannot access the index to obtain the elements at the specified position.
Course. java
Package com. test. collection;/*** Course class * @ 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 class ** @ author Administrator **/public class Student {public String id; public String name; publicSet <Course>Courses; // public Student (String id, String name) {this. id = id; this. name = name; this. courses = newHashSet <Course>(); // Instantiate the sourses interface (Set is an interface, and the interface cannot be directly instantiated )}}
SetTest. java
Package com. test. collection; import java. util. arrayList; import java. util. arrays; import java. util. list; import java. util. secret; public class SetTest {public List <Course> coursesToSelect; public SetTest () {coursesToSelect = new ArrayList <Course> ();} public void testAdd () {Course c1 = new Course ("1", "Data Structure"); // create an instance of the Course object Course c2 = new Course ("2", "C "); course c3 = new Course ("3", "Discrete Mathematics"); Course c4 = new Course ("4", "assembly language"); Course [] course = {c1, c2, c3, c4}; coursesToSelect. addAll (Arrays. asList (course);} public void testForEach () {System. out. println ("you can choose from the following courses: (For Each)"); for (Object obj: coursesToSelect) {Course c = (Course) obj; System. out. println ("Course:" + c. id + ":" + c. name) ;}} public void testForEachForSet (Student student ){
System. out. println ("selected" + student. courses. size () + "course! "); For (Course c: student. courses) {System. out. println ("Selected Course:" + c. id + ":" + c. name) ;}} public static void main (String [] args) {SetTest st = new SetTest (); st. testAdd (); st. testForEach (); Student student = new Student ("1", "Li Lei"); login console = new Login (System. in); for (int I = 0; I <3; I ++) {System. out. println ("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 Course: 3: discrete mathematics course: 4: assembly language, enter the course number: 1. Enter course No.: 3. Enter course No.: 4.
Three courses have been selected!
Course selected: 3: discrete mathematics Selected Course: 1: Data Structure Selected Course: 4: Assembly Language
Note: to add an object to a Set, no matter how many times it is added, only the object (the reference) will be retained and the one added for the first time will be retained.