We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

multi elements in a set.

meow00
meow00 asked
on
Medium Priority
362 Views
Last Modified: 2010-10-05
Hello experts,

    Can I have one elements to appear several times in a set ? if not, how do I do it ?

 thanks.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
No - use a LIst
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
not if the elements are equals

you need to use a different collection, such as a List
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Sets are for containing uniqueue elements, ie. no duplicates

what is it u are trying to achieve?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
If you see here, it explains what a Set is used for

http://java.sun.com/docs/books/tutorial/collections/interfaces/set.html

Have a look thru here to determine the type of Collection that best suits your needs:

http://java.sun.com/docs/books/tutorial/collections/interfaces/index.html

Author

Commented:
So I have two collections:

 list1 = "A", "A", "B", "C", "C"
 and
 list2 = "C", "A", "A", "C","B"

 if the order I added to the list are different ...
 how do I check if they are the same ( ps. I need the above to list to be EQUAL) ... can list do that ?

 thanks.
CERTIFIED EXPERT
Top Expert 2016
Commented:
sort them first then apply list1.equals(list2)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
use an ArrayList and first sort them
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Collections.sort(list1);
Collections.sort(list2);
if (list1.equals(list2))
{
    ...
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
you couls also use the List's containsAll() method

Commented:
Add a new class with weight value
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.