Sorting the Collection(java.util.Collection) of Elements

Discussion in 'Java' started by Pradeep, Jan 24, 2007.

  1. Pradeep

    Pradeep Guest

    Hi EveryBody,

    Suppose that a collection(Java.util.Collection) contains some number of
    elements and I have to iterate through the collection by comparing each
    element in the collection with each other element so that no 2 elements
    should be same...(Note: No two same elements having the same index
    position in the collection shouldn't be eligible for comparison)...If
    any one of u have any Idea so please Let Me know that..

    Thanks& Regards,
    Venkata Pradeep K.
    Pradeep, Jan 24, 2007
    #1
    1. Advertising

  2. Pradeep

    Lew Guest

    Pradeep wrote:
    > Suppose that a collection(Java.util.Collection [sic]) contains some number of
    > elements and I have to iterate through the collection by comparing each
    > element in the collection with each other element so that no 2 elements
    > should be same...(Note: No two same elements having the same index
    > position in the collection shouldn't be eligible for comparison)...If
    > any one of u have any Idea so please Let Me know that..


    I strongly suggest that you read the java.util (note: lower-case 'j') docs:
    <http://java.sun.com/javase/6/docs/api/java/util/package-summary.html>

    This question has been coming up a lot lately. If this is homework you might
    need to understand how to make such a collection. If that is the case, the
    correct way to go about doing the homework is not to provide a canned answer
    that you don't fully understand, but to think the problem through.

    Try first progrmming with a pen[cil] and paper. Think through the questions of
    your problem in detail. For example:

    If you have to iterate through the collection (is that really the requirement,
    or can you check at the insert attempt?), what does that mean? What is
    required at each loop of the iteration? What does it mean to "compare each
    element in the collection"? Does this require, say, nesting another loop
    inside, or is there another way? What do the API docs have to say? What are
    the initial conditions: do you only know that there is a Collection, or can
    you restrict it to some subtype? If you can, then what subtype would help?
    WHat are the differences between different Collections, or even different
    implementations of the same Collection (such as the many flavors of List)?

    As you break down the problem into teeny, tiny little steps it will become
    clearer how to represent those steps in a program.

    - Lew
    Lew, Jan 24, 2007
    #2
    1. Advertising

  3. Pradeep wrote:
    > Hi EveryBody,
    >
    > Suppose that a collection(Java.util.Collection) contains some number of
    > elements and I have to iterate through the collection by comparing each
    > element in the collection with each other element so that no 2 elements
    > should be same...(Note: No two same elements having the same index
    > position in the collection shouldn't be eligible for comparison)...If
    > any one of u have any Idea so please Let Me know that..


    Why not just create a HashSet with the elements?

    Patricia
    Patricia Shanahan, Jan 24, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael T. Peterson
    Replies:
    2
    Views:
    725
    Michael T. Peterson
    Sep 9, 2003
  2. mgalaty
    Replies:
    0
    Views:
    2,708
    mgalaty
    Sep 10, 2004
  3. amy j
    Replies:
    4
    Views:
    870
    Alex Hunsley
    Oct 13, 2004
  4. yogesh
    Replies:
    4
    Views:
    5,018
    anish.mathew84
    Dec 30, 2009
  5. Øyvind Isaksen
    Replies:
    1
    Views:
    938
    Øyvind Isaksen
    May 18, 2007
Loading...

Share This Page