准备:
public class Student {
private String id;
private String name;
private String address;
private String mobile;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
import java.util.HashSet;
import java.util.Scanner;
public class Main {
private static HashSet<Student> set = new HashSet<Student>();
public static void main(String[] args) {
System.out.println("*********************************");
System.out.println("*\t\t\t\t*");
System.out.println("*\t欢迎使用学生信息管理系统\t*");
System.out.println("*\t\t\t\t*");
System.out.println("*********************************");
while (true) {
menu();
}
}
static void menu() {
System.out.println("1、添加学生信息");
System.out.println("2、删除学生信息");
System.out.println("3、修改学生信息");//地址传递
System.out.println("4、查询学生信息");//name
System.out.println("请输入操作,以Enter键结束:");
Scanner scanner = new Scanner(System.in);
int option = scanner.nextInt();
switch (option) {
case 1:{
System.out.println("请输入学号:");
String id = scanner.next();
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入号码:");
String mobile = scanner.next();
System.out.println("请输入地址:");
String address = scanner.next();
Student student =new Student();//放在set方法的前面,不然报错。
student.setId(id);
student.setName(name);
student.setMobile(mobile);
student.setAddress(address);
set.add(student);
if (set.contains(student)) {
System.out.println("学号不允许重复");
return;
}
System.out.println(set.size());
System.out.println("添加成功");
break;
}
case 2:{
System.out.println("请输入学号:");
String id = scanner.next();
Student student =new Student();
student.setId(id);
if (set.remove(student)) {//删除并判断是否成功
System.out.println("删除成功");
return;
}
System.out.println("没找到"+id+"学号");
return;
}
case 3:{
System.out.println("请输入学号:");
String id = scanner.next();
Student student =new Student();
student.setId(id);
if (!set.contains(student)) {//set是否包含student
System.out.println("没找到"+id+"学号");
return;
}
for (Student stu:set) { equals方法,而student未改写方法,则调用object的equals,比较的是地址,因此和需要的不符合,所以要改写
System.out.println("请输入新姓名:");
String name = scanner.next();
System.out.println("请输入新号码:");
String mobile = scanner.next();
System.out.println("请输入新地址:");
String address = scanner.next();
stu.setName(name);
stu.setMobile(mobile);
stu.setAddress(address);
return;
}
}
break;
}
case 4:{
System.out.println("请输入学号:");
String id = scanner.next();
Student student =new Student();
student.setId(id);
if (!set.contains(student)) {
System.out.println("没找到"+id+"学号");
return;
}
for (Student stu:set) {
if (stu.getId().equals(id)) {
System.out.println(stu);
return;
}
}
break;
}
default:
System.out.println("I'm Sorry,there is not the "+option+" option,please try again.");
}
}
}
case 2的remove:
remove
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
removeNode
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&//如果没有数据直接删除,则table为null,removeNode直接返回null,删除失败
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))//table不为null,这里用到了hash,所以为了达到效果,student需要重写hash里的hashcode方法
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;//将p的下一个位置(null)赋值给e,使得其为null,删除成功
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
此时,student重写的方法:
@Override
public int hashCode() {
// TODO Auto-generated method stub
return id.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Student) {
Student stu = (Student)obj;
return stu.id.equals(this.id);
}
return false;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", address=" + address + ", mobile=" + mobile + "]";
}
case 4中的contains:
contains
public boolean contains(Object o) {
return map.containsKey(o);
}
containsKey
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
}
getNode
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))//判断是否有要找的 数据,有则返回和输入的key相同key的数据,即first
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}