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 4 years ago.
Improve this question
I have this assignment to complete. (I am new to java). I started to think about it and establish a plan. I am not looking for the answers just a feedback on my approach. I am supposed to enter integers into a hash table using a chaining scheme within the hash table (different from the regular chaining scheme). My idea is to use an arraylist so I can store data + pointer in each slot of the hash table. when a collision occurs, find an empty slot, insert the new integer and set the pointer from the original hashed slot to this new position in the arraylist. This way I am building a sort of linked list within the array. does that make sense? there is a hint about keeping track of the free space with a stack... here I have to say I am not sure how to use the stack in that instance
So prior to java8 hashmap internally will resolve collision using link list similar to your approach which brings the performance from O(1) to O(n) in case collision occurs, since java8 it is being handled via balanced tree which further improves the complexity to O(log n) in case of collision.
Your approach to use list is correct, but it comes to how you implement it.
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 5 years ago.
Improve this question
Currently my program is filled with many ugly references that often make field or method access look like this: weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2. I want to move to shorter and faster strong references like field1.field2. But my program design is such that I will also have to go for an ArrayList element-by-element search (in a for-loop) instead of accessing a WeakHashMap by get() method.
Thus, I'd like to understand, can moving to simpler references compensate for rejecting HashMap performance wise. Herewith I presume that WeakHashMap.get() is much faster than a loop-search of ArrayList.
Can someone, please, give me a rough estimate? Or maybe there's even an appropriate comparison table like this one. I'd appreciate that.
Thank you.
Currently my program is filled with many ugly references that often make field or method access look like this:
weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2
Given that the objects involved are not Data Transfer Objects
this is a violation of the law of Demeter aka Don't talk to Strangers / Tell, don't ask!
Following this LoD principle you should move the operations working with the data in field2 to a new method in the class SomeCustomObject.
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 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
Should I use ArrayList, Vectors, HashMp or is there anything else I should use to store data from a bank program I am working on.
I am trying to store the user information in an one of them and then send it to a file, which should I use?
Thank you.
To decide between using an ArrayList/Vector or a HashMap depends on whether you have key-value pairs or simple a list of elements. For key-value pairs a HashMap is the go-to option (e.g name - person object). If you just have objects but no real keys (a list of trees in a forest) then a ArrayList or Vector would be better.
The difference between Vector and ArrayList is more subtle. If you want more information on the difference between the two you can read this article. But as the article is from 2001, the information isn't the newest.
In most cases, the ArrayList is the better choice and Vector is pretty much considered deprecated nowadays. As the biggest difference between the two is, that Vector is synchronized while ArrayList isn't, which in most cases, makes the Vector slower than the ArrayList as it creates unnecessary overhead. For more information you can look at this question: Why is Java Vector class considered obsolete or deprecated?
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 8 years ago.
Improve this question
How to look for a particular object in a large list(java collection) without iterating it. Assume we have a large collection and just need to check whether a object exists or not without iterating.
Ok, Let's step out of the binary world.
Think of a chest full of Lego parts. You want a 2x2 flat black piece.
How would you find it without looking in the chest?
There is no magical to find it, you need to jump into the chest and find the piece grabbing one by one and checking if it's the one you are looking for.
There are ways to speed up the process.
You can Organise (sort your collection) by colour for example and just look in the black pile.
Or you can map (Index your pieces) so you know the position of the piece and can go and retrieve from you know where the piece is.
That is, in a very simplistic way, the same idea for databases and collection.
So, summarizing, no, you can't not just find without looking. Sorry :(
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 8 years ago.
Improve this question
I need som help understanding how this works! how do I go about calculating the complexity of 'Computing the first half of an array of n items' or 'displaying the third element in a linked list' ? I need someone to explain how this works, theses are just examples, so be free to use your own if it helps explaining! thank you.
You should look at how the processing time of the algorithm grows as the size of the input grows. I'll take your two concrete examples:
Computing the first half of an array of n items
We need to process n/2 items. If n doubles, then the processing time should also double. Consequently, this is a linear operation (i.e. O(n)).
displaying the third element in a linked list
We always want to display the third element, so the size of the list doesn't actually matter. If it doubles, we don't care; the processing time is not affected. Consequently, this is a constant-time operation (i.e. O(1)), it doesn't depend on the size of the input.