hashmap

JavaScript Hashmap Equivalent

As made clear in update 3 on this answer, this notation: var hash = {}; hash[X] does not actually hash the object X; it actually just converts X to a string (via .toString() if it's an object, or some other built-in conversions for various primitive types) and then looks that string up, without hashing it, in "hash". Object equality i...

HashMap intialization parameters (load / initialcapacity)

What values should I pass to create an efficient HashMap / HashMap based structures for N items? In an ArrayList, the efficient number is N (N already assumes future grow). What should be the parameters for a HashMap? ((int)(N * 0.75d), 0.75d)? more? less? What is the effect of changing the load factor? ...

Storing MATLAB structs in Java objects

I'm using Java HashMap in MATLAB h = java.util.HashMap; And while strings, arrays and matrices works seemlessly with it h.put(5, 'test'); h.put(7, magic(4)); Structs do not h=java.util.HashMap; st.val = 7; h.put(7, st); ??? No method 'put' with matching signature found for class 'java.util.HashMap'. What would be the easiest...

strange HashMap.put() behaviour

I am attempting to troubleshoot an intermittent failure that appears to be related to removing an object from a HashMap and then putting that same object back using a new key. My HashMap is created as follows: transactions = new HashMap<Short, TransactionBase>(); The code that does the re-assignment is as follows: transactions.remov...

How do I use a foreach loop in Java to loop through the values in a HashMap?

I am trying to compile the following code: private String dataToString(){ Map data = (HashMap<MyClass.Key, String>) getData(); String toString = ""; for( MyClass.Key key: data.keySet() ){ toString += key.toString() + ": " + data.get( key ); return toString; } I get an error in the for line that says: incompati...

[Java] What is the best way to check if an object is from another(say fixed) list of objects?

Currently, I create a HashMap with Object Id as key and 1 as value. And the method asks for Object/Id and checks if there is a matching key. Is that ok? Or, is(are) there better alternative(s)? ...

2-D (concurrent) HashMap: 2-property key type? hashmap of hashmaps? [update]

So I need a 2-dimensional ConcurrentHashMap. It has to be as blazing fast as possible, as I'm going to be adding to and updating its values extremely frequently. It's in a multithreaded application, hence the choice to use ConcurrentHashMap instead of just HashMap. Both the "x" and "y" indices are integers with a known range (0 throug...

Efficiently iterate through all MATCHING keys in a hashmap?

I have a HashMap with millions of entries. Need to retrieve all entries whose keys match a specific set of criteria (in this case, each key is an object with two integer properties; I need to retrieve all keys where each of these integers fall within a specified range). What is the fastest, most efficient way to iterate through all suc...

HashMap gets over written every time I use .put()

My application is related to the stock market. I have a feed that is consistently updating an object called Price. Price has a HashMap that stores the security code (String) and price (Double). Every time a new price comes in this object is updated. The application is supposed to scan the prices for large moves. I have a separate class ...

C++ stdext hashmap efficiency - reorganising (?)

Hello, I have faced a quite strange thing related to stdext hashmap. I have to work with a lot of objects and it is a priority to access the elements in a fast way. My program read the object values from a file and if it's a new element, then insert this value in a hashmap, if it is an already processed object, then changes the stored ...

ConcurrentModificationException and a HashMap

I am persisting objects using JPA. The Main object has an owning One-Many relationship with another object. The other object is stored in a HashMap. What sort of synchronization would fix this problem? It seems to happen at completely random times and is very unpredictable. Here is the exception I get: Exception in thread "pool-1-t...

How can I form an ordered list of values extracted from HashMap?

My problem is actually more nuanced than the question suggests, but wanted to keep the header brief. I have a HashMap<String, File> of File objects as values. The keys are String name fields which are part of the File instances. I need to iterate over the values in the HashMap and return them as a single String. This is what I have cu...

Faster CompareText implementation for D2009

Hello, I'm extensively using hash map data structures in my program. I'm using a hash map implementation by Barry Kelly posted on the Codegear forums. That implementation internally uses RTL's CompareText function. Profiling made me realize that A LOT of time is spent in SysUtils CompareText function. I had a look at the Fastcode site...

About Hibernate save using Dynamic-Map entity mode

In the following example, how can I save the value of role to the role with id=1 without loading it? I have tried: Map user = new HashMap(); user.put("address","Address test"); user.put("role",1); session.save("User",user); But that results in: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Map at o...

Best way to create an empty map in java

I need to create an empty map if the map its null. if (fileParameters == null) fileParameters = (HashMap<String, String>) Collections.EMPTY_MAP; The problem is that I get a code warning: Type safety: Unchecked cast from Map to HashMap what its the best way to create this emptyMap. ...

Best data structure in C for these two situations?

I kinda need to decide on this to see if I can achieve it in a couple of hours before the deadline for my school project is due but I don't understand much about data structures and I need suggestions... There's 2 things I need to do, they will probably use different data structures. I need a data structure to hold profile records. Th...

Best way to store Country codes, names, and Continent in Java

I want to have a List or Array of some sort, storing this information about each country: 2 letter code Country name such as Brazil Continent/region of the world such as Eastern Europe, North America, etc. I will classify each country into the region/continent manually (but if there exists a way to do this automatically, do let me k...

How to get rid of g++ hash_map deprecation warning?

When I compile a c++ application I'm writing that makes use of hash_map, I get this warning on g++ 4.3.2: You are using the deprecated header . To eliminate this warning, use an ANSI-standard header file or use hte -Wno-deprecated compiler flag. 9> #include <ext/hash_map> What include replaces this? I've searched for a while on goog...

NullPointerException while using put method of HashMap

The following code is giving me a NullPointerException. The problem is on the following line: ... dataMap.put(nextLine[0], nextLine[6]); What is strange is that I have run this code without the above line and the call to nextLine[0] and nextLine[6] work exactly as expected - that is they give me back elements of a csv file. I declar...

the get() function for java hashmaps

Hi, I've declared the following hashmap: HashMap<Integer, Hive> hives Where Hive is an object. If I call "hives.get(2)" will it return a copy of the object Hive at that location or a reference to it? My goal is to modify the Hive object at that location. If it returns the reference, I can just modify the returned hive and be don...