my dog learned polymorphism
The moose likes Beginning Java and the fly likes Why doesn't Collection Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why doesn Watch "Why doesn New topic

Why doesn't Collection Interface "set" have a get method like List?

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Why does an Collection.List have an Iterator and a get() method
While a Collection.Set does only have an iterator?
Thanks for your answers.
class Collection_List_Test1{
public static void main(String[] args) {
ArrayList alst = new ArrayList();
alst.add("anna"); //Dublette; add() liefert "false" und
//f�gt Dublette nicht zum Set hinzu!!
System.out.println("Number of Elements: "+alst.size());
// Iterator myIterator = alst.iterator();
// while (myIterator.hasNext() ) {
// System.out.println(;
// }
for(int i=0; i<alst.size(); i++)
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
A List has a defined order. A Set does not, so set.get(i) is not a meaningful concept.
It would make sense to have a get(i) operation for SortedSet and TreeSet, but there isn't one.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
I agree. Here's the link:
subject: Why doesn't Collection Interface "set" have a get method like List?
It's not a secret anymore!