java - 使用 Jung 去除 RDF 图死胡同

标签 java rdf jung

我正在尝试实现一种从 RDF 图中删除死胡同的方法

Iterator<String> iter = rdfGraph.getVertices().iterator();  
    while(iter.hasNext()){
        String x = iter.next();
        if(rdfGraph.outDegree(x)==0){

            iter.remove();
        }

    }

每当我运行它时,我都会得到一个 java.lang.UnsupportedOperationException。我该如何修复我的代码。

最佳答案

迭代器的 remove()方法是可选的。 javadoc 提到当它不受支持时,它会抛出一个UnsupportedOperationException:

Throws:

UnsupportedOperationException - if the remove operation is not supported by this iterator

基于 adding a node to a collection using JUNG2 中的讨论,我假设 getVertices() 返回一个不可修改的集合,在这种情况下,迭代器将不支持 remove()。请注意,getVertices() 的 javadoc 指出该方法返回顶点的 View 。它并没有说在集合中添加或删除会在图中添加或删除顶点,或者甚至可以在集合中添加或删除顶点。

getVertices Collection<V> getVertices()

Returns a view of all vertices in this graph. In general, this obeys the Collection contract, and therefore makes no guarantees about the ordering of the vertices within the set.

代替 iter.remove(),使用 rdfGraph.removeVertex(x) 可能更有意义。

但是,根据您的评论

I used rdfGraph.removeVertex(x) initially but i got a java.util.ConcurrentModificationException. any idea on how I could bypass this without using an iterator?

听起来您要么需要遍历顶点并创建一个包含要删除的顶点的新集合,然后在遍历顶点后删除它们。或者,您可以创建一个包含所有 顶点的新集合,然后在遍历时删除它们。例如,

new ArrayList<>(rdfGraph.getVertices()).stream()
  .filter(v -> rdfGraph.outDegree(x) == 0)
  .forEach(rdfGraph::removeVertex);

ArrayList 的使用在那里并不重要;您只需要一些未连接到原始图形的新集合。

关于java - 使用 Jung 去除 RDF 图死胡同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37577506/

相关文章:

java - 在抽象类中保存数据的最佳实践

java - 安装apk时如何修复 Lollipop 中的解析错误

encoding - Redland RDF 中 URI 的字符串表示形式的字符编码是什么?

c++ - 十进制、 double 、整数等数据类型的范围?

java - 在 SimpleDateFormat 模式字符串中使用字母字符

java - GWT + Hibernate 多对一 xml O/R 映射 : SerializationException

rdf - 4store 是否支持 SPARQL 1.1?

java - 创建一个 DelegateForest 并以预先确定的顺序显示它

java - 荣格2 : how to disable node selfloop

Java Jung 不兼容类型转换