-
吃鸡游戏
您可以通过添加串联的姓名、性别来创建静态字符串哈希集。class YourClass { public static Set<String> studentSet = new HashSet<>(); public static void addStudent(String name, String gender) { YourClass.studentSet.add(name + "," + gender); } public static Boolean studentExists(String name, String gender) { return YourClass.studentSet.constains(name + "," + gender);}因此,您的 HashSet 中的数据将类似于 Tom,male 、Tom,female、John,male 。class Student {...public Student(String name, String gender) { this.name = name; this.gender = gender; YourClass.addStudent(name, gender); }}在创建 Student 实例时,您可以检查 HashSet 中是否没有 Name、Gender,然后实例化和对象。if(!YourClass.studentExists(name, gender)) { Student student = new Student(name, gender);}我希望这能解决你的问题
-
暮色呼如
您可以创建一个类来控制对象的创建 - 基本上使用享元模式。步骤 1. 将 Student 包的构造函数设置为私有public class Student { Student(String name, String gender) { ...步骤2.在Student中实现equals方法 public boolean equals (Object other) { if (!(other instanceof Student)) return false; Student otherStudent = (Student) other; return Objects.equals(otherStudent.getName(), this.getName()) && Objects.equals(otherStudent.getGender(), this.getGender()); }步骤 3. 在同一包中实现 Student 对象池public class StudentPool { private List<Student> students = new ArrayList<>(); public Student getOrCreate(String name, String gender) { Student student = new Student(name, gender); return students.stream() .filter(s -> Objects.equals(s, student)) .findFirst() .orElseGet(() -> { students.add(student); return student; }); } public List<Student> get(String name) { return students.stream() .filter(student -> Objects.equals(student.getName(), name)) .collect(Collectors.toList()); }}
-
婷婷同学_
假设您所说的“值”指的是学生的姓名,我建议您编写一个小函数,如果该姓名已被占用,该函数将返回 true。为此,您可以将所有“学生”存储在 ArrayList 中:ArrayList<Student> students = new ArrayList<Student>();现在您可以将学生添加到此 ArrayList 中,如下所示:private boolean nameIsAlreadyTaken(ArrayList<Student> students, String newName){ for (int i = 0; i < student.size(); i++){ if(students.get(i).getName().equals(newName)){ return true; } } return false;}(您需要在学生类中定义一个 getName() 函数才能使其工作。)那么你可以这样做:字符串 newName = "迈克尔"; String newGender = "男";if (!nameIsAlreadyTaken(students, newName)){students.add(new Student(newName, newGender));} else{ //something you want to to if the name is already taken}您可以在不将 Students-ArrayList 传递给函数的情况下执行此操作,但这取决于您。
-
偶然的你
我想说你需要重写Student和equals方法hashCode,然后检查构造函数是否存在这样的学生。关于这一点的好答案:链接。import java.util.*;public class Student { private static final Set<Student> REGISTERED_STUDENTS = new HashSet<>(); private String name; private String gender; public static Collection<Student> getRegisteredStudents() { return Collections.unmodifiableCollection(REGISTERED_STUDENTS); } public Student(final String name, final String gender) { this.name = name; this.gender = gender; if (REGISTERED_STUDENTS.contains(this)) throw DuplicateStudentException(); REGISTERED_STUDENTS.add(this); } @Override public int hashCode() { return Objects.hash(name, gender); } @Override public boolean equals(final Object o) { if (this == o) return true; if (!(o instanceof Student)) return false; final Student student = (Student) o; return Objects.equals(name, student.name) && Objects.equals(gender, student.gender); }}请注意,这个示例不是线程安全的,并且使用了在构造函数中抛出异常的有争议的解决方案。您可能需要一个工厂方法和ConcurrentSkipListSet/或另一个线程安全集合,如下所示:class Student { private static final Set<Student> REGISTERED_STUDENTS = new ConcurrentSkipListSet<>(Comparator.comparing(Student::getName).thenComparing(Student::getGender)); private String name; private String gender; public static Set<Student> getRegisteredStudents() { return Collections.unmodifiableSet(REGISTERED_STUDENTS); } public static void addStudent(final String name, final String gender) { Student probablyExists = new Student(name, gender); REGISTERED_STUDENTS.add(probablyExists); } private Student(final String name, final String gender) { this.name = name; this.gender = gender; } public String getName() { return name; } public String getGender() { return gender; } @Override public int hashCode() { return Objects.hash(name, gender); } @Override public boolean equals(final Object o) { if (this == o) return true; if (!(o instanceof Student)) return false; final Student student = (Student) o; return Objects.equals(name, student.name) && Objects.equals(gender, student.gender); }}