|
| ||||||||||
| Tags: hashmap, iteration, java, looping, programming language |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Iteration over a hashmap
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)
} |
|
#2
| |||
| |||
| Re: Iteration over a hashmap
Hello, Please look at the following line in the code Code: Article at = (Item) it.next (); |
|
#3
| |||
| |||
| 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);
}
} |
|
#4
| |||
| |||
| 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);
} |
|
#5
| |||
| |||
| 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. |
|
#6
| |||
| |||
| 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). |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |