疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

浅谈Java集合框架 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 coursesToSelect;

  private Scanner console;

  public static Student student;

  public SetTest() {

  coursesToSelect = new ArrayList();

  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;

  }

  }

  复制代码