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

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.

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.

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.

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:

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.