【问题标题】:How can I get hashmap's key inside of the hashmap [duplicate]如何在哈希图中获取哈希图的密钥 [重复]
【发布时间】:2022-01-07 23:51:09
【问题描述】:

我正在尝试制作一个没有 MySQL 数据库的系统,该系统将包含帐户和帐户数据。但是没有用

我的账户 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");
        }
    }  
}

在我的 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;
}

第二个“.get”给出了“找不到符号”之类的错误。为什么将 hashmap 作为对象元素?另外,如果您有其他方法可以在没有哈希表的情况下存储这些数据并检查密码等。请告诉我,谢谢。

【问题讨论】:

  • 哦,打错字了,但还是一样的语法错误
  • 你有原始类型。它的Map&lt;String, String&gt; accountList = new HashMap&lt;&gt;(); - 然后它工作正常。并不是说这是好的代码,但我们都必须从某个地方开始。

标签: java authentication hashmap


【解决方案1】:

为了得到你会做的钥匙

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

你应该将你的地图声明为

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

但最好是声明一个用户类并将其放入地图中。

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

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

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

然后您可以使用已定义的 getter(例如 user.getPassword()user.getUsername()) 来检索信息。

那么可能会这样使用:

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

【讨论】:

    猜你喜欢
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    相关资源
    最近更新 更多