Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I want to store a list of (Object, int) in a data structure such that the int field can be easily sorted (ascending or descending).
I'm looking at Hashtables and TreeMaps but i'm unsure which, if either, of these are good for this purpose.
The priority is the sort speed. Any suggestions?
Given what you've told us, it's hard to say what'd be best.
If you're concerned with sorting performance alone, a HashTable or TreeMap (actually a red-black binary tree) have great sorting performance, but they're slower than some other data structures when adding (and in TreeMap's case, deleting) items.
You should probably provide more details regarding what you're doing with the data.
Related
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
In Data Structures and Algorithms in Java, the advantages of ordered arrays are stated. One of the advantages, I wish I had some kind of real example for. This is not for homework, but just self-clarification. What are some real cases for when insertion/deletion is not frequent, but searches are frequent? Anything would help even if you can point me in the direction of some github repository. Thank you.
An example would be a dictionary. After it is built, it can be looked up millions of time. Like your paper dictionary, the words in it better be sorted.
While I like leeyuiwah's answer, a more common domain which you can see in commercial context is a data base of some entity, for example the customers or employees, for which normally you create a view. That's why we index them (make the retrieval faster). Indeed, after inserting some records most of the operations will be retrieval which includes a search (based on complicated conditions or a simple identifier).
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I was asked this in interview. I thought the question was too generic to specify a particular data structure.
However, still if we channelize the question to following criterion what would be the best data structure to use:
If insertion speed should be fastest?
If search of particular data is to be the fastest?
The HashSet provides both O(1) insertion and O(1) search, which is hard to top from a theoretical point of view.
In practice, for a size of 10.000 references, a sorted ArrayList might still outperform HashSet, although insertion is O(n) and search is O(log(n)). Why? Because it stores the data (the references at least) in a contiguous memory range and therefore can take advantage of hardware memory caching.
The issue with big-O notation is that it completely ignores the time required for a single operation. That's fine for asymptotic considerations and very huge data sets, but for a size of 10.000 it might be misleading.
Haven't tried it, though. And I bet your Interviewer hasn't either :).
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 have many records in to fetch and it could be in millions, I want to use hashmap, but I am not sure if that is ok or what would be the memory implications.
Please suggest
Thanks
VIvek
Welcome to StackExchange. A good place to start would be taking a look at this question:
Difference between HashMap, LinkedHashMap, and SortedMap in Java
HashMap is generally discouraged if you need to grab items in a specific order.
If you need to store 1,000,000+ records, you may want to look into an external database to store the information, as it will be memory consuming to hold all of the records.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
The question is not too dificult: How do I best implement a table in Java.
I wish to have something like a table in a database. Named columns and numbered rows. My values are Integer.
My inital idea: LinkedHashMap<String, ArrayList<Integer>>
But is there a better way of doing it?
What do you mean by "better"? It depends on what do you need to do with that.
You may build
a 2-dimensional array and a map of column names (C++ way)
a list of objects, where each object is a row (the most Java way)
a Guava Table
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What are some good data structures for keeping track of agents in a two-dimensional, spatial simulation?
I've seen some references to quadtrees (which I understand) and kd-trees (which I don't understand very well).
I'm looking for something through which an agent can efficiently say, "I know my location, and I would like to know which agents are near me (within a certain radius of myself)."
Examples (pseudo-code is fine) would be greatly appreciated.
I'm working in Java.
Well, I'm not sure exactly how it is implemented, but the MASON toolkit uses a discretization algorithm that places agents that are close to one another in the same "bucket" of a hash table. It makes for very fast lookups, as only a few of these buckets have to be checked for each query.
The best thing for you is probably to take a look at the source code here:
http://code.google.com/p/mason/source/browse/trunk/mason/sim/field/continuous/Continuous2D.java?r=529
I have found something called a Bucket PR Quadtree.