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



TreeMap and duplicates

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 10-02-2010
Member
 
Join Date: Dec 2009
Posts: 211
TreeMap and duplicates
  

Hello
I have a problem with TreeMap, but different from what one can usually find it to say that I would keep the duplicates and not delete them. I have to use restriction necessarily a TreeMap, which must be sorted by name. Here is my code for it
Code:
public static class cmch implements Comparator {
		  
		Public int compares(Object o1, Object o2){
		  
			if(o1 == null & & O2 == null) return 0;
			if(o1 == null) return -1;
			if(o2 == null) return -1;
			
		    return ((Room)o2).getDesignation().compareTo(((Room)o1).getDesignation());
		  }
	}
Everything works very well except that it turns me all the duplicates, which is normal since my comparator is on the "designation" ... In short how to achieve the same sort, but with duplication using the same TreeMap?
Thank you very much

Reply With Quote
  #2  
Old 10-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: TreeMap and duplicates

Hello,
I think that it is not possible to keep the duplicates in a TreeMap. Is it that search did not return other criteria to compare your rooms as it is in your example code? Rooms may have the same name chambre250 but another unique identifier. Are you getting my point, what I am trying to explain you, please do not get confused in this.
Reply With Quote
  #3  
Old 10-02-2010
Member
 
Join Date: Dec 2009
Posts: 211
Re: TreeMap and duplicates

Hello,
Yes of course they all have a name ... only the compareTo can sort the TreeMap ... Each "put" the comparative method is called to add the item to the right place ... If I put in comparing identifiers instead of names I classify them by identifying and this is not what I want. If you have any other alternative for this then please let me know. Thanks in advance.
Reply With Quote
  #4  
Old 10-02-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: TreeMap and duplicates

Hello,
Unless I am not mistaken method of TreeMap could take as parameters a key and a value. This line: listeOccupationChambre.put (zoneTmp) does not compile. What is the key you are using? Once I did something similar, I'd have to compare the elements of a List. I make the comparison at the beginning Element.attribut1 s they are equal I pass the second element. but I do not know if it will work on a TreeMap. If you need you can try out.
Reply With Quote
  #5  
Old 10-02-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: TreeMap and duplicates

Hello,
I have not tried what you are talking about, but I think i have a code which can help you. Just have a look at it.
Code:
Public static class cmch implements Comparator {
		  
		Public int compares(Object ob1, Object ob2){
		  
			if(ob1 == null & & O2 == null) return 0;
			if(ob1 == null) return -1;
			if(ob2 == null) return +1;/ / To stay commutative
			
		    int rs =((Room)ob2).getDesignation().
                       compareTo(((Room)ob1).getDesignation());
                    if (result! = 0) return result;
                    / / same name, but the id must always be different => result always! = 0.
                   return ((Room)ob2).getId().compareTo(((Room)ob1).getId());
		  }
	}
Reply With Quote
  #6  
Old 10-02-2010
Member
 
Join Date: Nov 2009
Posts: 347
Re: TreeMap and duplicates

Hello,
There is something that I did not understand, logically "two spot" of treemap pointed with two different keys may very well have the same object? no? So unless I have absolutely nothing has understood what you want to do (it is a possibility that more than likely ) I do not understand or is your problem. I think you are confused in you code and that is why you are getting an undesired results.
Reply With Quote
Reply

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



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "TreeMap and duplicates"
Thread Thread Starter Forum Replies Last Post
Compare and merge two TreeMap TechGate Software Development 5 23-02-2010 03:20 AM
Sort listing a TreeMap with String Ash maker Software Development 5 17-02-2010 04:14 AM
Deleting Duplicates in OpenOffice Bansi_WADIA Operating Systems 3 24-10-2009 06:41 PM
How to manage duplicates ? Blazej Software Development 6 21-01-2009 06:41 PM
Delete duplicates on Outlook C.M.D Windows Software 5 02-09-2008 03:51 PM


All times are GMT +5.5. The time now is 11:23 PM.