|
| ||||||||||
| Tags: duplicate entries, hashmap, javautil, programming language, sort table, treemap |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| TreeMap and duplicates
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());
}
} Thank you very much |
|
#2
| |||
| |||
| 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. |
|
#3
| |||
| |||
| 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. |
|
#4
| |||
| |||
| 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. |
|
#5
| |||
| |||
| 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());
}
} |
|
#6
| |||
| |||
| 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. |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |