Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it better to use linked list over arraylist for implementaion of Queue?
If I dequeue , the over load would be less in the case of liked list implementation?
The time it takes to dequeue an element from the beginning of a LinkedList is less than from ArrayList.
This is because ArrayList is based on an array, and when a first element is removed all elements except the dequeued need to be shifted one position left. The greater the number of elements there is in ArrayList, the longer it will take.
In case of LinkedList, no matter how big the list is, there just constant number of references that should be updated in order to dequeue a first element.
Of course you can dequeue elements from the end of an ArrayList and it will take constant time (most of the time), but than addition of a new element (to the beginning) will require to shift all elements one position to the right.
There is already a LikedList queue implementation in Java. I did not analyse the code but I would guess that a LinkedList if faster than an ArrayList in dequeuing an entry.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I need to program an application in Java language that has a pre-defined number of categories,
but the exact items in each category are unknown. Which one of the following data
structures I would use and why? Array, singly linked list, or doubly linked list
I'd go with a map that contains a List, the implementation is irrelevant.
Map<Category, Collection<Item>> categoryMap
If the categories are ordered, use a TreeMap
If they're not ordered, use a Hashmap
If the items are known to be unique I'd have the Collection be a Hashset;
If the items are unique and ordered, I'd use a Treeset;
If they're non-unique, I'd use an ArrayList.
I see no reason to use a LinkedList of any variety if the number of entries are known. The reason to use LinkedLists is if there's to be many insertion/removal operations.
In almost all use cases an ArrayList is the superior implementation to use. The only real downside to an ArrayList is if it's gets modified often, especially added to as it uses a "double up" method for resizing the internal array. Basically, if it needs more room, it doubles the size of the existing array and then copy the items from the old array into the new array. That can cause memory issue for large lists, but you'd need to be dealing with thousands of entries.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I wonder how to get the number of items in a ShortBuffer.
I want the number of items that are really in the buffer, not the maximum capacity.
Thanks.
Buffer is not a Collection, but a (comparatively thin) wrapper around a primitive array that provides some useful methods for operating on groups of primitive values. Like a primitive array, it always contains values for each valid index.
Therefore the number of items is always equal to its capacity.
It does not keep track of which indices have already been written to since its creation. And as one major use case is to wrap an existing array while still reflecting all changes to the wrapped array, that would not even be possible to implement.
ShortBuffer holds a position that keeps track on where to put elements. You can use it to know how many elements you put in. The number of elements always equals its capacity as others mentioned.
#Test
fun testShortBuffer() {
val shortBuffer = ShortBuffer.allocate(1024)
println(shortBuffer.position()) // position 0
shortBuffer.put(1)
println(shortBuffer.position()) // position 1
shortBuffer.put(shortArrayOf(2, 3, 4))
println(shortBuffer.position()) // position 4
shortBuffer.clear()
println(shortBuffer.position()) // position 0
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to create my own singly linked list in java and then use it to perform sorting.
I only have access to first node and next is a pointer used to point to next node.
Every node contains 2 fields x and y which are both integers.
How do I add elements to a linked list without using the built in methods and the sort them using either quicksort or merge sort?
How do I add elements to a linked list without using the built in methods and the sort them using either quicksort or merge sort?
Fast algorithms like quicksort and mergesort depend on being able to >>index<< the array of elements being sorted; i.e. in operation sequences like a[i] = a[j]. Using a linked list means that random indexing will be an O(N) operation. This is likely to turn an O(N log N) algorithm into an O(N^2 log N) one.
If you look at Java's built-in sort methods (in various versions of Java), you will see that sorting a LinkedList is done by:
copying the list to a temporary array,
sorting the array, and
clearing and copying the array back to the list.
This is the most efficient approach for sorting a large linked list.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've implemented it successfully with count, the book I'm using to learn just shows the code but without any proper explanation.
I've tried doing some search on google but can't find a material explaining it.
All what I've understood that Front and Rear points will assume the same location if the array is empty or full, so we are using an extra slot in the array "to do some sort of checking I don't understand to get it done".
Does anybody have a tutorial on this? I'm sorry if this comes as waiting for someone to feed me information, but I truly cannot find anything.
The math is pretty simple: if you have the beginning and the ending indexes, you can have three situations there:
The ending index is lower,
The beginning index is lower, and
The indexes are the same.
When the ending index is lower, say, b=10 and e=3, all the data is located between the two indexes. You can compute the count by subtracting the ending index from the beginning index, b-e: in my example, you've got seven items in the queue.
When the beginning index is lower, say, b=3 and e=10, then the data goes around the end of the queue. Say, the total size of your queue is N=100. Then the number of data items is b+N-e, or 3+100-10=93 elements.
The fact that you would be able to use at most N-1 elements follows from these two formulas: the highest number of entries that you can have is N-1 for both formulas, because the situation when all N entries are filled up is indistinguishable from the situation when the queue is empty.
You can check if your queue is full by comparing (b+1)%N to e. If they are the same, you cannot put any more data into your queue.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have no any special ideas to have my collection ordered or not to allow duplicates in the collection. In my entity class what should I use ? Set or a List? Is there any consideration in performance wise ?
Lets say we want to perform CRUD operations on this entity . I don't want to have the results ordered . So what should I use ? List or Set in the Entity. What should be user in order to maximize performance
What are the valid cases of using List and Set in an entity considering performance ? Not for ordering or to restrict duplicates?
As mentioned in another post given in the links in comment by vels4j, the following is definitely true.
List: Allows duplicate elements in it.
Set: All elements should be unique.
If you talk about performance, one point that I'm aware of is as follows:
If you take a List & map it to a table using Hiberante, you need to add an extra column as the index. This column will work as position/index/order of the element in the List because List is an ordered collection. See this for more details on index.
In case of Set no such column is required.
Now take an example where you need to remove an element from the List/Set.
In case of List, after removing element, you need to update all other elements of the List to update their index. This is an overhead.
While in case of Set, as you don't have index column, you need not to worry about other elements' index.
So my suggestion is: If you don't need your collection to be in a particular order while fetching it from the database, you should go for Set.
Find an element by value in a HashSet is O(1). In an ArrayList, it's O(n). But dictionary structure HashSet is probably a bit slower than an ArrayList. And ArrayLists are faster at random access.