contains判断
List<Student> set = new ArrayList<>(); set.add(new Student(3, "Java")); isListContains(set); // 判断是否存在Map值 private void isListContains(List<Student> set) { Student course = new Student(3,"Java"); if (set.contains(course)) { L.i("1找到了"); } else L.i("1没找到"); } public class Student { public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } private int id; private String name; public Student(int id, String name) { this.id = id; this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null) return false; if (!(o instanceof Student)) return false; if (this.getName() == null) { Student stu = (Student) o; if (stu.getName() == null) return true; else return false; } else { Student stu = (Student) o; if (this.getName().equals(stu.getName())) return true; else return false; } } }
这样写contains判断是可以的。但是把
List<Student> set = new ArrayList<>(); set.add(new Student(3, "Java")); isListContains(set); // 判断是否存在Map值
改成Set就不行了???
Set<Student> set = new HashSet<>(); set.add(new Student(3, "Java")); isListContains(set); // 判断是否存在Map值