Java HashMap – 通过在方法中传递 “value” 作为参数来获取/检索 “key”

huangapple go评论72阅读模式
英文:

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&lt;String,String&gt; domainMap;
	
public Map&lt;String,String&gt; getDomainMap() {
	return domainMap;
}

public void setDomainMap(Map&lt;String,String&gt; 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&lt;String, String&gt; 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(); // &lt;----
        
        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&lt;String, String&gt; entry : domainMap.entrySet()) {
            String k = entry.getKey();
            String v = entry.getValue(); // &lt;----  
            
            if (ipAddress.equals(v)) {
                domain = k;                 
            }
     }     
     return domain;                 
}

Can be shorten in

public String findDomainName(String ipAddress) {
    for (Map.Entry&lt;String, String&gt; 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 -&gt; ipAddress.equals(e.getValue()))
                    .map(Map.Entry::getKey).findFirst().orElse(null);
}

btw : First code returns last match, second code return first match

huangapple
  • 本文由 发表于 2020年8月5日 21:55:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63266735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定