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: Comparing a list against another list, and storing it back into a list. Java

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Comparing a list against another list, and storing it back into a list. Java

    I am doing a mini project on "beauty contest". A list of contestant will be inserted as a list in such format `<Num <height, age, weight>>`.
    To be able to join the "beauty contest" the contestant have to meet the strict standard set by the organizer. `<Require_height, Require_age, Require_weight>`

    How do i set it up such that i can select the height of the first contestant and compare it with the height of the required height. If the contestant meet the standard, her information <height, age, weight> will be saved, else she will be "ignored"

    I have made a sketch out with my best effort as to what i am trying to do. Could anyone please guide me along or give me a helping hand ? *(I am not doing this as an homework or assignment, i just wish to satisfy my curiosity as to how it can be done)*
    It would really help a lot if someone can also teach me how to pull items out from a List<> to compare.

    Thanks in advance




     public static Map<Contestant, List<Contestant>> getRequiredstandard(
                			List<Requiredstandard> Requirements) {
     
                public int height;
                public int age;
                public int weight;
            foreach(Map<Contestant, List<Contestant>> getRequiredstandard(
                			List<Requiredstandard> Requirements)){    
     
            if (C.height >= Require.height)
                {
                List <critrial_Met> P1= new list<critrial_Met>
                p1 = <height, age, weight>;
                }
                elseif ( c.age <= Require.age)
                {
                List <critrial_Met> P1= new list<critrial_Met>
                p1 = <height, age, weight>;
                }
                elseif (c.weight >= Require.weight)
                {
                List <critrial_Met> P1= new list<critrial_Met>
                p1 = <height, age, weight>;
                }
     
                else
                {
                *Ignore and move on to next contestant*
                }
     
               }
     
          }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Comparing a list against another list, and storing it back into a list. Java

    The simplest approach is to just lay out the minimum criterion for a contestant, inside a Contestant object. Then you create two Lists, one holding every contestant and another storing only the ones who get through to the next round. After that, all you need to do is loop through the list with every contestant, compare against the criterion object and put the contestant through to the next round (into the new list) or send them home (do nothing). Once the loop has ended, you can discard the original list and only carry on with the new list.

    A short SSCCE demonstrating what I mean.

    import java.util.ArrayList;
    import java.util.List;
     
     
    public class Contestant{
     
    	private final float height;
     
    	public Contestant(float height){
    		this.height = height;
    	}
     
    	public float getHeight(){
    		return this.height;
    	}
     
    	public static void main(String[] args){
    		List<Contestant> nextRound = new ArrayList<Contestant>();
    		List<Contestant> contestants = new ArrayList<Contestant>();
     
    		contestants.add(new Contestant(2.0f));
    		contestants.add(new Contestant(4.0f));
     
     
    		Contestant minRequirement = new Contestant(3.0f);
     
    		for(Contestant contestant : contestants){
    			if(contestant.getHeight() >= minRequirement.getHeight()){
    				System.out.println("Contestant got through!");
    				nextRound.add(contestant);
    			}
    		}
    	}
    }

    Note: It is not my belief that people under 3 foot should not be allowed to progress further in beauty pagents!
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Comparing a list against another list, and storing it back into a list. Java

    Wow. Thanks Newbie. Just a side question, is it possible to make such implementation as a data structure. Using Quick sort perhaps or other form of data algorithm ?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Comparing a list against another list, and storing it back into a list. Java

    When comparing two different lists, then the approach laid out is the main way to go about it really. If you just wanted to sort the contestants in a single list easily by their height say, then you would use the Comparable interface. However, this doesn't really do the behaviour you need for comparing two lists, unless you use it for sorting and then finding values quicker using a binary search etc.


    If you're interested in this idea anyway, let me show you what the API states:

    This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
    and

    Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort).
    What this means, is that you implement the only method of Comparable, which has a signatue of:
    public int compareTo(Object other){}

    where object will be the generic type you specific and int will be the indicator for the natural ordering. If the value of the object is less than the value of the `other` object, then -1 will be returned. If they're equal, 0 will be returned. In the case of member variable being greater than `other`, then you guessed it, 1 is returned.

    If you implement the interface and the method alike, then using Collections.sort(), you pass in your List and bobs your uncle; your objects will now be order based on the natural ordering you defined.

    To help you better understand, I've concocted a quick and workable example which you can find below.

    Best of luck, and be sure to come back if you have any further questions.

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
     
     
    public class Contestant implements Comparable<Contestant>{
     
    	private final float height;
     
    	public Contestant(float height){
    		this.height = height;
    	}
     
    	public float getHeight(){
    		return this.height;
    	}
     
    	@Override
    	public int compareTo(Contestant other) {
    		if(this.height < other.getHeight()){
    			return -1;
    		}else if(this.height > other.getHeight()){
    			return 1;
    		}
    		return 0;
    	}
     
     
    	public static void main(String[] args){
    		List<Contestant> contestants = new ArrayList<Contestant>();
    		contestants.add(new Contestant(2.0f));
    		contestants.add(new Contestant(4.0f));
    		contestants.add(new Contestant(1.0f));
    		contestants.add(new Contestant(3.0f));
     
    		Collections.sort(contestants);
     
    		for(Contestant contestant : contestants){
    			System.out.println(contestant.getHeight());
    		}
     
    	}
    }

    This code gives the output of:

    HTML Code:
    1.0
    2.0
    3.0
    4.0
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  3. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 01:20 PM
  4. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  5. SpinnerListModel setList(List<?> list)
    By roy epperson in forum Collections and Generics
    Replies: 4
    Last Post: November 29th, 2010, 10:30 AM

Tags for this Thread