小编Rob*_*ann的帖子

带迭代器的java.util.ConcurrentModificationException

我知道如果试图通过简单的循环从集合中删除循环,我会得到这个异常:java.util.ConcurrentModificationException.但我正在使用Iterator,它仍然会产生这个异常.知道为什么以及如何解决它?

HashSet<TableRecord> tableRecords = new HashSet<>();

...

    for (Iterator<TableRecord> iterator = tableRecords.iterator(); iterator.hasNext(); ) {
        TableRecord record = iterator.next();
        if (record.getDependency() == null) {
            for (Iterator<TableRecord> dependencyIt = tableRecords.iterator(); dependencyIt.hasNext(); ) {
                TableRecord dependency = dependencyIt.next(); //Here is the line which throws this exception
                if (dependency.getDependency() != null && dependency.getDependency().getId().equals(record.getId())) {
                    tableRecords.remove(record);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

java iterator removechild concurrentmodification

24
推荐指数
1
解决办法
3万
查看次数

如何获取TypeScript以加载Cordova插件随附的类型

cordova-plugin-camera在用TypeScript(3.0.1)编写的基于cordova的应用程序中使用,我希望能够看到该插件的类型。

所以我@types/cordova-plugin-camera使用npm 安装软件包。在安装过程中,npm显示不需要WARN deprecated解释此@types软件包的信息,因为类型已经包含在软件包中cordova-plugin-camera。因此,我再次取出包装。

然后,我开始编译,但是失败error TS2339: Property 'camera' does not exist on type 'Navigator'。我的理解是,这应该是开箱即用的(至少对于@types软件包而言),但是看来TypeScript不知道它应该直接包含来自的类型cordova-plugin-camera。这似乎是合乎逻辑的,因为我没有将其告知的明确声明import 'cordova-plugin-camera',但是由于我仅使用由cordova加载的全局可用对象,因此不需要。

如何获取TypeScript以直接从中加载键入内容cordova-plugin-camera


我已经尝试过的可能解决方案


import 'cordova-plugin-camera'

在我的情况下不起作用,因为我从事的项目相当老,并且不支持模块。它只是使用outFile和生成单个文件"module": "none"


?? 在中使用types属性tsconfig.json

新增中

"types": [ 
  "cordova-plugin-camera" 
]
Run Code Online (Sandbox Code Playgroud)

tsconfig.json了作品cordova-plugin-camera,但会导致打字稿停止加载类型的所有其他包,所以我不得不每一个封装类型,以我的补充tsconfig.json


?? 在中使用typeRoots属性tsconfig.json

新增中

"typeRoots": [
  "./node_modules/@types",
  "./node_modules/cordova-plugin-camera"
]
Run Code Online (Sandbox Code Playgroud)

tsconfig.json了工程cordova-plugin-camera的建设在每个文件夹中,而另一包,但会导致错误的选项 …

cordova typescript cordova-plugins typescript-typings

8
推荐指数
1
解决办法
1658
查看次数

排序地图时优先考虑某些字符串的最佳方法

我想用字符串对地图进行排序,以便对某些字符串进行优先排序,而其余的则像往常一样排序。

像这样:

"Dan",   "value"  //priority 1
"Eric",  "value"  //priority 2
"Ann",   "value"  //priority 3
"Bella", "value"  //no priority
"Chris", "value"  //no priority
Run Code Online (Sandbox Code Playgroud)

就像在这个问题中一样。

我正在使用 TreeMap 并且我当前的比较方法如下所示:

public int compare(String o1, String o2) {
    if (o1.equals(o2)) return 0;
    if (o1.equals("Dan")) return -1;
    if (o2.equals("Dan")) return 1;
    if (o1.equals("Eric")) return -1;
    if (o2.equals("Eric")) return 1;
    if (o1.equals("Ann")) return -1;
    if (o2.equals("Ann")) return 1;
    else return o1.compareTo(o2);
}
Run Code Online (Sandbox Code Playgroud)

如您所见,对于优先级更高的字符串,这会变得非常麻烦。

有一个更好的方法吗?


解决方案(感谢 amit 的想法):使用第二张地图来存储优先级:

TreeMap<String, Integer> prio = new TreeMap<>();
prio.put("Dan", 1); …
Run Code Online (Sandbox Code Playgroud)

java sorting string compare treemap

5
推荐指数
1
解决办法
1198
查看次数