Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Iteration over a hashmap

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 07-02-2010
Member
 
Join Date: Dec 2009
Posts: 178
Iteration over a hashmap
  

Hello
I am trying to do a iteration over a hashmap. Here is my code, I am not quite successful in this. Please see my code and if i am wrong then guide me.
Code:
<Article,String> HashMap items = new HashMap <Article,String>();
                      / / Adding items in Articles

Iterator it = (Iterator)this.Articles.keySet().iterator();
			while(it.hasNext()){		
	                   Article at = ....... ;  / / COMPLETED
                           Integer it = ....... ;  / / COMPLETED
                           articlestemp.could(AT, IT)
			}

Reply With Quote
  #2  
Old 07-02-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: Iteration over a hashmap

Hello,
Please look at the following line in the code
Code:
Article at = (Item) it.next ();
By cons I do not know what you want to put into your variable of type Integer. In addition to this variable has the same name as your iterator so it could be a problem. though I am not sure, do you know exactly why you are using the following in your code may be there is an alternative for this.
Reply With Quote
  #3  
Old 07-02-2010
Member
 
Join Date: Dec 2009
Posts: 178
Re: Iteration over a hashmap

Hello,
I think the fact that I specify <Article,Integer> keeps me from an iterator over correct. Here is my modified code .
Code:
	HashMap <Article,Integer> articlestemp = new HashMap <Article,Integer>();
Set s = items.keySet () ;
Iterator itr = (Iterator)s.itrator () ; 
 
			while(itr.hasNext()){
				Object o = itr.next();
Article at = (Item)o;
				if(! at.equals(has)){
					Itt = Integer (Integer)items.get(has);
articlestemp.could(a, itt);
				}
			}
Reply With Quote
  #4  
Old 07-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: Iteration over a hashmap

Hello,
Your iterator is not good, we must iterate through the collection of pairs (key + value) - so the type Map.Entry <Article, string> - and not on collection of keys only. The function that okay? HashMap: entrySet ():
Code:
<Article,String> HashMap itms = new HashMap <Article,String>();
/ / Add to your articles.

Iterator <Map.Entry<Article, String>> it = itms.entrySet().iterator()
while (it.hasNext()) {
    Map.Entry<Article, Integer> entry = it.next();
    Article = entry.getKey();
    String s = entry.getValue();
    System.out.System.out.println("map ["+ a +"] = "+ s);
}
Reply With Quote
  #5  
Old 07-02-2010
Member
 
Join Date: Dec 2009
Posts: 178
Re: Iteration over a hashmap

Hello,
All this code is a method of a class that contains items attributes and a parameter of the method. I made a mistake import Iterator object for which the need for caster. But in fact my iterator was correct. I decided to spend my hashmap in <String,Integer> was easier here to use the above method Contains Key.
Code:
Map itms = <Article,Integer> new HashMap <Article,Integer>();
a1 = new Item("XXX","Jersey");
a2 = new Item("XXX","Jersey"); / / Same object a1.
itms.could(a1new Integer(1));
if(itms.ContainsKey(a2)) => Returns false so thatit contains the same object.
Admittedly, this is not the same reference. How in this case that ContainsKey returns true? with two items with the same reference and the same wording.
Reply With Quote
  #6  
Old 07-02-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: Iteration over a hashmap

Hello,
No, it does not contain the same object: it contains an instance of Item that contains the same data, but not the same instance. Indeed, (a1! = A2) for these two references do not point to the same memory area.
To compare the contents of each instance and not their reference, look to overload the equals () that is used by default containers (List, Map, ...). in Java there are two types of comparisons:
- Comparison of reference (ie. the equivalent pointer in C / C + +) which is using the operator ==
- Comparison with the content of the body itself. To make this comparison, we must override the function Object:: equals (Object o).
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Iteration over a hashmap"
Thread Thread Starter Forum Replies Last Post
Iteration and recursion in C++ language Kim|ball Software Development 4 27-06-2011 10:32 AM
HashMap vs HashTable Joyjeet Software Development 6 04-01-2011 08:07 PM
Associated values with HashMap Miles Runner Software Development 5 13-02-2010 02:50 AM
Sorting a hashmap Gunner 1 Software Development 5 10-02-2010 04:06 AM
What is HashMap and Map? TAIPA Software Development 4 27-02-2009 10:16 PM


All times are GMT +5.5. The time now is 07:31 PM.