Answer the question
In order to leave comments, you need to log in
What is the correct way to check the uniqueness of an object in Java before creating it?
Please advise how to correctly organize the check for the uniqueness of an object before creating it.
public class User {
private String name;
private static Set<String> onlineUsers = new HashSet<>();
public User(String name) { this.name = name; onlineUsers.add(name); }
public void logoutUser() { onlineUsers.remove(this.name); }
public String getName() { return name; }
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question