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

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

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.

  1. private Map<String,String> domainMap;
  2. public Map<String,String> getDomainMap() {
  3. return domainMap;
  4. }
  5. public void setDomainMap(Map<String,String> domainMap) {
  6. this.domainMap = domainMap;
  7. }
  8. //This method should add the domainName as key and their ipAddress as value into a Map
  9. public void addDNSDetails (String domainName,String ipAddress){
  10. domainMap.put(domainName, ipAddress);
  11. }
  12. /*
  13. * This method should return the domain name for the specific ipAddress which is passed as the argument.
  14. * For example: If the map contains the key and value as:
  15. * www.yahoo.net 205.16.214.15
  16. www.gmail.net 195.116.254.154
  17. www.tech.net 15.160.204.105
  18. if the ipAddress is 195.116.254.154 the output should be www.gmail.net
  19. */
  20. public String findDomainName(String ipAddress)
  21. {
  22. String domain = null;
  23. for (Map.Entry<String, String> entry : domainMap.entrySet()) {
  24. String k = entry.getKey();
  25. String v = ipAddress;
  26. if (k.equals(v)) {
  27. domain = k;
  28. }
  29. }
  30. return domain;
  31. }
  32. } ```"
  33. <details>
  34. <summary>英文:</summary>
  35. Both Key and value are user input.
  36. Then value is passed to the as variable to retrieve the corresponding key for the value.
  37. My output returns NULL. Please guide me to retrieve key by passing value.

public class DomainBO {

  1. private Map&lt;String,String&gt; domainMap;
  2. public Map&lt;String,String&gt; getDomainMap() {
  3. return domainMap;
  4. }
  5. public void setDomainMap(Map&lt;String,String&gt; domainMap) {
  6. this.domainMap = domainMap;
  7. }
  8. //This method should add the domainName as key and their ipAddress as value into a Map
  9. public void addDNSDetails (String domainName,String ipAddress){
  10. domainMap.put(domainName, ipAddress);
  11. }
  12. /*
  13. * This method should return the domain name for the specific ipAddress which is passed as the argument.
  14. * For example: If the map contains the key and value as:
  15. * www.yahoo.net 205.16.214.15
  16. www.gmail.net 195.116.254.154
  17. www.tech.net 15.160.204.105
  18. if the ipAddress is 195.116.254.154 the output should be www.gmail.net
  19. */
  20. public String findDomainName(String ipAddress)
  21. {
  22. String domain = null;
  23. for (Map.Entry&lt;String, String&gt; entry : domainMap.entrySet()) {
  24. String k = entry.getKey();
  25. String v = ipAddress;
  26. if (k.equals(v)) {
  27. domain = k;
  28. }
  29. }
  30. return domain;
  31. }

}

  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. You actually compare the key to your `ipAdress` that's what you want, rather it's:
  5. ```java
  6. public String findDomainName(String ipAddress) {
  7. String domain = null;
  8. for (Map.Entry<String, String> entry : domainMap.entrySet()) {
  9. String k = entry.getKey();
  10. String v = entry.getValue(); // &lt;----
  11. if (ipAddress.equals(v)) {
  12. domain = k;
  13. }
  14. }
  15. return domain;
  16. }

Can be shortened to:

  1. public String findDomainName(String ipAddress) {
  2. for (Map.Entry<String, String> entry : domainMap.entrySet())
  3. if (ipAddress.equals(entry.getValue()))
  4. return entry.getKey();
  5. return null;
  6. }

Using Streams:

  1. public String findDomainName(String ipAddress) {
  2. return domainMap.entrySet().stream().filter(e -> ipAddress.equals(e.getValue()))
  3. .map(Map.Entry::getKey).findFirst().orElse(null);
  4. }

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 :

  1. public String findDomainName(String ipAddress) {
  2. String domain = null;
  3. for (Map.Entry&lt;String, String&gt; entry : domainMap.entrySet()) {
  4. String k = entry.getKey();
  5. String v = entry.getValue(); // &lt;----
  6. if (ipAddress.equals(v)) {
  7. domain = k;
  8. }
  9. }
  10. return domain;
  11. }

Can be shorten in

  1. public String findDomainName(String ipAddress) {
  2. for (Map.Entry&lt;String, String&gt; entry : domainMap.entrySet())
  3. if (ipAddress.equals(entry.getValue()))
  4. return entry.getKey();
  5. return null;
  6. }

Using Streams

  1. public String findDomainName(String ipAddress) {
  2. return domainMap.entrySet().stream().filter(e -&gt; ipAddress.equals(e.getValue()))
  3. .map(Map.Entry::getKey).findFirst().orElse(null);
  4. }

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:

确定