简体   繁体   English

如何从Java中的HashMap返回关键内容?

[英]How to return a key content from a HashMap in java?

Working on a small project which stores some students' records in a HashMap as database. 正在做一个小项目,该项目将一些学生的记录存储在HashMap中作为数据库。

private HashMap<String, Student> studentDb = new HashMap<String, Student>();

My application has a UI which lets the users to choose any string as key record. 我的应用程序具有一个UI,使用户可以选择任何字符串作为键记录。 It can be numbers or letters.Creating each record, creates an objects from another class which keeps information in its fields, but it is not suppose to store the key. 它可以是数字或字母。创建每条记录,从另一个类创建一个对象,该对象将信息保留在其字段中,但不应存储该键。

After storing all the data they can request to see all the data has been stored so far. 存储所有数据后,他们可以请求查看所有数据是否已存储。 The toSting method from the class which I instantiate my object from, generates the message to be displayed. 我实例化我的对象的类中的toSting方法生成要显示的消息。

At the time of displaying, I need to display the key they chose with the rest of stored data and they may not ask for all of them. 在显示时,我需要显示他们选择的键以及其余存储的数据,而他们可能并不需要全部。 The question is, how can I get access to a given record's key? 问题是,如何获得给定记录的密钥? Let's say if I don't want to use key that user should enter at the time of their request. 假设我不想使用用户在请求时应输入的密钥。 Is there any way to drive it from the HashMap collection? 有什么方法可以从HashMap集合中驱动它吗?

I hope I could explain my question. 我希望我能解释我的问题。 Thank you. 谢谢。


Edit: 编辑:

在此处输入图片说明

This is the output for a request, but the thing is I read the student's Id from the textfield they insert the request which is kind of cheating. 这是请求的输出,但问题是我从文本字段中读取了学生的ID,他们插入了请求,这是一种作弊行为。 I wondered if I can drive it from the collection itself. 我想知道是否可以从集合本身中驱动它。

Could use something like this: 可以使用如下形式:

Working version: 工作版本:

import java.util.HashMap;

public class test{

    public static void main(String[] args){

        HashMap<String, String> studentDb = new HashMap<String, String>();

        studentDb.put("1","A");
        studentDb.put("2","B");
        studentDb.put("3","C");

        for(String key : studentDb.keySet()){
            System.out.println(studentDb.get(key));
        }

    }
}

What you could do for your situation: 您可以根据自己的情况采取的措施:

...

for(String key : studentDb.keySet()){
    if(studentDb.get(key)==/* STUDENT OBJECT */){
        // Display / do something with the key.
    }
}

...

What do you mean "to drive it from the HashMap collection"? 您是什么意思“从HashMap集合中驱动它”? Your hashmap stores data by key (in this case a String). 您的哈希图按键(在这种情况下为字符串)存储数据。 In order to access a specific value, you need to know its key. 为了访问特定值,您需要知道其键。 If I understand correctly, what you want would be like doing something like : 如果我理解正确,那么您想要做的事情就像:

map.get("myKey").getKey();

Which doesn't make sense. 这没有意义。 What you can do is iterate over all the keys in your map, with the keySet() method. 您可以执行的操作是使用keySet()方法遍历地图中的所有键。 But I'm not sure that's what you were looking for. 但是我不确定那不是您想要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM