Foreach loop in java for Dictionary

后端 未结 4 1110
死守一世寂寞
死守一世寂寞 2021-02-19 20:15

I want to go through every items in a dictionary in java. to clarify what I want to do, this is the C# code

Dictionary LableList = new Dict         


        
4条回答
  •  离开以前
    2021-02-19 20:27

    I'm assuming you have a Map which is the Java built-in dictionary structure. Java doesn't let you iterate directly over a Map (i.e. it doesn't implement Iterable) because it would be ambiguous what you're actually iterating over.

    It's just a matter of choosing to iterate through the keys, values or entries (both).

    e.g.

    Map map = new HashMap();
    //...
    
    for ( String key : map.keySet() ) {
    }
    
    for ( Label value : map.values() ) {
    }
    
    for ( Map.Entry entry : map.entrySet() ) {
        String key = entry.getKey();
        Label value = entry.getValue();
    }
    

    Your C# code seems to be the same as iterating over the entries (the last example).

提交回复
热议问题