1

I know it's a noob question but i cant find anything online.I have this HashMap :

HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) children.item(i);
        map.put("title", ParseXMLmethods.getValue(e, "title"));
        map.put("pubDate", ParseXMLmethods.getValue(e, "pubDate"));
        map.put("link", ParseXMLmethods.getValue(e, "link"));
        map.put("description",ParseXMLmethods.getValue(e, "description"));
        localist.add(map);

I am getting an error on localist saying:"localist cannot be resolved". i know i have to declare localist but i don't know which variable type to use . Any help would be really appreciated.

2
  • what are you trying to do exactly with your map ? Commented Sep 26, 2013 at 21:35
  • What exactly are you trying to accomplish? Not easy to read the intention of localist from this. Commented Sep 26, 2013 at 21:35

3 Answers 3

1

First of all, declare your Map variables as the interface Map and initialize them as the implementation, e.g.

Map<String, String> map = new HashMap<String, String>();

you can also use the diamond operator <> to infer the template types, e.g.

HashMap<String, String> map = new HashMap<>();

While the above are not necessary, they are good practices. Now, to declare your localist, you can do something like:

List<Map<String, String>> localist = new ArrayList<>();

which is a list of maps that map a string to a string.

Sign up to request clarification or add additional context in comments.

Comments

0

If you're trying to add the map to a list, then you would use a List<Map> implementation, via ArrayList or LinkedList primarily. However why you would be keeping a list of Maps would be beyond me, I would recommend a class for this if you're storing multiple maps.

Comments

0

depending on how specific you want to be with your type, you could use ArrayList<HashMap<String, String>> localist = new ArrayList<HashMap<String, String>>();

As stated by the other answer, I would take some time to plan your Classes in your system first. Often if you can't find an answer online, it could suggest that your approach is not best practice or design when it comes to writing extensible and modular code.

You should always consider using abstract or interface types instead of concrete implementations for a start:

AbstractList<Map<String,String>>

Comments

Your Answer

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.