Results 1 to 6 of 6

Thread: Compare and merge two TreeMap

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Compare and merge two TreeMap

    Hello,
    I have two TreeMap containing key Each person as a String (representing a name) and a Float object as value. Both TreeMap have the same list of key (the same name). I would like to compare my first treemap with the second and I fell on the same key, I want to record the amount of float associated. Do, you have any idea about this, if yes then please help me with it, any suggestion regarding this topic will helpful for me. Thanks in advance.

  2. #2
    Join Date
    Dec 2009
    Posts
    192

    Re: Compare and merge two TreeMap

    Hey guys,
    I was also trying for a similar kind of a program, but I did not found a solution for it still, this is what i have tired till now
    Here is my code
    Code:
    	Public TreeMap usrprf = new TreeMap(); / / my first treemap
    	Public TreeMap prf = new TreeMap(); / / my second treemap
    
    Iterator itUserProfil1 = usrprf.values().iterator(); / / iterator for my first treemap
    	Iterator itUserProfil2 = usrprf.keySet().iterator();
    
    Iterator = iprf1 prf.values().iterator(); / / iterator for my second treemap
    	Iterator = iprf2 prf.keySet().iterator();
    		
    		while(itUserProfil2.hasNext()){
    			while(iprf2.hasNext()){
                                     
                                    / / compare itUserProfil2 and iprf2
                                    / / if equal then we add itUserProfil1 and iprf1
     
    				}
    			}
    		}

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Compare and merge two TreeMap

    Hello,
    Normal that you had the error in the following code:
    Code:
    while(iuprf2.hasNext()){ 
             while(iprf2.hasNext()){ 
                                    / / compare iuprf2 and iprf2 
                                    / / if equal then we add iuprf1 and iprf1 
                                    if(iuprf2.next().equals(iprf2.next()))  {...}
     
                } 
             } 
          }
    In effect you take the following iuprf2 element for each iteration in iprfs2 then after some time there has more elements. of error or NoSuchElementException. Are you getting my point.

  4. #4
    Join Date
    Nov 2009
    Posts
    333

    Re: Compare and merge two TreeMap

    Hello,
    It would be better to use the method containsKey (Object)
    Code:
    Public TreeMap usrprf = new TreeMap();
    Public TreeMap prf = new TreeMap();
    
    Iterator iusrprf2 = usrprf.keySet().iterator();
    		
    while(iusrprf2.hasNext()){
       String key = (String) iusrprf2.next();
       if (prf.containsKey(key)) {
          ...
       }
    }
    As for the method putAll (Map)You'll lose data if your maps have 2 identical keys.

  5. #5
    Join Date
    Dec 2009
    Posts
    263

    Re: Compare and merge two TreeMap

    Hello,
    Thank you for your responses. I went to see the API in sun but I can not find a method to do directly what I want. The method putAll () copies all entries in a treemap treemap other. By causing the test I realized that when you insert a key already existing when the former is removed so I can not add the values corresponding. If you have other avenues to explore, you can post it here and help me. Thanks in advance.

  6. #6
    Join Date
    Nov 2009
    Posts
    583

    Re: Compare and merge two TreeMap

    Hello,
    I do not know this is going to help you or not, still have a look at the following code.
    Code:
    Comparator naturalStringOrder=new Comparator()
    {
        public int compare(Object o1,Object o2)
        {
            return ((String)o1).compareTo((String)o2);
        }
    };
     
    Comparator rvstrob=new Comparator()
    {
        public int compare(Object o1,Object o2)
        {
            return -((String)o1).compareTo((String)o2);
        }
    };

Similar Threads

  1. Mac OS X Merge Folder
    By Ravana in forum Operating Systems
    Replies: 6
    Last Post: 27-10-2010, 01:43 AM
  2. Sort listing a TreeMap with String
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 17-02-2010, 05:14 AM
  3. TreeMap and duplicates
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 02:06 AM
  4. How to merge vob files as one
    By Patio in forum Windows Software
    Replies: 3
    Last Post: 12-06-2009, 05:50 PM
  5. PDF file merge
    By active in forum Windows Software
    Replies: 5
    Last Post: 14-03-2009, 12:48 AM

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,714,115,048.38213 seconds with 17 queries