Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅打印hashmap中的特定键_Java_Arrays_Hashmap_Key - Fatal编程技术网

Java 仅打印hashmap中的特定键

Java 仅打印hashmap中的特定键,java,arrays,hashmap,key,Java,Arrays,Hashmap,Key,我一直在尝试让我的小应用程序只打印hashmap中的特定键(其中不包含“不需要的”字符串)。我尝试这样做的方式如下所示: Map<String, Integer> items = new HashMap<String, Integer>(); String[] unwanted = {"hi", "oat"}; items.put("black shoes", 1); items.put("light coat", 10); items

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

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());
            }
        }
    }
但是,它的意思是打印这个(因为它应该省略其中带有“hi”和“oat”的键,它们应该离开:)


我不知道为什么我看不到故障,但希望有人能帮我指出。

您的内部循环逻辑不正确。只要不存在不需要的字符串,它就会打印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());
}
boolfound=false;
for(条目:items.entrySet()){
发现=错误;
for(int i=0;i<1.length;i++){
if(entry.getKey()包含(不需要的[i])){
发现=真;
}
}
if(found==false)
System.out.println(entry.getKey()+“=”+entry.getValue());
}

如果看到外环:

for(int i = 0; i < unwanted.length; i++)
您的地图如下所示:

"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"
因此,您的内部循环不会打印
“深色外套”
“浅色外套”
,而是打印地图上的剩余部分:

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());
    }
Map items=newhashmap();
字符串[]不需要={“hi”,“oat”};
物品。放置(“黑鞋”,1);
项目。放置(“薄涂层”,10);
物品。放置(“白色鞋子”,40);
物品。放置(“深色外套”,90);
布尔标志;
for(Map.Entry:items.entrySet()){
如果(!StringContainesSiteMFromList(entry.getKey(),不需要))
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;
    }
公共静态布尔StringContainesSiteMFromList(字符串inputString,字符串[]项)
{
对于(int i=0;i

希望这有帮助

方法是首先通过
entrySet
获取
map
的所有条目,
ith
iteration fetch
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()); 
} 

在打印出来之前,您必须检查每个键中是否有任何不需要的字符串。。。在您的解决方案中,yoor for循环仅检查两个不需要的字符串中是否有一个在键中。例如,如果(!black shores.包含(“hi”)sysout(…),这就是为什么您的结果是错误的
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;
    }
Iterator itr = yourMap.entrySet().iterator();
while(itr.hasNext()) {
    Map.Entry entry = (Map.Entry)itr.next();
    System.out.print("(Key: " + entry.getKey() + " ,  Value:" + entry.getValue()); 
}