• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set And List  RSS feed

 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Set and List, so bare with me please.
I have this code:

I worked on it and everything is cool, until i got to a place where order is important, I read around and found that I should have used list instead.
My questions:
1) How do I declare a list ? Is this correct:

2) I don't want to allow duplicate entries, that is done automatically in Set, how to do this in List ?
3) How do I randomly shuffle a List? Can a set be shuffled?
4) Can you demonstrate a loop over a list ?
5) commands like (add, remove, contains .. etc) are also available in list ?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to the Java Tutorials! Find the bit about Set interfaces and what a Set is (the API documentation for Set and List are also worth reading).

You see what the difference between a Set and a List is?

You will notice there are hybrid implementations, which incorporate a linked list into another interface. I think you may want a LinkedHashSet. You can use almost the same declaration simply using "LinkedHasSet" in place of HashSet after the "new."

For your declaration of a List you would put the name of something implementing the List interface after the new in your 2nd code snippet. The two most popular List implementations are ArrayList and LinkedList.
2: It is pretty awkward to exclude duplicates from a List, since the essence of a List is that it permits duplicates. You could try if(!myList.contains(nextObject)) but "no duplicates" really suggests "Set."

3: For shuffling etc find the Collections class which has all sorts of methods like that.
4: Yes, you can loop over a List.
5: Yes, Lists implement all the methods of the Collection interface.

CR
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I worked on it and everything is cool, until i got to a place where order is important, I read around and found that I should have used list instead.


If order is important, there are Set implementations that deal with this. If insertion order or access order is preferred, you can use the LinkedHashSet class. If a particular (sorted) order is needed, you can use the TreeSet class.

Henry
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that shuffling a Set does not make sense since, in general, a Set does not require a specific order. Shuffling implies changing the order.

You can shuffle the order of a LinkedHashSet though, by using a List as intermediate collection:

This won't work with HashSet and TreeSet though, since these either have no order at all (HashSet) or a specific order (TreeSet).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!