List of questions

1)How exactly does the implementation of set interface like HashSet and LinkedHashSet store the elements. Is it totally random?

2) Why is there no data structure like LinkedSet just like linked list? Does LinkedHashSet and LinkedHashMap have any relation with hashing?

3)What is the difference between add method(without parameters) in Collection interface and List interface? Is it overloaded or is inherited in the list interface?

4)Why is iteration slower in linked list compared to array list?

5)What is the need for peek and poll methods in Queue interface when we have get and set methods in Collection interface which is parent of Queue interface?

6)Where is the code where set interface which extends from Collection doesn't allow duplicates whereas list allows duplicates? I mean where excactly is the code to differentiate between two? How does using a set not allow duplicate entries?

7) Why dont we have a HashList just like we have a HashSet?

8) Is there a relation between synchronization and fail-safe and fail-fast iterator?

raja singh kumar wrote:1)How exactly does the implementation of set interface like HashSet and LinkedHashSet store the elements. Is it totally random?

Well, basically, it is a hashing algorithm. The hashCode() method is used, which is then munged a bit (supposedly to get a better distribution), and then, mapped to the range of available buckets in the set.

Obviously, it is not random -- as that is not the intent... but I guess, with the complex algorithm, it may be very difficult figuring out the order... which BTW, may change with different implementations, so probably don't bother.

raja singh kumar wrote: 2) Why is there no data structure like LinkedSet just like linked list? Does LinkedHashSet and LinkedHashMap have any relation with hashing?

7) Why dont we have a HashList just like we have a HashSet?

First, I am assuming that you have *not* taken a data structures and algorithm course in university -- apologies if this is incorrect.

Anyway, the Java collections follows the common data structures and algorithms taught in school. I guess the main reason is because there is a ton of research with these type of collections. Furthermore, many other languages and frameworks do the same thing.

Additionally, in terms of "why" questions, please keep in mind that we can only speculate -- unless of course, one of the original designers of Java happens to stop by the ranch.

In terms of speculation, I will say that the Java collections is designed like other common collections, which in turn, is designed after all the research. Additionally, the designers probably picked the more commonly used data structures, assuming that external libraries will be released for the more exotic ones.

There are no design reasons why one should be faster than the other, in terms of iteration. This means that it is likely an implementation detail (assuming that you are correct) and may not be true with different releases/versions of Java.

There are no design reasons why one should be faster than the other, in terms of iteration. This means that it is likely an implementation detail (assuming that you are correct) and may not be true with different releases/versions of Java.

It depends on how iteration is implemented. If you iterate using for-each or an (explicit) Iterator, then there should be no real difference in performance.

If, however, you iterate as below, then yes, LinkedList is horribly slower than ArrayList. The reason is in how get works. ArrayList is implemented using an array, and get simply access the specified array element directly. LinkedList works completely differently. It has a long doubly linked list of entries, each with a reference to the previous entry and the next entry. The LinkedList only stores references stored to the first element and to the last element. That means, if get is called, the LinkedList will have to go from the start of the list, and go all the way forward until it finds the entry for the given index. (If the index is larger than the midway point, it checks from the end instead of the start, but it still has to go through a lot of entries.)

Conclusion: just use for-each or an Iterator when you need to iterate over any List, Set or other Collection.