GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Friday, December 17, 2010

Java Collection - Map

There's two kind of implementation for Map: HashMap and TreeMap. To initialize a map, use:

>> Map mNames = new HashMap();

then

>> mNames.put("luna","maya");
>> mNames.put("aura","kasih");

To access an element, use:

>> String s = mNames.get("luna");     // use key

To remove an element, use:

>> mNames.remove("luna");     // use key

To get the size:

>> int count = mNames.size();

It's better to convert the key of the Map to Set for iterating the elements.

>> Set set = mNames.keySet();     // convert key to set
>> Iterator ite = set.iterator();
>> while(ite.hasNext()){
>>    String key = ite.next();
>>    String value = mNames.get(key);
>> }



Share/Bookmark

No comments:

Post a Comment