Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Sorting the Collection(java.util.Collection) of Elements

Reply
Thread Tools

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

 
 
Pradeep
Guest
Posts: n/a
 
      01-24-2007
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.

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      01-24-2007
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
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      01-24-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to avoid the sorting of elements in the pallete component(netbeans) jaysaj Java 0 08-29-2011 01:07 PM
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
fired event Sorting which wasn't handled - sorting and SelectedIndexChanged Jason ASP .Net Web Controls 0 10-04-2006 02:19 PM
sorting XML by multiple elements R XML 4 05-30-2006 05:32 PM
sorting by multiple criterias (sub-sorting) Tom Kirchner Perl Misc 3 10-11-2003 05:16 PM



Advertisments