gpt4 book ai didi

java - 在 HashMap Key 中输入 3 个属性

转载 作者:行者123 更新时间:2023-11-30 22:01:17 25 4
gpt4 key购买 nike

我是 Java 新手。我现在使用 HashMap 来存储来自 MySQL 数据库的数据,我将使用 JSON POST 请求从用户那里获取输入并在 HashMap 中搜索相关数据并从 HashMap 中检索。我需要来自用户的三个输入,但在 HashMap 中只能输入 1 个键。所以,我尝试了将 key 作为对象输入的方法,但它不起作用。下面是我从 MySQL 数据库存储数据的代码。

public class AppDataService {
HashMap<AppDataRequest, AppData> appdatas = new HashMap<AppDataRequest, AppData>();

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://****:3306/****_demo";

static final String USER = "****";
static final String PASS = "****";

public AppDataService(){
Connection conn = null;
Statement stat = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stat = conn.createStatement();
String sql = "SELECT * FROM testdata";
ResultSet resu = stat.executeQuery(sql);
while(resu.next()){
int id = resu.getInt("app_id");
String email = resu.getString("email");
String password = resu.getString("password");
String status = resu.getString("status");
String message = resu.getString("message");
String token = resu.getString("token");
appdatas.put(new AppDataRequest(id, email, password), new AppData(status, message, token));
}
resu.close();
stat.close();
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(stat!=null){
stat.close();
}
}
catch (SQLException se2){

}
try{
if(conn!=null){
conn.close();
}
}
catch(SQLException se3){
se3.printStackTrace();
}
}
}

public List<AppData> getAllAppData(){
return new ArrayList<AppData>(appdatas.values());
}

public AppData getAppData(int id){
return appdatas.get(id);
}

public AppData getSAppData(int id, String email, String password){
return appdatas.get(new AppDataRequest (id, email, password));
}
}

我的 JSon POST 代码

@Path("/appdata")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)

AppDataService ads = new AppDataService();

@POST
@Path("/appdatas")
public AppData getSAppData(AppDataRequest adr){
return ads.getSAppData(adr.getId(), adr.getEmail(),adr.getPassword());
}

应用数据类

public class AppData {
public String status;
public String message;
public String token;

public AppData(){

}

public AppData(String status, String message, String token) {
this.status = status;
this.message = message;
this.token = token;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}
}

AppDataRequest 类

public class AppDataRequest {
public int id;
public String email;
public String password;

public AppDataRequest(){

}

public AppDataRequest(int id, String email, String password) {
this.id = id;
this.email = email;
this.password = password;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

最佳答案

你可以在你的类中覆盖 hashCode 和 equals :

@Override
public int hashCode() {
int hash = 3;
hash = 83 * hash + this.id;
hash = 83 * hash + Objects.hashCode(this.email);
hash = 83 * hash + Objects.hashCode(this.password);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final AppDataRequest other = (AppDataRequest) obj;
if (this.id != other.id) {
return false;
}
if (!Objects.equals(this.email, other.email)) {
return false;
}
if (!Objects.equals(this.password, other.password)) {
return false;
}
return true;
}

如果 id 在您的表中是唯一的,为什么不将其作为键,而您的其他信息作为 map 的值:

Map<Integer, AppData> appdatas;
appdatas.put(id, new AppData(status, message, token, email, password));

关于java - 在 HashMap Key 中输入 3 个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43251702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com