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 4 of 4

Thread: Help needed with Comparator objects and Collections

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help needed with Comparator objects and Collections

    I'm having issues with using Comparator objects and Collections and would be grateful to receive assistance on the following:

    Write code for a Comparator object that orders List<Integer> objects in order of their length. Then write code for a Comparator object that orders List<Integer> objects in the order of the sum of their integers (the sum is all the integers in the list added together). Use these Comparator objects and the two-argument method max from Java’s class Collections to find the longest list and the list which adds up to the most from the set of lists from a HashSet<List<Integer>> object, for example as created in question 2.
    The following is the code that I already have (to summarise it's consists of a HashSet<List<Integer>> object that will store values of type List<Integer>, I'm at the point now where I need to re-order the HashSet and then run both Comparators):

    import java.util.*;
     
    class Ex5b implements Comparator<HashSet<List<Integer>>>
    {
        public static void main(String[] args) 
        {
            Scanner in = new Scanner(System.in);
            String line, ask;
            boolean run = true;
            HashSet<List<Integer>> hs = new HashSet<List<Integer>>();
     
            while(run)
            {
                System.out.println("Enter some integers (all on one line):");
                line = in.nextLine();
     
                String[] words = line.split(" ");
     
                List<Integer> l = new LinkedList<Integer>();
     
                for(int i=0; i<words.length; i++)
                {
                    l.add(new Integer(words[i]));
                }
     
                hs.add(l);
     
                System.out.println("Would you like to enter more integers?: (y/n)");
                ask = in.nextLine();
                if(ask.equalsIgnoreCase("N"))
                {
                    run = false;
                }   
     
            }
     
            System.out.println("Contents of list: " + hs);
     
            //PLACE WHERE I AM STUCKFOR REST OF CODE
        }
     
        // ATTEMPT TO CREATE A COMPARATOR THAT WOULD SORT HASHSET IN ORDER OF LENGTH OF EACH LIST 
        public HashSet<List<Integer>> compareLength(HashSet<List<Integer>> hs) 
        {
            HashSet<List<Integer>> hsLength = new HashSet<List<Integer>>();
     
            // INCOMPLETE - I PRESUME THAT THIS SHOULD BE DONE CONSTRUCTIVELY AND NOT DESTRUTIVELY - ALSO MEANS RETURNING A HASHSET WHICH I AM HAVING PROBLEMS WITH (NON-STATIC METHOD BEING REFERENCED FROM A STATIC CONTEXT)
     
        }
    }

    INTERFACE FOR COMPARATOR(S)
    import java.util.*;
     
    interface Comparator<T>
    {
         public T compareLength(T o1);
    }
     
    // I PRESUME I WILL REQUIRE ANOTHER METHOD FOR ORDERING BY SIZE HERE

    I'd be grateful if anyone could assist me as I've been working on this on/off over the last couple of days and are completely lost...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with Comparator objects and Collections

    What method of what class are you using that requires a Comparator object?

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with Comparator objects and Collections

    Oh would I require another class that contain a method which would run the comparators?

    I'm pretty new to using Interfaces if the case...

    Thanks for replying.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with Comparator objects and Collections

    First I'd recommend you read up on what an interface is for and how to use them.

    What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)
    Lesson: Interfaces and Inheritance (The Java™ Tutorials > Learning the Java Language)

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. collections
    By bardd in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2011, 09:31 AM
  3. how to sort objects with collections???
    By kyros in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 02:21 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