Results 1 to 6 of 6

Thread: Associated values with HashMap

  1. #1
    Join Date
    Dec 2009
    Posts
    292

    Associated values with HashMap

    Hello,
    All this corresponds to a program that should provide the shortest path between two vertices of a weighted graph.
    My structure is this:
    Code:
    private HashMap <Summit, trst <arc>> graph;
    So I have a class that contains the Summit:
    Code:
    private String nm; 
    	private int plusCourt; / / Evaluation Associate at Summit
    And a class Arc:
    Code:
    private Summit arr;
    	private int wg;
    I created all vertices and all arcs. I entered the hashmap for each vertex the arcs involved. All this works wonderfully! Now I want to change the attribute plusCourt for all vertices contained in the hashmap.
    Code:
    for(S Summit: graph.keySet()){
    						
    			if(s.getName()== Departure)
    			{
    				sommetDepart = s;
    s.setPlusCourt(0);
    				
    			}
    			else
    			{
    				s.setPlusCourt(3255);	
    			}
    		}
    And in fact, after running this code, as I travel all the keys to display the value, I have only null?!? Logically if I change a key it should keep its value in the associated hashmap? Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Associated values with HashMap

    Hello,
    Just have a look at the code below, this will describe a function and I think you are interested in this.
    Code:
    if(s.getName()== Departure)
    No comparison == on strings. Your "null" where you have specifically?

  3. #3
    Join Date
    Dec 2009
    Posts
    211

    Re: Associated values with HashMap

    Hello,
    Even I am trying a code something like this. In fact I put this bit of code before and after treatment:
    Code:
    TreeSet <arc> t;
    		for(S Summit: graph.keySet()){
    					
    					
    			t = graph.get(s);
    System.out.System.out.println(t);
    		}
    It appears that everything is correct before and after I display with null.

  4. #4
    Join Date
    Dec 2009
    Posts
    292

    Re: Associated values with HashMap

    Hey,
    Here is my modified code, have a look and if I am wrong somewhere then you can correct me.
    Code:
    for(S Summit: lesKey){
    			Summit tsm = s;
    TreeSet <arc> tmpTree = graph.get(s);
    			
    			if(s.getName().equals(Departure))
    			{
    				sommetDepart = s;
    s.setPlusCourt(0);
    graph.remove(tsm);
    graph.could(s, tmpTree);
    				
    			}
    			else
    			{
    				s.setPlusCourt(3000);
    graph.remove(tsm);
    graph.could(s, tmpTree);
    			}
    		}

  5. #5
    Join Date
    Nov 2009
    Posts
    583

    Re: Associated values with HashMap

    Hi,
    Okay, you can add the following to your code and see how it is compiled.
    Code:
    HashMap <Summit, TreeSet <arc>> mtp = graph;
    Set <VERTEX> lsky = mtp.keySet();
    This is because AC reference the same objects in memory? You will have to create a new object again. Do, this and see if the program runs perfectly.

  6. #6
    Join Date
    Dec 2009
    Posts
    211

    Re: Associated values with HashMap

    Hello,
    Ok, I have modified my code and this is what I have with me now.
    Code:
    TreeSet <arc> t = graph.get(sommetDepart);	
    		
    		for(Arc: t){
    			
    			if(sommetDepart.getPlusCourt()+ a.getPoids()<a.getArrivee().getPlusCourt()){					
    				a.getArrivee().splcrt(sommetDepart.getPlusCourt()+ a.getPoids());
    			}
    		}

Similar Threads

  1. W32 registry values are not getting matched by the default values
    By Angrzej in forum Networking & Security
    Replies: 5
    Last Post: 19-05-2011, 12:23 PM
  2. HashMap vs HashTable
    By Joyjeet in forum Software Development
    Replies: 6
    Last Post: 04-01-2011, 08:07 PM
  3. Sorting a hashmap
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 04:06 AM
  4. Iteration over a hashmap
    By New ID in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 02:40 AM
  5. What is HashMap and Map?
    By TAIPA in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 10:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •