相关文章推荐
不要命的鸡蛋  ·  List (或ArrayList) ...·  1 月前    · 
温暖的啄木鸟  ·  SuperMap iClient3D ...·  6 月前    · 
茫然的显示器  ·  php ...·  7 月前    · 
失恋的鞭炮  ·  教你如何成为Java OOM ...·  1 年前    · 
大方的茴香  ·  因子分析 ...·  1 年前    · 
Map<String, Integer> map = new HashMap<>();
// 往 map 中添加元素
Map.Entry<String, Integer> firstEntry = map.entrySet().iterator().next();
String key = firstEntry.getKey();
Integer value = firstEntry.getValue();
  • 使用 keySet() 方法:
  • Map<String, Integer> map = new HashMap<>();
    // 往 map 中添加元素
    String key = map.keySet().iterator().next();
    Integer value = map.get(key);
    

    注意:如果map是空的那么会抛出NoSuchElementException

    可以判断map是否为空,如果不为空再进行操作

    if(!map.isEmpty()){
        // 获取第一个元素
    
  •