user3134565
user3134565

Reputation: 965

new to android - understanding HashMap<String,

I have been going through the following tutorial and came across this line of code which I have no idea what it means:

HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);

I know what a hashmap is, what I don't understand is what is < , (); , and Element

Upvotes: 1

Views: 793

Answers (2)

tom87416
tom87416

Reputation: 542

HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
            Element e = (Element) nl.item(i);

Decode version

HashMap<String, String>; map = new HashMap<String, String>();
Element e = (Element) nl.item(i);

&lt; represent <

&gt; represent >

This is improper HTML coding

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502476

The tutorial is just badly (horribly!) formatted - too many levels of HTML escaping. Someone didn't take nearly enough care over their post... It should look like this:

private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) 

... later on ...

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

Now this may not make much more sense to you yet, but at least it's proper Java code :) This is using generics - HashMap is a generic type with two type parameters - one for the key and one for the value. So in this case, it's a map from string to string (i.e. strings are used for both keys and values).

Read the Generics part of the Java Tutorial for more information.

The code provided isn't idiomatic Java code in terms of its use of ArrayList and HashMap, mind you. Typically you'd declare variables using the interfaces and only use the concrete classes when constructing the objects. For example:

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

Given what I've seen of the tutorial so far, I'd suggest trying to find a better one...

Upvotes: 4

Related Questions