0

I needed to extend my question Map collection separate Object values

Here is the code

I'm working with cypher in java. I wrote a query (you can see it in the answer of my question Taxi sharing scheduling in neo4j thank you Brian for your help)

after the query i wrote this code

Result userData = graphDb.execute(query);

ArrayList<Map<String, Object>> userNode = new ArrayList<>();

while(userData.hasNext()){               
    Map<String, Object> user = userData.next();
    userNode.add(user);

    System.out.println("Key: " + user.keySet());
    System.out.println("Value: " + user.values());  
 }

and I got the results:

Key: [stops]
Value: [[{grid_item=Grid1, time=09:30}, {grid_item=Grid13, time=10:00}, {grid_item=Grid3, time=10:15}, {grid_item=Grid10, time=10:35}]]

If i use the format

map.get(stops)[0] // grid_item=Grid1, time=09:30
map.get(stops)[1] // grid_item=Grid13, time=10:00

how can i correctly translate it?

user.get(stops)[0]; //in this case i got an error
Community
  • 1
  • 1
marhg
  • 659
  • 1
  • 17
  • 30
  • `user.get(stops)` returns an Object. You need to cast it to the actual type. – Klitos Kyriacou Nov 10 '15 at 15:13
  • String gridName = user.get("stops")[0].toString(); Can i do something like this? But i got this error "The type of the expression must be an array type but it resolved to Object" – marhg Nov 10 '15 at 17:46
  • No you can't do that, because `user.get("stops")` returns an Object. You can't index an Object. You can only index an array. If you know what exact type it is that you are storing as the value in your map, then you need to cast it to that type. For example, `((MyType[])user.get("stops"))[0].toString()`. – Klitos Kyriacou Nov 10 '15 at 20:09
  • @KlitosKyriacou, i still have problems, could you please help me. I declared an Array variable String[ ] gridName; and then this ((gridName[ ])user.get("stops"))[0].toString(); Is not correct. But i tried with different ways and i couldn't do it. Could you please write the correct variable declaration. Or how should i do it? thank you thank you! – marhg Nov 12 '15 at 11:22
  • You get a compilation error because you defined a variable, and you are trying to cast an expression to a variable: ((gridName[])user.get("stops")). You can't cast anything to a variable. You must cast to a type or a class. – Klitos Kyriacou Nov 12 '15 at 17:52

0 Answers0