这个怎样,因为用了set,所以没用list.。
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class UserManager {
Set
public static void main(String[] args) {
UserManager manager = new UserManager();
User user1 = new User();
User user2 = new User();
user1.setId(1);
user1.setName("jack");
user2.setId(2);
user2.setName("lucy");
manager.addUser(user1);
manager.addUser(user2);
manager.removeUser(user1);
manager.isExist(user1);
manager.show();
}
private void show() {
System.out.println("当前有" + userSet.size() + "个用户:");
for (Iterator iterator = userSet.iterator(); iterator.hasNext();) {
System.out.println(iterator.next());
}
}
private void isExist(User user) {
if(userSet.contains(user)) {
System.out.println("该用户存在");
}
System.out.println("该用户不存在");
}
private void removeUser(User user) {
userSet.remove(user);
System.out.println("删除用户:" + user);
}
private void addUser(User user) {
userSet.add(user);
System.out.println("添加用户:" + user);
}
}
class User {
private int id;
private String name;
public User() {
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return id + " " + name;
}
}
你好,按照你的要求,代码如下:
import java.util.HashSet;
public class UserManager {
HashSet
public void add(User user) {
set.add(user);
}
public void del(User user) {
set.remove(user);
}
public boolean isExist(User user) {
return set.contains(user);
}
public void print(User user) {
System.out.println(user.toString());
}
}
class User {
String name;
public String toString() {
return "姓名: " + name;
}
}
import java.util.HashSet;
public class UserManager {
HashSet
public void add(User user) {
set.add(user);
}
public void del(User user) {
set.remove(user);
}
public boolean isExist(User user) {
return set.contains(user);
}
public void print(User user) {
System.out.println(user.toString());
}
}
class User {
String name;
public String toString() {
return "姓名: " + name;
}
}