為什么我輸出的鍵值是哈希值。。。
請輸入要刪除的學生ID!
1
成功刪除學生:2
取得鍵:3
對應的值為:com.imooc.collection.Student@2b21cc40
取得鍵:2
對應的值為:com.imooc.collection.Student@37e893df
請輸入要刪除的學生ID!
1
成功刪除學生:2
取得鍵:3
對應的值為:com.imooc.collection.Student@2b21cc40
取得鍵:2
對應的值為:com.imooc.collection.Student@37e893df
2017-10-20
舉報
2018-10-23
System.out.println("對應的值為:" + entry.getValue());
get到的Value值是一個Student對象,輸出了地址吧? 要想輸出名字還的.name
2018-05-02
public void testRemove() {
? ? ? ? //獲取從鍵盤輸入的待刪除的學生信息?
Scanner console = new Scanner(System.in);
while (true) {
// 提示用戶輸入待刪除的學生的ID字符串
System.out.println("請輸入要刪除的學生的ID!");
String ID = console.next();
// 判斷該ID是否有對應的學生對象
Student st = students.get(ID);
if (st == null) {
// 提示輸入的ID并不存在
System.out.println("該ID并不存在");
}else{
students.remove(ID);
System.out.println("成功刪除學生:" + st.name);
break;
}
}
}
試試 ?我自己試過了 就是這段的問題 ?改的地方你自己對照一下
2017-10-23
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 {
/*
* 創建一個名為students屬性用來裝學生類型的對象
*?
* */
public Map<String, Student> students; ? ? ?//需要注意 Map得import
/*
* 在構造器中初始化students屬性
*?
* */
public MapTest(){
this.students = new HashMap<String,Student>();
}
/*
* 測試添加的方法 :在鍵盤中輸入學生id,判斷是否被占用
* 若未被占用,則輸入學生姓名,創建一個新的學生對象,并且添加到students屬性中去
*?
* */
public void testPut(){
//創建一個Scanner對象 用來獲取輸入的學生id和姓名
Scanner console = new Scanner(System.in);?
int i = 0;
while(i<3){
System.out.println("請輸入學生id");
String ID = console.next();
//判斷該ID是否被占用了
Student st = students.get(ID);
if(st == null){ ? ? ? ? ? ? ? ? ? ? ? //在st為空的情況下會提示學生輸入姓名和
//提示輸入學生的姓名
System.out.println("請輸入學生姓名:");
String name = console.next();
//創建新的學生對象
Student newStudent = new Student(ID,name);
//通過調用students 的put方法,添加ID-學生的映射關系
students.put(ID, newStudent);
System.out.println("成功添加學生:" + students.get(ID).name);
i++;
}else{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//在st部位空的情況下會提示
System.out.println("該學生id已被占用!");
continue;
}
}
}
/*
* 測試Map的keySet方法
* */
public void testKeySet(){
//通過keySet方法,返回Map中所有“鍵”的Set集合
Set<String>keySet = students.keySet();
//取得students的容量
System.out.println("總共有:" + students.size() + "個學生");
//遍歷整個keySet對象取得Map中的每一個鍵,再調用get方法 取得每個鍵對應的value
for(String stuId : keySet){
Student st = students.get(stuId); ? ? ? ? ? ? ? ? ? ?//這樣便取得了某一個鍵的value值
if(st!=null){
System.out.println("學生:" + st.name);
}
}
}
/*
* 測試刪除Map的映射
*?
* */
public void testRemove(){
//獲取從鍵盤輸入待刪除的學生ID字符串
Scanner console = new Scanner(System.in);
while(true){
//提示輸入待刪除學生的id
System.out.println("請輸入要刪除的學生ID!");
String ID ?= console.next();
//判斷這個ID是否有對應的學生對象
Student st = students.get(ID);
if(st == null){
//提示輸入的ID并不存在
System.out.println("該ID不存在");
continue;
}
students.remove(ID);
System.out.println("成功刪除學生:" + st.name);
break;
}
}
/*
* 通過entrySet方法來遍歷Map
* */
public void testEntrySet(){
//通過entrySet方法 ,返回Map中所有鍵值對的集合
Set<Entry<String,Student>> entrySet = students.entrySet();
for(Entry<String,Student>entry:entrySet){
System.out.println("取得鍵:" + entry.getKey()); ? ? ? ?//取得entry鍵的key值
System.out.println("對應的值為:" + entry.getValue());
}
}
public static void main(String[] args) {
MapTest ?mt= new MapTest();
mt.testPut();
mt.testKeySet();
mt.testRemove();
mt.testEntrySet();
}
}
2017-10-21
貼代碼啊,沒代碼用命去說?。?/p>