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.
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 3 years ago.
Improve this question
I understand that the items of a HashMap do not maintain or are not arranged in a constant order, although sometimes it does, so here's my question: What condition would make the HashMap print its output in that ordered form or is it an arbitrary process?
What condition would make the HashMap print its output in [order of insertion]?
The iteration order of the standard library's HashMap is deterministic, but it varies in ways that are difficult to predict. Factors affecting that order include the map's initial capacity and load factor, the hash codes (and to a lesser extent, the values) of the keys inserted into it and removed from it, and the order of insertions and removals.
For a given set of reasonable map parameters and a flexible-enough key type, it should be possible to compute key insertion / deletion sequences that result in insertion-order iteration of the final map contents, but only by sheer luck could you expect to see insertion-order iteration of a HashMap populated with real-world data.
For example, without testing or consulting the implementation, I speculate that if you use Integer keys (whose hash codes are their int values), chosen such that each ends up in a separate hash bucket and inserted in ascending order, then you might see them iterated in insertion order. This is not intended to be a recipe, just a mechanism to convey the kind of considerations that would be involved.
Overall, do not choose HashMap if you care about iteration order. No good can come from that.
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 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
As we have many Sorting algorithms,I wanted to select the proper sorting algorithm for my case.
For Ex:Insertion sort is best for Small case of numbers ,whereas Merge sort is best suited for Large case of numbers.
I dont know what is that small range of numbers means .i.e 1-100 or 1-1000 or so.
Probably what is the best case for sorting a list of numbers where the same set of numbers are present present repeatedly.I am planning to store it in a hash and then store the elements accordingly .
Whether doing in through hash is a better way or Using some sorting algorithm is the best way
But as here it contains the same data again and again
If you add some elements to already sorted array(list), then you have only small number of inversions. In this case insertion sort will work rather fast.
Alternatives - natural merge sort or TimSort(if implementation is available for your language) - these ones will behave good in all cases, including unsorted arrays.
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.