- 如何判断集合中是否存在某个元素——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; } }
- Map中是否包含指定的Key和Value
在Map中,用containsKey()方法,判断是否包含某个Key值;用containsValue()方法,判断是否包含某个Value值。
以下是MapTest类的部分示例代码:
package com.test.collection;
public class MapTest { public Map<String, Student> students; public Scanner console; public MapTest() { this.students = new HashMap<String, Student>(); this.console = new Scanner(System.in); } public void testContainsKeyOrValue() { System.out.println("请输入学生ID:"); String stuId = console.next(); System.out.println("是否有ID为" + stuId + "的学生:" + students.containsKey(stuId)); if (students.containsKey(stuId)) { System.out.println("ID为" + stuId + "的学生的名字是:" + students.get(stuId).name); } System.out.println("请输入学生的姓名:"); String stuName = console.next(); Student st = new Student(null, stuName); System.out.println("是否有姓名为" + stuName + "的学生:" + students.containsValue(st)); } }
注:跟List中的Contains()方法一样,Map中的ContainsValue()方法也需要调用某个Value值的equals()方法,去和参数对象进行比较,如果匹配成功,返回结果为true,说明在Map中的Value值确实包含参数对象。因此,需要重写Student类的equals()方法。
Student类:
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是接口,接口不能被直接实例化) } @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 Student)) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }