开发者

How to iterate a synchronized treeMap?

开发者 https://www.devze.com 2023-04-08 01:03 出处:网络
Currently have this implementation: static Map s_AvailableGameTables = Collections.synchronizedMap(new TreeMap<Integer,Table>());

Currently have this implementation:

static Map s_AvailableGameTables = Collections.synchronizedMap(new TreeMap<Integer,Table>());   

How开发者_StackOverflow社区 can I iterate over all it's content from the start to the end like an array?

Thanks


Assuming the declaration is

static Map<Integer,Table> s_AvailableGameTables = Collections.synchronizedMap(new TreeMap<Integer,Table>());

(not just Map)

The following will iterate over all key/value pairs:

for (Map.Entry<Integer,Table> e : s_AvailableGameTables.entrySet())
{
    int key = e.getKey();
    Table tbl = e.getValue();
}


If you want to iterate over the entries (key-value pairs in the map):

for (Map.Entry<Integer, Table> entry : s_AvailableGameTables.entrySet()) {
    System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号