【问题标题】:Printing only specific keys in a hashmap仅打印哈希图中的特定键
【发布时间】:2013-08-01 10:57:16
【问题描述】:

我一直在尝试让我的小型应用程序仅打印哈希图中的特定键(其中不包含“不需要的”字符串)。我尝试这样做的方式如下所示:

Map<String, Integer> items = new HashMap<String, Integer>();

    String[] unwanted = {"hi", "oat"};

    items.put("black shoes", 1);
    items.put("light coat", 10);
    items.put("white shoes", 40);
    items.put("dark coat", 90);

    for(int i = 0; i < unwanted.length; i++) {
        for(Entry<String,Integer> entry : items.entrySet()) {
            if(!entry.getKey().contains(unwanted[i])) {
                System.out.println(entry.getKey() + " = " + entry.getValue());
            }
        }
    }

但它会打印:

dark coat = 90
black shoes = 1
light coat = 10
white shoes = 40
black shoes = 1

但是,它的目的是打印它(因为它应该省略其中包含“hi”和“oat”的键,它们应该离开:)

black shoes = 1

我不知道为什么我看不到错误,但希望有人可以帮助我指出。

【问题讨论】:

  • 在打印出来之前,您必须检查是否可以在每个键中找到任何不需要的字符串...在您的解决方案中,您的 for 循环仅检查您的两个不需要的字符串之一是否在键中.例如。 if (!blackshores.contains("hi")) sysout(...) 这就是你得到错误结果的原因

标签: java arrays hashmap key


【解决方案1】:

您的内部循环逻辑不正确。只要不存在不需要的字符串,它就会打印一个 hashmap 条目。

将for循环逻辑改成如下图...

bool found = false;
for(Entry<String,Integer> entry : items.entrySet()) {
    found = false;
    for(int i = 0; i < unwanted.length; i++) {
        if(entry.getKey().contains(unwanted[i])) {
           found = true;            
        }
    }
    if(found == false)
      System.out.println(entry.getKey() + " = " + entry.getValue());
}

【讨论】:

    【解决方案2】:

    如果你看到你的外循环:

    for(int i = 0; i < unwanted.length; i++)
    

    然后它会遍历

    String[] unwanted = {"hi", "oat"};
    

    你的地图如下:

    "dark coat" : 90
    "white shoes": 40
    "light coat" : 10
    "black shoes", 1
    

    因此在第一次迭代中,

    unwanted[i]="hi"
    

    所以你的内部循环不会打印“白鞋”,而是打印:

    dark coat = 90
    black shoes = 1
    light coat = 10
    

    因为它们不包含“hi”

    在第二次交互中,

    unwanted[i]="oat"
    

    因此,您的内部循环不会打印 "dark coat""light coat",而是从地图中打印剩余部分:

    white shoes = 40
    black shoes = 1
    

    因此,您将获得上述两次迭代的组合输出:

    dark coat = 90
    black shoes = 1
    light coat = 10
    white shoes = 40
    black shoes = 1
    

    所以你可以试试这段代码,其中内循环和外循环被翻转:

    Map<String, Integer> items = new HashMap<String, Integer>();
    
        String[] unwanted = {"hi", "oat"};
        items.put("black shoes", 1);
        items.put("light coat", 10);
        items.put("white shoes", 40);
        items.put("dark coat", 90);
    
        boolean flag;
        for(Map.Entry<String,Integer> entry : items.entrySet()) {
            if(!stringContainsItemFromList(entry.getKey(),unwanted))
                System.out.println(entry.getKey() + " = " + entry.getValue());
        }
    

    在上面的代码中,我们使用了静态函数:

    public static boolean stringContainsItemFromList(String inputString, String[] items)
        {
            for(int i =0; i < items.length; i++)
            {
                if(inputString.contains(items[i]))
                {
                    return true;
                }
            }
            return false;
        }
    

    希望有所帮助!

    【讨论】:

      【解决方案3】:

      想法是首先通过条目列表获取mapentrySetiterate的所有条目,在ith迭代中获取ith条目,然后打印它的值

      Iterator itr = yourMap.entrySet().iterator();
      while(itr.hasNext()) {
          Map.Entry entry = (Map.Entry)itr.next();
          System.out.print("(Key: " + entry.getKey() + " ,  Value:" + entry.getValue()); 
      } 
      

      【讨论】:

        猜你喜欢
        • 2022-12-10
        • 2011-10-27
        • 1970-01-01
        • 2017-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多