当前位置:网站首页>Judge whether the map contains the specified key and value
Judge whether the map contains the specified key and value
2022-07-23 05:57:00 【Luoyang pig】
rewrite name Of hashcode() and equals() Method
package com.imooc.collection;
import java.util.HashSet;
import java.util.Set;
/** * Students * @author Monica * */
public class Student {
public String id;
public String name;
public Set<Course> courses;
public Student(String id,String name){
this.id = id;
this.name = name;
this.courses = new HashSet<Course>();//set Interface call
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Student))
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package com.imooc.collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
/** * Used to hold student type objects * @param args */
public Map<String,Student> students;
/** * Try it out in the construction method students attribute * @param args */
public MapTest(){
this.students = new HashMap<String,Student>();
}
/** * Test add : Enter the students ID, Determine whether it is occupied * If not occupied , Then enter the name , Create a new student object , And add to students in * @param args */
public void testPut(){
// Create a Scanner object , Students used to get input ID And name
Scanner console = new Scanner(System.in);
int i = 0;
while (i<3){
System.out.println(" Please input the students ID:");
String ID = console.next();
// Judge that ID Is it occupied
Student st = students.get(ID);
if(st==null){
System.out.println(" Please enter the student's name :");
String name = console.next();
// Create a new student object
Student newStudent = new Student(ID,name);
// By calling students Of put Method , add to ID- Student mapping
students.put(ID, newStudent);
System.out.println(" Successfully added students :"+students.get(ID).name);
i++;
}else{
System.out.println(" The student ID Occupied !");
continue;
}
}
}
/** * test Map Does it contain a Key Value or value value */
public void testContainsKeyOrValue(){
// Prompt for student id
System.out.println(" Please enter the student to query ID:");
Scanner console = new Scanner(System.in);
String id = console.next();
// stay Map in , use containsKey() Method , To determine whether it contains a Key value
System.out.println(" The student you entered ID by :"+id+", Whether there exists in the student mapping table :"+students.containsKey(id));
if(students.containsKey(id))
System.out.println(" The corresponding students are :"+students.get(id).name);
// Prompt for student name
System.out.println(" Please enter the name of the student you want to query :");
String name = console.next();
// stay containsValue() Method , To determine whether it contains a Value value
if(students.containsValue(new Student(null,name)))
System.out.println(" In the student mapping table , It does include students :"+name);
else
System.out.println(" The student does not exist in the student mapping table !");
}
/** * test Map Of keySet Method * @param args */
public void testKeySet(){
// adopt keySet Method , return Map All in “ key ” Of Set aggregate
Set<String> keySet = students.keySet();
// obtain Map The capacity of , call set Method
System.out.println(" All in all "+keySet.size()+" A student ");
// By traversing KeySet object , Get every key , Calling get Method to get the... Corresponding to each key value
for(String stuId:keySet){
Student st = students.get(stuId);
if(st!=null)
System.out.println(" Student :"+st.name);
}
}
/** * Test to delete Map Mapping in * @param args */
public void testRemove(){
// Get the student to be deleted entered from the keyboard Id character string
Scanner console = new Scanner(System.in);
while(true){
// Prompt for the name of the student to be deleted ID
System.out.println(" Please enter the students you want to delete ID!");
String ID = console.next();
// Judge that ID Whether there is a corresponding student object
Student st = students.get(ID);
if(st==null){
// Prompt for ID There is no such thing as
System.out.println(" The ID non-existent !");
continue;
}else{
students.remove(ID);
System.out.println(" Successfully deleted student :"+st.name);
break;
}
}
}
/** * adopt entrySet Method to traverse Map * @param args */
public void testEntrySet(){
// adopt entrySet Method , return Map All key value pairs in
Set<Entry<String,Student>> entrySet = students.entrySet();
for(Entry<String,Student> entry:entrySet){
System.out.println(" Get key :"+entry.getKey());
System.out.println(" The corresponding value is :"+entry.getValue().name);
}
}
/** * utilize put Methods to modify Map Mapping already exists in * @param args */
public void testModify(){
// Prompt for the student to modify Id
System.out.println(" Please enter the student you want to modify ID:");
// Create a Scanner object , To get students who input from the keyboard ID character string
Scanner console = new Scanner(System.in);
while(true){
// Get the student input from the keyboard ID
String stuID = console.next();
// from students Find the student in ID Corresponding student object
Student student = students.get(stuID);
if(student == null){
System.out.println(" The ID non-existent ! Please re-enter !");
continue;
}
// Prompt the name of the current corresponding student object
System.out.println(" At present, the student ID, The corresponding students are :"+student.name);
// Prompt for new student name , To modify the existing mapping
System.out.println(" Please enter a new student name :");
String name = console.next();
Student newStudent = new Student(stuID,name);
students.put(stuID, newStudent);
System.out.println(" Modification successful !");
break;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MapTest mt = new MapTest();
mt.testPut();
mt.testKeySet();
//mt.testRemove();
//mt.testEntrySet();
//mt.testModify();
//mt.testEntrySet();
mt.testContainsKeyOrValue();
}
}
边栏推荐
猜你喜欢
随机推荐
Test point, summary of the first day
指针学习日记(二)
DNS域名解析服务
After class exercise 01 --- QQ login
安装和登录登录
LINK : fatal error LNK1104: 无法打开文件“opencv_world340.lib”
存储过程
About the problem of re creation after deleting the module in idea:
UNIX实现IO多路复用之使用epoll函数实现网络socket服务端
讀《高效閱讀法-最劃算的自我投資》有感
爬取网页动态加载的评论
Shell programming specifications and variables
指针学习日记(一)
Learning about background mount and process management
amber教程3.2:GPU查看和用pmemd引擎跑MD
测试点,第一天总结
性能测试流程
Amber tutorial 4.6: hydrogen bond analysis of the system
Basic process of unit approval
Software life cycle model ----- V model









