javahashmapguavagraph-theoryjgraph

Get a node by ID with Guava Graph


I'm looking for a way to retrieve a node using an identifier, using only the Guava library. I prefer not to use an external HashSet, which I have considered, because my dataset is too big.

I would like a way to index all of my graph nodes using an index, using a String or integer type, and be later able to retrieve my nodes efficiently.

Right now I could iterate over the nodes set of my MutableGraph, and check for object equality, like this :

    MutableGraph<CategoryNode> wikiGraph = GraphBuilder.directed().build();
    for (MyNode node : wikiGraph.nodes()) {
        if(node.equals(new MyNode("myStringIndex"))) {
            // object found !
            return node;
        }
    }

But this is highly inefficient if the number of nodes gets big. Is there a built-in solution for indexing graph nodes in Guava or do I need to use another library ?


Solution

  • If you're looking for a way to index your graph nodes, sadly Guava doesn't have this feature built in. Consider using CQEngine, which by my understanding allows you to do this for any collection (such as a set of graph nodes).

    Alternatively, if you're just looking for a more memory-efficient set implementation, there is a wide range of options including but not limited to Koloboke, fastutil and Eclipse Collections.