Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with Comparator

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Problem with Comparator

    Hello!

    I have a Treemap, and use a comparator, co, in the declaration. The goal is to write the map out in String-size-order.

            static JämförOrd co = new JämförOrd();
     
           static NavigableSet<String> na = new TreeSet<>(co);

    The comparator-class I've made look like this:

    public class JämförOrd implements Comparator<String> {
     
        @Override
        public int compare(String s1, String s2)
        {
            if(s1.length()< s2.length())
            {
                return -1;
            }
            else if (s1.length()>s2.length())
            {
                return 1;        
            }
            else return 0;
     
        }
    }

    When I don't use the comparator, the treemap is written out, but not in string-size-order. When I use the comparator, only a small part of the words are written. Why is that?

    /hank


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with Comparator

    Can you post a small, complete program that compiles, executes and shows the problem?
    only a small part of the words are written
    What Strings are NOT written?
    Have you read the definition for a Set? What will happen when 2 Strings are equal?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Comparator

    Well, the program is a Scramble-solver, which means that every combination of letters is checked in a dictionary. I guess that a set with that very comparator just have one word at every word length. Is that right?

    // Hank

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with Comparator

    just have one word at every word length.
    Yes. There won't be two Strings with equal lengths.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    iHank (November 30th, 2013)

  6. #5
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Comparator

    Thank you, Norm!

Similar Threads

  1. When to use Comparator and Comparable Interface in java
    By diyaots in forum Java Theory & Questions
    Replies: 7
    Last Post: October 26th, 2012, 09:35 AM
  2. Comparator interface question
    By brisingrbrom in forum Collections and Generics
    Replies: 3
    Last Post: February 24th, 2012, 10:54 AM
  3. Help needed with Comparator objects and Collections
    By iceyd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 5th, 2011, 02:41 PM
  4. Replies: 0
    Last Post: October 2nd, 2009, 10:51 PM
  5. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM