0

I have a graph in Neo4j and every relationship in this graph has a property called InverseRandomNumber. I want to traverse all the nodes and for each one of them get those properties from all the relationships attached to the node, sum those property values and save the sum as property in the node. The InverseRandomNumber is an integer or a double and i use Neo4j 2.3.3 embedded in Java. Below you can find my code.

Label myLabel = DynamicLabel.label("Data");
final RelationshipType type2 = DynamicRelationshipType.withName("Rel"); 

ResourceIterator<Node> users = graphDb.findNodes( myLabel, "Group", "Random" );

Node firstUserNode;
while ( users.hasNext() )
{
    firstUserNode = users.next();

    ArrayList<Object> RelList = new ArrayList<Object>();
    if(firstUserNode.hasRelationship(Direction.BOTH)){
        for (Relationship relationship : firstUserNode.getRelationships(Direction.BOTH)){
            RelList.add(relationship.getProperty("InverseRandomNumber"));
        }
    }

    Double sum = 0.0;
    Double dd = 0.0;

    for(int j=0; j < RelList.size(); j++){
        dd = toDouble(RelList.get(j));
        sum+=dd;
    }

    firstUserNode.setProperty("SumInverse",sum);        
}
users.close();

In the above code i use the toDouble method as to turn the value i get into double. Is that the correct way to handle numerical properties or is there another method more efficient and accurate?

Any ideas?

dimcode
  • 213
  • 3
  • 16

1 Answers1

0

If you don't want that use BigDecima:

BigDecimal.valueOf((Double)n.getProperty("InverseRandomNumber"));
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • The value of the property, as i mentioned before, may be either an integer or a double so when i use your method i get an error that the integer cannot be cast to double. – dimcode Jun 27 '16 at 16:24