Results 1 to 6 of 6

Thread: Iteration over a hashmap

  1. #1
    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)
    			}

  2. #2
    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.

  3. #3
    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);
    				}
    			}

  4. #4
    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);
    }

  5. #5
    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.

  6. #6
    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).

Similar Threads

  1. Iteration and recursion in C++ language
    By Kim|ball in forum Software Development
    Replies: 4
    Last Post: 27-06-2011, 10:32 AM
  2. HashMap vs HashTable
    By Joyjeet in forum Software Development
    Replies: 6
    Last Post: 04-01-2011, 09:07 PM
  3. Associated values with HashMap
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 03:50 AM
  4. Sorting a hashmap
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 05:06 AM
  5. What is HashMap and Map?
    By TAIPA in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 11:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,440,802.82628 seconds with 17 queries