I want to access the last update time of entries inside a hashmap. i'm wondering if java can provide me with this information or i need to store them somewhere and retrieve them.
You will need to store them somewhere extra.
Or you can create your custom map implementation which would either extend Java's hashmap or use an hashmap internally and would additionally remember the last date of any action performed on your dataset, you seek to remember.
This would have the advantage that it would be ubiquitous and you wouldn't need to care about it within your algorithm, which uses the hashmap
Related
I have a situation where I have tons of hashmaps (millions and millions) in my server, and 95% of them have literally only one key, ever.
The server runs out of memory; which probably may have something to do with the fact that initial HashMap default size is 16, so for every HashMap object I'm wasting a ton of memory.
Short of re-designing the server with a new data structure that flexibly stores the data as 1 element OR an entire hashmap (which I'd prefer avoiding), I'm trying to first optimize this by changing the initial size to 1:
Map<String, Type2> myMiniMap = new HashMap<>(1);
However, my concern is that due to default load factor of 0.75 in HashMaps, this would immediately get increased to 2 the moment I add the first key to the map (since 1*0.75 < 1, which is how I understand hash sizing logic in Java)
Assuming my understanding above is correct (that by default, Java will create a space for 2 keys as soon as I add the first key to the hash), is there a way to prevent this from happening till I actually try to insert the second key?
E.g., should I set loadFactor to zero or one?
If they're truly only ever going to be singletons, why not use Collections.singletonMap to create them? The downside of this is that the map that is created is immutable.
Alternatively, you could create your own class implementing Map that will store a key and value in class fields and then, if an attempt is made to add a second key value, it will switch to using a HashMap as its default backing store. It would be more tedious than difficult to accomplish.
In a LinkedHashMap the entries are by default sorted by insertion order. The construction parameter "accessOrder" allows to change the sorting such that the last entry is the one that was ACCESSED last.
However, I do need a map where the sorting only changes when entries are added or overwritten via PUT (put, putAll, ...). Furthermore, the given map should directly provide a reverse iterator, i.e. without having to create a reversed array of the map's keySet to iterate over.
Does anyone know an existing Java class that provides what I'm searching for?
PS: It would be great if the map would allow concurrent modifications, but that is not essential as I can also synchronize the access manually.
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.
I have a HashMap that I am using to store information from my database. I have a Result Set that reads from each column of the database, and the Result Set is used to initialize a few string variables. These strings are then used in the HashMap. For example, one of my lines of code for my HashMap is parameters.put("CertificateCode", CertificateCode);. From what I'm beginning to understand, the first parameter of the HashMap allows the application to know WHERE to put the information, and the second parameter tells the HashMap WHAT information to put in.
Here is my problem... I noticed that if I try to run a loop through these lines of code, it will simply replace an existing value in the HashMap with the new one, thus only displaying the last record from my database.
How would I go about making the HashMap allow for the storage of multiple database records? I need this to work in a way that would still allow me to pass the parameters in to my Jasper Report. This method is JasperPrint jasperPrint = JasperFillManager.fillReport(), and it requires the parameters of JasperReport, Map<String, Object>, and a Connection.
I thought it might work if I added an array list to the HashMap. However, the "fillReport" method will not accept this as a proper parameter.
Any help would be much appreciated! I have been fighting with this reporting library for way too long now.
Thank you.
HashMap wont allow duplicate keys, when you try to insert value for a key which is already present inside the map then it will overwrite the key value with the new one.
If you want a map with duplicate keys then you have to go for Guava's Multimap.
I am storing some data in a hash map. Now I want to modify the values associated with a key based on a user input and store them these way permanently.
To make myself more clear, I have a hashmap like this:
public static HashMap<String,Integer> mymap= new HashMap<String,Integer>();
mymap.put("Hi",2);
mymap.put("Hello",3);
I will take feedback from user in some user and if he wants then I will, say, store 4 against Hello. I want these changes to be saved for future references.
I have heard about Reflection API in Java, but am not sure whether that will serve the purpose.
Reflection API allows one to manipulate/access data that is not accessable otherwise - or some data on the class that is unknown at compile time.
In here, it is really not needed. All you need is to put() the element into the map, it will "remove" the old value from the key you just inserted (if it is already there) and associate it (the key) with the newly added value.
So, basically - all you need to do is myMap.put(key,newValue), and the implementation of the Map (assuming it is a correct one, of course) will take care of the rest.
If you want to store the data between runs of the program - you will have to save it (the map) on disk. In order to do so, you can use serialization, or if you can use Properties in some cases.
Make sure that you load the map from disk once the program starts, or you will not see the values you stored.
Just say, mymap.put(key,value);. It will update the value for matching key. If not there, it will insert a new entry e.g.
mymap.put("Hello",4);
If you don't want to insert new value for a new key e.g. World, you can put a check like this:
if(mymap.containsKey(key)){
mymap.put(key,value);
}else{
//no existing key found
}
The Preferences API makes it easy to store a small amount of data on disk. It's usually used to store configuration data. It's similar to the Windows registry.
Here's an introduction: http://docs.oracle.com/javase/1.4.2/docs/guide/lang/preferences.html