Results 1 to 6 of 6

Thread: TreeMap and duplicates

  1. #1
    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

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

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

  6. #6
    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.

Similar Threads

  1. Compare and merge two TreeMap
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 03:20 AM
  2. Sort listing a TreeMap with String
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 17-02-2010, 04:14 AM
  3. Deleting Duplicates in OpenOffice
    By Bansi_WADIA in forum Operating Systems
    Replies: 3
    Last Post: 24-10-2009, 06:41 PM
  4. How to manage duplicates ?
    By Blazej in forum Software Development
    Replies: 6
    Last Post: 21-01-2009, 06:41 PM
  5. Delete duplicates on Outlook
    By C.M.D in forum Windows Software
    Replies: 5
    Last Post: 02-09-2008, 03:51 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
  •