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