Hashing with Linked Lists - java

I am trying to do a form of coalesced hashing, and to do so, I need to maintain multiple linked lists that get created when you try to insert something into the table and it collides with another object. How would I go about creating multiple linked lists inside of an add(object x) function and then be able to call the same list again in a find(object x) function?
For example, if my hash value is 5, and bucket 5 is occupied, I create a linked list with bucket 5 as a head, and then create a new node where the object I tried to put into 5 ends up getting put. This way when I try to find the object latter, rather than probe the table, I can just follow the linked list I created referencing slot 5 and follow it to my object.
My issue is, I can not figure out how to maintain multiple linked lists for different collisions, and then call the appropriate list later on when I try to find the object. Any help is greatly appreciated.

If you're trying to replicate something like HashMap (and it sounds very much like you are), you'll want to keep the linked lists in search tree, so that you can find the right list for inserting and for finding an object in reasonable time.

Related

Storing multiple values and picked entity with lowest value

I'm currently looking to add a section to my program where engineers are selected for jobs based on their distance from a job. I've used the google api to get the distance from one address to the other but how would I store the engneers ID, their distance from the address and select whos the closest to the destination? A linked list?
Thanks
You could use a linked list with nodes as an Engineer object class that you make yourself with ID, distance and job attributes, then traverse the linked list to find the Engineer(s) with the closest distance.
Or if all you care about is ID and distance, you could make a hashmap that hashes ID's to distances so that you only have the one hashmap instead of a bunch of link list node objects.
These are two solution out of many that you could use though, my suggestion would be to use whatever you are most comfortable with.
If you need a more specific answer, please make a more specific question.

What data structure is used in apps for modifiable lists?

In some apps you have lists of items where you can move items around, delete items, add or insert items, etc.
Normally I'd say an ArrayList would work but apparently a lot of operations are linear time.
Is there a better data structure most people use for this?
If your priority is inserting and/or removing elements from a collection that maintains an arbitrary order, the the LinkedList class bundled with Java meets that need. You can very quickly insert or remove any element at any specific index number.
Each link in the chain that is a doubly-linked-list knows its predecessor and its successor. Each element holds a reference/pointer to the element in front and another reference/pointer to the one following. So insertion means telling a linked pair to consider the new element as their successor or predecessor. The rest of the chain remains untouched.
The downside to LinkedList is that access by index number is expensive as finding the nth element means traversing n links going from one element to the next in the chain. A linked-list inherently means sequential access. So, getting to an element is expensive but once there the mechanics of the insertion/deletion is cheap.
Another downside to LinkedList is searching, for similar reason (sequential access). Since the ordering is arbitrary and not sorted, there is no way to approximately predict/expect where the element might be found. So searching means traversing the chain from one element to the next and performing a comparison on each one.
On the other hand, if indexed access is your priority, then ArrayList is the way to go. Directly accessing the nth element is the speciality for ArrayList. Inserting and removing elements are very expensive operations requiring the backing array to be rebuilt unless dealing with the last element. For large arrays this has implications for memory management as the array must be in contiguous memory.
Both LinkedList and ArrayList allow duplicates.
Neither LinkedList nor ArrayList are thread-safe. So if accessing either from more than one thread, you have a whole other category of concerns to address.
To understand the nuances, study linked lists and arrays in general.

Best complexity for synchronizing a map with a collection

