hashmap

how to implement n:m relation in java?

Sorry for this newbie question, but I just never worked with java... My problem is I need to implement an n:m relation in java. The use case is a catalog. a product can be in multiple categories a category can hold multiple products the classic... My current solution is to have a mapping class that has to hashmaps. The key of the ...

Java: Find and replace words/lines in a file

I have a file (more specifically, a log4j configuration file) and I want to be able to read in the file and pick out certain lines in the code and replace them. For example, within the file there is a string of text that indicates the directory it is stored in, or the level of the logger. I want to be able to replace those string of text...

Java Iterate Over Collection

I have a practice project which I need help with. It's a simple MailServer class. Here's the code: import java.util.ArrayList; import java.util.List; import java.util.Iterator; import java.util.HashMap; import java.util.TreeMap; import java.util.Collection; import java.util.Map; public class MailServer { private HashMap<String, Arr...

HashMap - correct usage?

i am trying to process an MMS message with multiple attachments. For that I'm creating a HashMap as following (this is not the complete implementation but only the relevant part): HashMap<String, Integer> hashAttachments = new HashMap<String, Integer>(); int c = 0; if(atts != null) { for(Attachment a : atts){ ...

Are there any drawbacks with ConcurrentHashMap?

I need a HashMap that is accessible from multiple threads. There are two simple options, using a normal HashMap and synchronizing on it or using a ConcurrentHashMap. Since ConcurrentHashMap does not block on read operations it seems much better suited for my needs (almost exclusively reads, almost never updates). On the other hand, I ...

question, the best data structure and algorithm

I have a question about implementing a data structure (with algorithm) that has these features: There are too many places (like stores), and each place can store too many items, I want to store items in the places. The point is we need to have optimized insertion (or deletion) and optimized search feature. Search can be done based on p...

Convert HashMap.toString() back to HashMap in Java

Hi i am using HashMap in java. i put some key-value pair in the HashMap object and then i converted HashMap into an String using toString() method and pass to the another method that take an String and convert this String into HashMap object and retrieve the value with it's corresponding key. Is it is possible to get the HashMap Obje...

Java HashMap: How to get the first key's value?

Hello, I am trying to use a HashMap to map a unique string to a string ArrayList like this: HashMap<String, ArrayList<String>> Basically, I want to be able to access the keys by number, not by using the key's name. And I want to be able to access said key's value, to iterate over it. I'm imagining something like this: for(all keys in...

Converting a Map<Integer, Object> to Map<Integer, List<Object>>, without using for loops (java)

As the title says, I am currently implementing the below code (well about to), is their a better way - as this seems a bit nasty. Map<Integer, List<Objects>> allObjectsMap = newHashMap(); //guava for(int i=1:i<myVar:i++){ Map<Integer, Objects> eachObjectMap = getMyObjectMap(i); for(Map.Entry<Integer, Object> entry:eachObjec...

How can I iterate over a map of <String, POJO> ?

I've got a Map<String, Person> (actually I'm using a more complex POJO but simplifying it for the sake of my question) Person looks like : class Person { String name; Integer age; //accessors } How can I iterate through this map, printing out the key, then the person name, then the person age such as : System.out.println(St...

Basic question on passing references of HashMaps

Hi ! I have defined a HashMap which uses a double type key and another HashMap as value as shown HashMap<Double, HashMap<Double, String>> HM1 = new HashMap<Double, HashMap<Double, String>>(); Now for each entry of this HashMap I have a reference to a different HashMap; the name of which is derived from the key value of that entry in t...

__gnu_cxx hash map with keys of type std::pair<std::string, unsigned int>?

Hi experts, Since std::pair<std::string, unsigned int> is not defined for __gnu_cxx hash map, how do I create a __gnu_cxx hash map with keys of type std::pair<std::string, unsigned int> and values of type std::pair<int, CBTNODE>? (CBTNODE is typedef as typedef int CBTNODE) If it's possible, I would really want to substitute std::pair<...

Map with own Datatyp (Java)

Map<String,MyData> map = new HashMap<String,MyData>(); ... static class MyData { String theString; Bitmap theBitmap; int theInt; ... } How can I put data in this map??? map.put("xxx", new MyData()); // Does not work Thank you ;) ...

For HashMap, would implementing hashCode() for the value help if I only search by key?

I'm using Java 6. For simplicity, everything will be public. Suppose I have this simple class. public class A{ public String name; public String data; } I want to put my class A objects into a HashMap. I will use the field name as the key, and the whole object as the value. I will only search for an object in this map by na...

Open Addressing vs. Separate Chaining

Which hashmap collision handling scheme is better when the load factor is close to 1 to ensure minimum memory wastage? I personally think the answer is open addressing with linear probing, because it doesn't need any additional storage space in case of collisions. Is this correct? ...

Java HashMap put in a for loop in

I have a for loop, where i check if there is a certain key in a HashMap, if the key is not there, it should put a new association in the HashMap. The problem is that that it puts the association, but by the next iteration of the loop the association is gone! I don't get it! public void DrawBoard(ArrayList<Integer[]> BoardList, int Frame...