【问题标题】:How can I compare the string with the keys in hash map如何将字符串与哈希图中的键进行比较
【发布时间】:2017-03-27 02:44:08
【问题描述】:

我很难将字符串 (str) 与 hashmap 中的键进行比较。从下面的代码可以看出,我有多个键。我正在将str 与键进行比较,但我无法获得该特定键的值。请注意地图包含coin

只有当我直接输入字符串时才有效,例如 entry.getKey().getKeyOne().toString().contains("coin")

String str = "coin";
for(Map.Entry<Pair, String> entry : map.entrySet()) {
    if(entry.getKey().getKeyOne().toString().contains(str)||
        entry.getKey().getKeyTwo().toString().contains(str)) {
            entry.getValue();
    }
}

public class Pair {
    private String keyOne;
    private String keyTwo;

    Pair(String one,String two) {
        this.keyOne=one;
        this.keyTwo=two;
    }

    public String getKeyOne() {
        return keyOne;
    }

    public String getKeyTwo() {
       return keyTwo;
    }
}

【问题讨论】:

  • 什么是Pair
  • 试试这个它是在 android 但stackoverflow.com/questions/40062224/…
  • 如果 KeyOne 和 KeyTwo 是您自己的类,您能否发布其 toString 实现。如果不是,请按照 Andrew 之前所说的添加 Pair 的代码。
  • 你能发布你如何初始化你的HashMap吗?是HashMap吗?你想要类似的东西:entry.getKey().contains("coin")?

标签: java arrays hashmap hashtable


【解决方案1】:

建议:只要您在Map 中使用自己的类Pair,您必须 覆盖equals() 并且应该 覆盖hashCode()

您使用字符串文字的示例有效,因为“coin”在string pool 中,并且所有引用相同的字符串。

在这里运行:

public class Pair {

    private String keyOne;
    private String keyTwo;

    Pair(String one, String two) {
        this.keyOne = one;
        this.keyTwo = two;

    }

    public String getKeyOne() {
        return keyOne;
    }

    public String getKeyTwo() {
        return keyTwo;

    }

    public static void main(String[] args) {
        Map<Pair, String> map = new HashMap();
        map.put(new Pair("coin", "5"), "U");
        map.put(new Pair("bill", "100"), "H");
        map.put(new Pair("10", "5coin"), "T");

        String str = new String("coin");
        for (Map.Entry<Pair, String> entry : map.entrySet()) {
            if (entry.getKey().getKeyOne().toString().contains(str)
                    || entry.getKey().getKeyTwo().toString().contains(str)) {
                System.err.println(entry.getValue());
            }
        }
    }
}

【讨论】:

  • 你能给我举个例子吗?
【解决方案2】:

这是为您提供的简单示例代码 如果您只希望它用于一个 Hashmap

public void findInkeys(String findkey){
    HashMap<String, Object> map=new HashMap<>();

    //Place your content of MAp Here
    //Start

    //end
    //or you can place your content anywhere else but make sure is is initialized before this


    Set<String> keys=map.keySet();
    boolean isInKeys=false;
    for (String k : keys)
    {
        if(k.equals(findkey))
        {
            isInKeys=true;
        }
    }
    System.out.println(isInKeys);
}

如果你想要任何hashmap

public void findInkeys(HashMap<String,Object> map,String findkey){

    Set<String> keys=map.keySet();
    boolean isInKeys=false;
    for (String k : keys)
    {
        if(k.equals(findkey))
        {
            isInKeys=true;
        }
    }
    System.out.println(isInKeys);
}

如果您有多个要比较的键,请使用以下代码

public void findInkeys(HashMap<String, String> map,ArrayList<String> keyList){
    Set<String> keys=map.keySet();
    boolean isInKeys=false;
    for (String k : keys)
    {
        if(keyList.contains(k))
        {
            isInKeys=true;
        }
    }
    System.out.println(isInKeys);
}

【讨论】:

    猜你喜欢
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2011-03-27
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多