热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用字符串键管理嵌套映射-Managingnestedmapswithstringkeys

CanIaccessmynestedMapinmyiteratorwhenthenestedMapiscreatedintheput()method,likethi

Can I access my nestedMap in my iterator when the nestedMap is created in the put() method, like this:

当在put()方法中创建嵌套映射时,我可以在迭代器中访问我的嵌套映射,如下所示:

@Override
public String put(final String row, final String column, final String value) {
    /**
     * Second map which is contained by centralMap, that contain Strings as
     * Keys and Values.
     */
    Map nestedMap;

    if (centralMap.containsKey(row))
        nestedMap = centralMap.get(row);
    else
        nestedMap = new HashMap();
    if (!nestedMap.containsKey(column))
        counter++;
    centralMap.put(row, nestedMap);
    return nestedMap.put(column, value);
}

and the centralMap is declared as an Object-Variable,

并且centralMap被声明为对象变量,

private final Map> centralMap;

but instantiated just in the constructor, like this:

但只是在构造函数中实例化,如下所示:

centralMap = new HashMap>();

the method i'm trying to implement is the remove method:

我试图实现的方法是remove方法:

@Override
    public void remove() {
        for (Map map : centralMap.values()) {
            map = centralMap.get(keyName);
            iteratorNested.remove();
            if (map.size() <= 0)
                iteratorCentral.remove();
        }
    }

Thanks a lot!

非常感谢!

1 个解决方案

#1


0  

Not sure what you're asking exactly, but I think this is a little easier to read:

不确定你究竟在问什么,但我认为这更容易阅读:

@Override
public String put(final String row, final String column, final String value) {
    /**
     * Second map which is contained by centralMap, that contain Strings as
     * Keys and Values.
     */
    Map nestedMap = centralMap.get(row);
    if (nestedMap == null) {
      nestedMap = new HashMap();
      centralMap.put(row,nestedMap);
    }

    if (!nestedMap.containsKey(column))
        counter++;
    centralMap.put(row, nestedMap);
    return nestedMap.put(column, value);
}

I can't quite understand what you're doing in the second stanza, so can't help you improve that. And I don't see an iterator as referred to in your question.

我不太明白你在第二节中做了什么,所以无法帮助你改进。而且我没有看到你问题中提到的迭代器。

You're making me guess, but maybe ELSEWHERE in your program (it would really help to see more code, and a specific function prototype or statement of behavior you're seeking) you want to be able to iterate through the contents of the centralMap instance, and nested instances of nestedMap. Yes you can.

你猜对了,但是你的程序中可能是ELSEWHERE(它确实有助于查看更多代码,以及您正在寻找的特定函数原型或行为声明)您希望能够遍历centralMap的内容实例和嵌套的嵌套实例。是的你可以。

public void iterateOverAllNested()
{
   for (Map.Entry> nested : centralMap) {
     final String centralKey = nested.key();
     final Map nestedMap = nested.value();
     System.out.println("Central map row/column: "+centralKey);
     for (Map.Entry entry : nestedMap) {
       System.out.println(" key="+entry.key()+", value="+entry.value());
     }
   }
}

Note that this smells. Nested maps of untyped Strings are probably wrong. Any chance you've been writing Perl recently? I suggest you write a second SO question asking about a good data structure for your specific problem. You can include this code as your starting place, and folks will likely offer a cleaner solution.

请注意,这闻起来。无类型字符串的嵌套映射可能是错误的。你最近有没有机会写Perl?我建议你写第二个问题,询问有关特定问题的良好数据结构。您可以将此代码作为起始位置,人们可能会提供更清晰的解决方案。


推荐阅读
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 电话号码的字母组合解题思路和代码示例
    本文介绍了力扣题目《电话号码的字母组合》的解题思路和代码示例。通过使用哈希表和递归求解的方法,可以将给定的电话号码转换为对应的字母组合。详细的解题思路和代码示例可以帮助读者更好地理解和实现该题目。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • React项目中运用React技巧解决实际问题的总结
    本文总结了在React项目中如何运用React技巧解决一些实际问题,包括取消请求和页面卸载的关联,利用useEffect和AbortController等技术实现请求的取消。文章中的代码是简化后的例子,但思想是相通的。 ... [详细]
  • 一、HashSet1.虑重功能特性(HashMap实现)2.put(key)如果重复返回false***Add ... [详细]
author-avatar
那一年2502931247
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有