Results 1 to 6 of 6

Thread: Sort listing a TreeMap with String

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Sort listing a TreeMap with String

    Hello,
    I have a TreeMap containing String as key. By default the natural sort order is 0-9, AZ, az, CEAA, etc. I wish this sort alphabetically TreeMap "French", as in a basic dictionary, namely: 0-9, a | A | A, B | b, c | c | c | c, etc. (the '|' means 'or'). Roughly sorting case insensitive and accent.
    I know that we can pass a Comparator to the constructor TreeMap but I do not think we should redefine the compareTo in class String based on all accented characters? I have also seen that the method compare () in class Collator might help but I do not see how to bind the TreeMap and Collator. so, if you guys have any idea about it then, please do post back.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Sort listing a TreeMap with String

    Hello,
    I have also seen that the method compare () in class Collator might help but I do not see how to bind the TreeMap and Collator.
    Collator implements ComparatorYou can use it in your constructor TreeMap. So this allows sorting according to rules of practice in French:
    Code:
    Map m = new TreeMap(Collator.getInstance(Local.IND));
    And you can do this if you just want to sort by local user:
    Code:
    Map m = new TreeMap(Collator.getInstance());
    Finally if these rules do not suit you you can define your own, using the class RuleBasedCollator.
    Last edited by GlassFish; 17-02-2010 at 05:15 AM.

  3. #3
    Join Date
    Dec 2009
    Posts
    204

    Re: Sort listing a TreeMap with String

    Hello,
    Thank you very much ca sorts exactly as I wanted . This interesting class RuleBasedCollator I'll look at it in detail.
    I try to create a lexicon. I parse an XML file and adds in a TreeMap:
    word -> nb_apparitions
    eg
    bus -> 13
    the -> 128
    etc..

    Passing Collator.getInstance (Locale.FRENCH) in TreeMap, for a task containing about 300 000 calls TreeMap.put (), it multiplies by 20 the duration of the task.

  4. #4
    Join Date
    Dec 2009
    Posts
    192

    Re: Sort listing a TreeMap with String

    Hello,
    Even I am trying a similar kind of a program. The sorting is made new each call put () and needing the TreeMap sorted once it is fully completed, someone would have there a method to create a HashMap TreeMap from a temporary but providing more a Comparator? The constructor TreeMap (Map m, Comparator c) does not exist I do not see how we can do. If you know about it then please do post back with the answer. Thanks in advance.

  5. #5
    Join Date
    Nov 2009
    Posts
    356

    Re: Sort listing a TreeMap with String

    Hello
    Passing Collator.getInstance (Locale.FRENCH) in TreeMap, for a task containing about 300 000 calls TreeMap.put (), it multiplies by 20 the duration of the task.
    But this is normal because each call to put () will conduct at least one comparison (and probably more). And as the comparison method Collator is much more complex ... (the default method simply compares the value of the character). The constructor TreeMap (Map m, Comparator c) does not exist I do not see how we can do. Well I just made a copy by iteration and it works very well. I actually think that working on a HashMap and then copy it into a TreeMap is the best solution in this case.

  6. #6
    Join Date
    Nov 2009
    Posts
    330

    Re: Sort listing a TreeMap with String

    Hello,
    You can also use this method for putAll ().
    Here is the code for it
    Code:
    Map m = new HashMap();
    / / We fill the Map
    ...
     
    / / Then "copy":
    SortedMap sm = new TreeMap(Collator.getInstance());
    sm.putAll(m);
    this is the basic code and i think it will help you in the coding for your program. This is the basic methods and the logic you have top use in the coding.

Similar Threads

  1. Replies: 4
    Last Post: 15-04-2011, 10:48 AM
  2. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  3. Compare and merge two TreeMap
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 04:20 AM
  4. TreeMap and duplicates
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 02:06 AM
  5. How to sort numeric and string value in dataview
    By garfield1 in forum Software Development
    Replies: 3
    Last Post: 21-07-2009, 12:58 PM

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,711,678,193.40116 seconds with 17 queries