I have the following problem for which I would like a decent solution.
I have a HashMap that contains some objects in the form of String(email) and object(Person).
This map is populated via a collection via a method updatePersonList(Collection list) as described below:
Every time a new collection is received via the above method the map will basically add all the elements from the collection to the map. That is all the map needs, the latest collection. What is not in the collection should be discarded from the map.
Now, I want to know how can I update efficiently the map because, as it can be read above it is possible to have the following scenarios :
1. Some objects can be found in both the map and the collection, therefore, only the new objects from the collection should be kept and not all.
2. Objects that are in the map but are not in the collection should be removed.
What is the best solution in terms of complexity?
After some investigation I came with the remove of all the objects from the map and add the ones from the collection. If someone knows something better would be nice if it can be shared.
You will never get better than O(n+m) where n is the size of your Collection and m is the size of your Map because you will always need to read at least both ones.
So in O-notation you could simply erase the hole Map and create a new one.
But in reality the constant might be not so unimportant and you also may want to reduce garbage collection. In this cases it might make sense to iterate through both and only delete the needed entries from the Map and add the new elements from the Collection to the Map.
But only profiling will tell you if you gained anything for that effort.
According to my point of view Use Treemap instead of list to improve your iteration performance i.e (Treemap will take only unique values) so that newly inserted objects will identify automatically
After succesfully design treemap don't remove markers on the map instead add news markers which will define in treemap
i hope it will help
If list has often the same content as map you can check it before cleaning the map and adding new entries.

what to use for this requirement, Array, List, Map,?

While making my program i have come across this requirement that i have to assign unique id's to some Objects that i create. Now i am creating the objects dynamically on GUI, and initially i used simple counter to assign int value to the created node, and it worked just fine.
However the problem that this approach creates is that if while creating the GUI, if some node has to be deleted, this id is also removed and is never used again. With the next new node, everytime i have to use the latest counter value and this creates lot of missing int values if nodes are deleted during the process.
I wanted to reuse those missing id's upon creating of new nodes, for this i am confused which approach i should addopt.
MY Ideas:
Using a ArrayList that contains the available values, plus if a node
is deleted, it's id is added to this list, i sort this list and use
the minimum value for new node. Fine but, when i use this value, if
i remove it from List, the index is not deleted and this causes
problem.
HashMap, similarly like above i add available id's and remove not used, but not sure how to sort this hashMap???
Can you suggest how i should go about it? May be i need some kind of stack where i can push values, sort it and use the minimum value, and if that i used, it is removed from this stack, please give some ideas about this how to accomplish this task???
Keep a list of the deleted IDs, and when you create a new node, check that list for an ID to re-use (doesn't matter which you take); if the list is empty (as it will be initially), get a new ID "the old way". Even more clever: make the list an object that will generate a new ID if there aren't any deleted ones in it, so the caller doesn't have to worry about HOW the ID was arrived at.
You could use a TreeSet (which automatically sorts all entries added from least to greatest) to store the deleted id's (myTreeSet.add(old_id)). That way, when you go to create a new instance, you would check to see if there are any entries in the TreeSet first. To grab the lowest value, you would use myTreeSet.first() (which should be an O(1) operation). If the TreeSet is empty, which means all known id's are currently in use, then you would go ahead and use the next available id as normal.
How about a TreeSet to store the used IDs? You could then use higher(0) to find the lowest free ID. If it returns null, then you know that you have no used IDs.
The first solution works fine only if you have few nodes! Imagine an application with thousands nodes! What about memory consumption?
The Hashmap solution is better to you aims and need less controls.

add to list without bringing whole list into memory

I have a web service that holds information for users. Each user has a list of items, possibly thousands. I want to add an item to the list without loading the entire list. Is there a list implementation that allows you to add elements to the list without bringing the entire list into memory?
A Doubly Linked List. By definition it's not necessary to traverse the list to add something to the end since it contains a pointer to the end.
the list is on a remote database. –
Lumpy
So the list is in a database, so it's not really a Java List? It's just a bunch of database rows? In that case why not just do an insert into the database to add another row?
INSERT INTO list VALUES 1, 2, 3;
Lazy proxies. You can use a JDK dynamic proxy (java.lang.reflect.Proxy) where you store only the information needed for retrieving the items from the database, not the items themselves. Only when calling the get(..), size(), contains(..) methods - fetch the data.
However I have a feeling that you are doing things the wrong way. Give more details about your implementation.
None that I know.
With middlewares such as Terracotta, some collections (such as maps) can be loaded on-demand and partially, but this doesn't exist as-is in the standart JDK.

Categories

Resources