I need to return both map and map1 how to return what return type i should use. is it possible?
public Map<String, String> getUserDetail(String email, String otp) {
HashMap<String, String> map = new HashMap<>();
HashMap<String, User> map1 = new HashMap<>();
if(otpService.validateOtp(email, otp)){
if(checkEmailId(email)){
User user = userRepository.findByEmail(email);
map1.put("user", user);
map.put("message","Login Success");
return map;
}
else {
map.put("message","Email Not Register");
}
}else {
map.put("message","Invalid OTP");
}
return map;
}
Java doesn't let you return multiple values, or tuples, natively. There are libraries that exist that include tuples or a Pair
class that you can use, but that might be overkill for your use case, as it could be simple enough to create your own:
class UserDetail {
private HashMap<String, String> messages = new HashMap<>();
private HashMap<String, User> userInfo = new HashMap<>();
public UserDetail(HashMap<String, String> messages, HashMap<String, User> userInfo) {
this.messages = messages;
this.userInfo = userInfo;
}
public HashMap<String, String> messages() {
return this.messages;
}
public HashMap<String, User> userInfo() {
return this.userInfo;
}
}
And then your function can look something like this:
public UserDetail getUserDetail(String email, String otp) {
HashMap<String, String> map = new HashMap<>();
HashMap<String, User> map1 = new HashMap<>();
if(otpService.validateOtp(email, otp)){
if(checkEmailId(email)){
User user = userRepository.findByEmail(email);
map1.put("user", user);
map.put("message","Login Success");
return map;
}
else {
map.put("message","Email Not Register");
}
}else {
map.put("message","Invalid OTP");
}
return new UserDetail(map, map1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.