Java—集合架構Set,java架構set

來源:互聯網
上載者:User

Java—集合架構Set,java架構set

  • Set介面及其實作類別——HashSet

  Set是元素無序並且不可以重複的集合,被稱作集。

  HashSet—雜湊集,是Set的一個重要實作類別。

  • Set的使用

    HashSet沒有像List一樣的set()方法來用指定元素替換指定位置的元素,因為Set集合的元素是無序的。同樣也不能用訪問索引的方式來擷取指定位置的元素。

Course.java

package com.test.collection;/** * 課程類 * @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;/** * 學生類 * @author Administrator * */public class Student {    public String id;    public String name;    public Set<Course> courses;//所選課程    public Student(String id, String name) {        this.id = id;        this.name = name;        this.courses = new HashSet<Course>();//執行個體化sourses介面(Set是介面,介面不能被直接執行個體化)    }}

SetTest.java

package com.test.collection;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Scanner;public class SetTest {    public List<Course> coursesToSelect;    public SetTest() {        coursesToSelect = new ArrayList<Course>();    }    public void testAdd() {        Course c1 = new Course("1", "資料結構");//建立課程對象的執行個體        Course c2 = new Course("2", "C語言");        Course c3 = new Course("3", "離散數學");        Course c4 = new Course("4", "組合語言");        Course[] course = {c1, c2, c3, c4};        coursesToSelect.addAll(Arrays.asList(course));    }        public void testForEach() {        System.out.println("有以下課程可以選擇:(通過For Each)");        for (Object obj : coursesToSelect) {            Course c = (Course) obj;            System.out.println("課程:" + c.id + ":" + c.name);        }    }        public void testForEachForSet(Student student) {
System.out.println("共選擇了" + student.courses.size() + "門課程!"); for (Course c : student.courses) { System.out.println("選擇了課程:" + c.id + ":" + c.name); } } public static void main(String[] args) { SetTest st = new SetTest(); st.testAdd(); st.testForEach(); Student student = new Student("1", "李雷"); Scanner console = new Scanner(System.in); for (int i = 0; i < 3; i++) { System.out.println("請輸入課程編號:"); 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); }}

  執行結果:

有以下課程可以選擇:(通過For Each)課程:1:資料結構課程:2:C語言課程:3:離散數學課程:4:組合語言請輸入課程編號:1請輸入課程編號:3請輸入課程編號:4
共選擇了3門課程!
選擇了課程:3:離散數學選擇了課程:1:資料結構選擇了課程:4:組合語言

  註:Set中,添加某個對象,無論添加多少次,最終只會保留一個該對象(的引用),並且,保留的是第一次添加的那個。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.