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

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.

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.

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

List.toArray(Object[]) performance [duplicate]

This question already has answers here:
.toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?
(8 answers)
Closed 4 years ago.
I'm getting a List of object A, then I use Apache Commons Collection4 to transform the obtained List from having A instances to having B instances.
listOfBs = (List<B>) CollectionUtils.collect(listOfAs, componentTransformer);
However, eventually I need to have an Array of Bs not a List.
So my question is, which is faster.
Convert the list using CollectionUtils.collect
Create an array using listOfBs.toArray(new B[listOfBs.size()])
Or
Loop over the listOfAs
Transform each A object to a B object
Add each B object to an array (B[])
The difference between the first approach and the second approach is that the first approach has much less code, but I'm not sure if the toArray method bares a hidden loop or expensive operations.
What I see in the second approach is that I'm sure I'll loop only once over the listOfAs list.
So which approach is faster ?
Don't be concerned about performance of List.toArray(), its complexity is linear as it will resort to a single loop internally.
As it is implemented with Arrays.copyOf, which eventually comes to System.arraycopy, that is implemented in native code it could be potentially even faster than a java-level loop.
Very interesting to read is this article:http://shipilev.net/blog/2016/arrays-wisdom-ancients/#_conclusion
It goes into great detail about the different ways to convert a List to an array.
Conclusion: do not use listOfBs.toArray(new B[listOfBs.size()]) as stated by you, but use listOfBs.toArray(new B[0]).
Believe it or not, this is faster.

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.

Difference between PriorityQueue and TreeSet in Java? [duplicate]

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.

Categories

Resources