简体   繁体   English

如何在 hashmap 中获取 hashmap 的密钥

[英]How can i get hashmap's key inside of the hashmap

I'm trying to make a system without MySQL database which will have accounts and accounts' datas.我正在尝试制作一个没有 MySQL 数据库的系统,该数据库将包含帐户和帐户数据。 But It didn't work但它没有用

My accounts java file:我的帐户 java 文件:

/*
* Instead of using 2 different ArrayList (for Username and Password) we used Hashmap
* HashMap stores keys and the keys' values
* In our HashMap keys refer to usernames values refer to passwords
*/
import java.util.HashMap; 
import javax.swing.JOptionPane;

public class Accounts {
    
    //I used static to reach list from another class suggest me another way please
    static HashMap AccountList = new HashMap<String, HashMap<String, String>>();
    
    public static void CreateAccount(String userID, String password, String email, String fullName, String department){
        
        //We have to check that there shouldn't be an account with the same username
        if (AccountList.containsKey(userID)){
            JOptionPane.showMessageDialog(null, "There is an account with that username");
        }
        else{
            HashMap Data = new HashMap<String, String>();
            Data.put("userID", userID);
            Data.put("password", password);
            Data.put("email", email);
            Data.put("fullName", fullName);
            Data.put("department", department);
            AccountList.put(userID, Data);
            JOptionPane.showMessageDialog(null, "Member created successfully");
        }
    }  
}

In my login.java I get an error on that line在我的 login.java 中,我在该行收到错误

boolean IsLoginSuccessful = false;
        
        //We are getting username and password text/string from the form
        String userID = username_field.getText();
        String password = password_field.getText();
        
        //Now check if username is in list then check if username's password equals to password
        if (Accounts.AccountList.containsKey(userID)){
            String userPassword = Accounts.AccountList.get(userID).get("password");
            
            IsLoginSuccessful = true;
        }

That second ".get" gives error like "cannot find symbol" why does it takes the hashmap as an object element?第二个“.get”给出了“找不到符号”之类的错误,为什么它将 hashmap 作为 object 元素? Also if you have another way to store these datas without hashtable and check the password etc. Show me please thank you.另外,如果您有另一种方法可以在没有哈希表的情况下存储这些数据并检查密码等。请告诉我,谢谢。

To get the keys you would do要得到你会做的钥匙

Set<String> keys = AccountList.keySet();

And you should declare your map as你应该将你的 map 声明为

Map<String, Map<String,String>> accountList = new HashMap<>();
Map<String,String> data = new HashMap<>();

But it would be best if you would declare a user class and put that in the map.但最好是声明一个用户 class 并将其放入 map。

class User {
   private String username;
   private String password;
   ...

   public String getUsername() {
       return username;
   }
   public String getPassword() {
      return password;
   }
}

Map<String, User>  accountList = new HashMap<>();

You could then use defined getters (eg user.getPassword() , user.getUsername() ) to retrieve the information.然后,您可以使用已定义的 getter(例如user.getPassword()user.getUsername() )来检索信息。

Then it might be used as follows:那么它可以按如下方式使用:

String username = get_from_console.
User user = accountList.get(username);
String password = user.getPassword();

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

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