英文:
Java Hashmap - Getting/retrieving "key" by passing "value" as parameter in a method
问题
以下是您要翻译的内容:
"Both Key and value are user input. Then value is passed to the as variable to retrieve the corresponding key for the value.
My output returns NULL. Please guide me to retrieve key by passing value.
private Map<String,String> domainMap;
public Map<String,String> getDomainMap() {
return domainMap;
}
public void setDomainMap(Map<String,String> domainMap) {
this.domainMap = domainMap;
}
//This method should add the domainName as key and their ipAddress as value into a Map
public void addDNSDetails (String domainName,String ipAddress){
domainMap.put(domainName, ipAddress);
}
/*
* This method should return the domain name for the specific ipAddress which is passed as the argument.
* For example: If the map contains the key and value as:
* www.yahoo.net 205.16.214.15
www.gmail.net 195.116.254.154
www.tech.net 15.160.204.105
if the ipAddress is 195.116.254.154 the output should be www.gmail.net
*/
public String findDomainName(String ipAddress)
{
String domain = null;
for (Map.Entry<String, String> entry : domainMap.entrySet()) {
String k = entry.getKey();
String v = ipAddress;
if (k.equals(v)) {
domain = k;
}
}
return domain;
}
} ```"
<details>
<summary>英文:</summary>
Both Key and value are user input.
Then value is passed to the as variable to retrieve the corresponding key for the value.
My output returns NULL. Please guide me to retrieve key by passing value.
public class DomainBO {
private Map<String,String> domainMap;
public Map<String,String> getDomainMap() {
return domainMap;
}
public void setDomainMap(Map<String,String> domainMap) {
this.domainMap = domainMap;
}
//This method should add the domainName as key and their ipAddress as value into a Map
public void addDNSDetails (String domainName,String ipAddress){
domainMap.put(domainName, ipAddress);
}
/*
* This method should return the domain name for the specific ipAddress which is passed as the argument.
* For example: If the map contains the key and value as:
* www.yahoo.net 205.16.214.15
www.gmail.net 195.116.254.154
www.tech.net 15.160.204.105
if the ipAddress is 195.116.254.154 the output should be www.gmail.net
*/
public String findDomainName(String ipAddress)
{
String domain = null;
for (Map.Entry<String, String> entry : domainMap.entrySet()) {
String k = entry.getKey();
String v = ipAddress;
if (k.equals(v)) {
domain = k;
}
}
return domain;
}
}
</details>
# 答案1
**得分**: 0
You actually compare the key to your `ipAdress` that's what you want, rather it's:
```java
public String findDomainName(String ipAddress) {
String domain = null;
for (Map.Entry<String, String> entry : domainMap.entrySet()) {
String k = entry.getKey();
String v = entry.getValue(); // <----
if (ipAddress.equals(v)) {
domain = k;
}
}
return domain;
}
Can be shortened to:
public String findDomainName(String ipAddress) {
for (Map.Entry<String, String> entry : domainMap.entrySet())
if (ipAddress.equals(entry.getValue()))
return entry.getKey();
return null;
}
Using Streams:
public String findDomainName(String ipAddress) {
return domainMap.entrySet().stream().filter(e -> ipAddress.equals(e.getValue()))
.map(Map.Entry::getKey).findFirst().orElse(null);
}
By the way, the first code returns the last match, and the second code returns the first match.
英文:
You actually compare the key to your ipAdress
that's what you want, rather it's :
public String findDomainName(String ipAddress) {
String domain = null;
for (Map.Entry<String, String> entry : domainMap.entrySet()) {
String k = entry.getKey();
String v = entry.getValue(); // <----
if (ipAddress.equals(v)) {
domain = k;
}
}
return domain;
}
Can be shorten in
public String findDomainName(String ipAddress) {
for (Map.Entry<String, String> entry : domainMap.entrySet())
if (ipAddress.equals(entry.getValue()))
return entry.getKey();
return null;
}
Using Streams
public String findDomainName(String ipAddress) {
return domainMap.entrySet().stream().filter(e -> ipAddress.equals(e.getValue()))
.map(Map.Entry::getKey).findFirst().orElse(null);
}
btw : First code returns last match, second code return first match
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论