慕仰6620973
2016-07-23 22:51
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值
好吧,原来下节课就有讲。。自己瞎整一个多小时。。没整明白。。醉了。
Java入门第三季
409792 学习 · 4340 问题
相似问题