如何個判斷集合中是否存在某個元素——contains() 1.List的contains(obj)方法 實際上,List調用contains(Object obj)方法時,會遍歷List中的每一個元素,然後再調用每個元素的equals()方法去跟contains()方法中的參數進行比較,如果有一個元 ...
- 如何個判斷集合中是否存在某個元素——contains()
1.List的contains(obj)方法
實際上,List調用contains(Object obj)方法時,會遍歷List中的每一個元素,然後再調用每個元素的equals()方法去跟contains()方法中的參數進行比較,如果有一個元素的equals()方法返回true則contains()方法返回true,否則所有equals()方法都不返回true,則ontains()方法則返回false。因此,重寫了Course類的equals()方法,否則,testListContains()方法的第二條輸出為false。
2.Set的Contains(obj)方法
當調用HashSet的contains(Object obj)方法時,其實是先調用每個元素的hashCode()方法來返回哈希碼,如果哈希碼的值相等的情況下再調用equals(obj)方法去判斷是否相等,只有在這兩個方法所返回的值都相等的情況下,才判定這個HashSet包含某個元素。因此,需重寫Course類的hashCode()方法和equals()方法。
以下代碼測試List和Set的contains()方法:
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; private Scanner console; public static Student student; public SetTest() { coursesToSelect = new ArrayList<Course>(); console = new Scanner(System.in); } 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); } } /** * 測試List 的contains()方法 */ public void testListContains() { Course c = coursesToSelect.get(0); System.out.println("取得課程:" + c.name); System.out.println("課程【" + c.name + "】是否在備選課程中:" + coursesToSelect.contains(c)); Course c2 = new Course(c.id, c.name); System.out.println("新創建課程:" + c2.name); System.out.println("課程【" + c2.name + "】是否在備選課程中:" + coursesToSelect.contains(c2)); System.out.println("請輸入課程名稱:"); String courseName = console.next(); Course c3 = new Course(); c3.name = courseName; System.out.println("課程【" + c3.name + "】是否在備選課程中:" + coursesToSelect.contains(c3)); } /* * 創建學生並選課 */ public void createStudentAndSelectCourse() { student = new Student("1", "李雷"); for (int i = 0; i < 3; i++) { System.out.println("請輸入課程編號:"); String courseId = console.next(); for(Course c : coursesToSelect ) { if (c.id.equals(courseId)) { student.courses.add(c); } } } } public void testForEachForSet(Student student) { System.out.println("共選擇了" + student.courses.size() + "門課程!"); for (Course c : student.courses) { System.out.println("選擇了課程:" + c.id + ":" + c.name); } } /** * 測試Set的contains()方法 */ public void testSetContains() { System.out.println("請輸入課程名稱:"); String courseName = console.next(); Course c = new Course(); c.name = courseName; System.out.println("所選擇的課程中是否包含" + courseName + ":" + student.courses.contains(c)); } public static void main(String[] args) { SetTest st = new SetTest(); st.testAdd(); st.testListContains(); st.createStudentAndSelectCourse(); st.testForEachForSet(SetTest.student); st.testSetContains(); } }
Course類:
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; } public Course() { } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Course)) return false; Course other = (Course) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }