Difference between PriorityQueue and TreeSet in Java? [duplicate] - java

This question already has answers here:
When should I use a TreeMap over a PriorityQueue and vice versa?
(11 answers)
Closed 9 years ago.
I am trying to understand when to use the two data structures. As far as I have understood the PriorityQueue is also implemented as a tree as the documentation states that the average time for insert, remove and contains is O(log(n)). The TreeSet also provides the same time complexity. Plus both of them are unsynchorized implementation. And I can write comparator for them to act like min heap or max heap.
Can some one point out in what conditions I use these two sets?

When you want a queue, use a PriorityQueue. When you want a Set, use a TreeSet. A TreeSet has unique elements, and doesn't offer the API of a Queue. A Queue doesn't offer the API of a Set, and allows multiple equal elements.

Related

List vs LinkedList vs ArrayList [duplicate]

This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 1 year ago.
On which basis should we choose to use List, LinkedList, ArrayList in Java?.
Also please explain which one is better to use?.
List is an interface, it should be the type of your variables or the arguments of your methods.
The other two are implementations of this interface.
LinkedList is optimized for inserts and deletes.
ArrayList is optimized for random access.
So, you can choose based on the kind of work you are going to do.

Strange behaviour of ProprityQueue [duplicate]

This question already has answers here:
How to iterate over a PriorityQueue?
(10 answers)
Closed 3 years ago.
I have configuration-elements put into a PriorityQueue. I am accessing the elements via
for (Element e : queue) {
...
}
This works well enough but after putting an additional configuration element into the queue, the ordering is disrupted. Even stranger, it depends on the sequence in which I enter the elements into the queue, whether sorting is correct or not.
Javadoc of PriorityQueue states
The Iterator is not guaranteed to traverse the elements of the priority queue in any particular order.
basically, a PriorityQueue cannot be used in a for-each construct if ordering is required.
Depending on the code in question, either use iterated queue.poll() calls or switch to another datastructure like List and sort this exlipcitly via Collections.sort().
As with the ordering being correct sometimes: You just got lucky at first.

Can we use LinkedList even though there is risk of memory overhead. In that isnt using ArrayList better option [duplicate]

This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 7 years ago.
I know that when it comes to search operation ArrayList is better. And when it comes to insertion and deletion operation LinkedList is better. But I have read that linked list will cause memory overhead. In that case is it still safe to use LinkedList. Is so in what situation we have to avoid using LinkedList even though our logic contains more of insertion and deletion operation
ArrayList is fast in search (iterating over elements), but LinkedList is fast in modifying (deleting, inserting in any position). Now it depends on you that what are you doing.
You can also refer to this stackoverflow answer

Time complexity regerding Iterator and for each-loop [duplicate]

This question already has answers here:
Which is more efficient, a for-each loop, or an iterator?
(7 answers)
Closed 7 years ago.
I am currently trying to figure out what's most efficient, implementing an iterator or a for each-loop.
The object that will be traversed is a class 'Graph', which is extended by a sub-class 'Graph.Vertex'. What should be most time efficient, iterating through the objects with an iterator or with a for each-loop:
Graph.iterator();
while (Graph.iterator().hasNext()) {
// Do something
}
vs.
for (Graph.Vertex v : Graph {
// Do something
}
This entirely depends on how you implement either of these.
The usual implementations of graphs make use of arrays to store vertices and will take only O(V) time for iteration where V is the number of vertices in the graph.
If your implementation differs from this, then you must supply more information in order to get an accurate answer as to the efficiency.

When to use each Java Collections data structure [duplicate]

This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 8 years ago.
I see that there are a ton of generic data structures provided in Java. They all implement List, so they can be used almost interchangeably, but when would I want to use each? Personally, I stick to LinkedList because it's something I'm "familiar" with. I'm not asking for an explanation of every single structure, but can you explain some of the more common ones and give their uses, as well as compare and contrast the uses of "Vector-like" structures?
It depends on the performance characteristics and behavior you are looking for.
For example in a LinkedList add, delete, and retrieve are O(1), O(1), and O(n), whereas for an ArrayList, the same operations are O(n), O(n), and O(1) if using get(int) and O(n) if using get(Object). However ArrayList uses less memory than LinkedList per entry.
One often uses Vector<type> to add elements to the structure that are part of the same collection, but do not have any relationship to other members (other than being part of the same collection). A LinkedList indicates that there is some sort of ordering that is important among the members of the collection.

Categories

Resources