0

I'm new to Java (and not too comfortable with strong typing) and I have a method that takes in a HashMap. A key in this hashmap contains a key, which has a hashmap for value, which also points to a hashmap, etc, until we reach a string:y

HashMap1->HashMap2->HashMap3->HashMap4->String

I am trying to access it as follows:

HashMap1
 .get("aKey")
 .get("anotherKey")
 .get("yetAnotherKey")
 .get("MyString");

But then I get an error,

Object does not have a method "get(String)

Here is the method, simplified:

public HashMap<String, HashMap> getMyString(Map<String, HashMap> hashMap1) {
 String myString = hashMap1
 .get("aKey")
 .get("anotherKey")
 .get("yetAnotherKey")
 .get("MyString");
 // do something with myString.
 return hashMap1;
}

How would someone properly define the method and the parameters to access nested elements easily?

Thank you,

ifloop
8,4062 gold badges28 silver badges35 bronze badges
asked Apr 1, 2014 at 11:16
4
  • What is the type parameter of internal HashMap? I hope that is NOT a map. that's why! Commented Apr 1, 2014 at 11:19
  • 1
    Maybe your Logic is flawed if you have to use this construct Commented Apr 1, 2014 at 11:19
  • are you sure you aren't calling get() on String at last? Commented Apr 1, 2014 at 11:19
  • 1
    seems you are calling one extra get method. can you add data adding code lines also. Commented Apr 1, 2014 at 11:25

3 Answers 3

1

Simple as that

HashMap1.get("aKey") -- > return hashMap2
.get("anotherKey") --> return hashMap3
.get("yetAnotherKey") --> return hashMap4
.get("MyString"); --> return String

There is something wrong with the adding part.

Now you have structure like below.

hashmap1 --> hashmap2 --> String 
String myString = hashMap1.get("aKey").get("MyString");

That is how it should be.

answered Apr 1, 2014 at 11:28
1
  • The structure was imposed by JSON being parsed and converted to a LinkedHashMap. However, I extracted the values earlier on and didn't have to run with this issue in my method. Commented Apr 2, 2014 at 11:38
1

You made too many .get calls. Probably the last one is not needed.

Can you just create class CompoundKey with arbitrary number of String fields and use it as a key? It would simplify your design.

To use it properly in Java you need to override hashCode and equals methods.

Thom
15.3k34 gold badges116 silver badges201 bronze badges
answered Apr 1, 2014 at 11:22
1
  • Accorrding to him its like that. HashMap1.get("aKey") -- > return hashMap2 .get("anotherKey") --> return hashMap 3 .get("yetAnotherKey") --> return hashMap4 .get("MyString"); --> return String seems nothing wrong with retrieving part. right? Commented Apr 1, 2014 at 11:26
0

You should first of all use interfaces not implementation, therefore use Map (and not HashMap) where possible.

And second, you should repair your Generics and use all levels. Now the compiler can help you and possible show your error.

// i suppose you want to return a String, at least the method name tells it
public String getMyString(Map<String, Map<String, Map<String, Map<String, String>>>> hashMap1) {
 String myString = hashMap1
 .get("aKey")
 .get("anotherKey")
 .get("yetAnotherKey")
 .get("MyString");
 return myString;
}

Yet i suggest that you use a different data structure.

answered Apr 1, 2014 at 11:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.