hashmap

Using the keySet() method in HashMap

I have a method that goes through the possible states in a board and stores them in a HashMap void up(String str){ int a = str.indexOf("0"); if(a>2){ String s = str.substring(0,a-3)+"0"+str.substring(a-2,a)+str.charAt(a-3)+str.substring(a+1); add(s,map.get(str)+1); if(s.equals("123456780")) { System.out.println("The sol...

Understanding the workings of equals and hashCode in a HashMap

I have this test code: import java.util.*; class MapEQ { public static void main(String[] args) { Map<ToDos, String> m = new HashMap<ToDos, String>(); ToDos t1 = new ToDos("Monday"); ToDos t2 = new ToDos("Monday"); ToDos t3 = new ToDos("Tuesday"); m.put(t1, "doLaundry"); m.put(t2, "payBills"); m.put(t3, "cleanAt...

String as a key in HashMap

Hi, I had seen, only the String is used as a key in HashMap.Although the put() method takes Object as a parameter.What is the significant of it.If any other object can also used as a Key or not? Please provide the answers. ...

How can I remove an item from a Hashmap in Hibernate ?

Hello, I try to delete an item from a hash map with hibernate. Here is my config on the collection: @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @OneToMany(mappedBy = "game", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @Where(clause = "charactType='charact'") @MapKey(name = "shortcut") @Cascade(org.hibernate.annotations....

3-dimensions different types Map or List: List(ID=integer) of List(ID=integer) of int[]

I'm going to build a hotel table. But I'm having problems when trying to implement this in Java. My hotel has Level(int id) x Room(int id) x Field(String status, int counter) The same in php would look like: $level=1; $room=2; if(isset($hotel[$level][$room])) { print("Room: ".$level*100+$room); print(", Status: ".$hotel[$level...

Trouble using hash function.

Hi all, I am implementing google's dense hash map in my C++ code. I want to use MurmurHash2 (http://murmurhash.googlepages.com/) as a hash function. But here's the problem. I have tried a lot but cant seem to get the hash function to work. The example shows the use of a default hash function (hash< const char * >). dense_hash_map < ...

Trouble finding key on hash_multimap search

I've done something wrong in defining my class which is causing Microsoft's implementation of the hash_multimap to "miss." Here is my class: class TimeParameter { public: TimeParameter(int _year, int _julianDay, int _hour) : m_Year(_year), m_JulianDay(_julianDay), m_Hour(_hour){} int GetHour() const {re...

HashMap with ~100 million keys, still constant time?

Does anyone know the answer to this question? ...

Use a Java hash map even when there is no "mapping"?

I want to store some objects and then be able to retrieve them later as efficiently as possible. I will also remove some of them under certain conditions. It seems a hash map would be the right choice. But, from what I've seen, hash maps always associate a value with another? For example, "john" and "555-5555", his phone number. Now, m...

How to sort hashmap?

Hi All ! I have hashmap and its keys are like "folder/1.txt,folder/2.txt,folder/3.txt" and value has these text files data. Now i am stucked. I want to sort this list. But it does not let me do it :( Here is my hashmap data type: HashMap<String, ArrayList<String>> following function work good but it is for arraylist not for hashma...

Using ArrayList or HashMap

Hi I got a question on whether to use an ArrayList or HashMap. I am trying to build a Paint program. Each drawn object will be assigned a unique object ID. If I want a fast retrieval speed when I click on an object, should I be using an arraylist or hashmap? In general hashmap has O(1) while arraylist has O(n) retrieval speed. Howeve...

Sorted hash table (map, dictionary) data structure design

Here's a description of the data structure: It operates like a regular map with get, put, and remove methods, but has a sort method that can be called to sorts the map. However, the map remembers its sorted structure, so subsequent calls to sort can be much quicker (if the structure doesn't change too much between calls to sort). For e...

Hashmap.keySet(), foreach, and remove

I know that it's typically a big no-no to remove from a list using java's "foreach" and that one should use iterator.remove(). But is it safe to remove() if I'm looping over a HashMap's keySet()? Like this: for(String key : map.keySet()) { Node n = map.get(key).optimize(); if(n == null) { map.remove(key); } else { map.put(...

Java: problem with hashmap and keyset()

Here's what I'm doing (it's my "homework"): the assignment is to make a map of gerbils and then flip through it using keySet() and get(key); import java.util.*; class Gerbil { int gerbilNumber; Gerbil(int i) { gerbilNumber = i; } void hoop() { System.out.println("The gerbil can ju...

How to create an iterable wrapper for TreeMap and HashMap (Java)?

I have a class MyMap which wraps TreeMap. (Say it's a collection of dogs and that the keys are strings). public class MyMap { private TreeMap<String, Dog> map; ... } I would like to turn MyMap iterable with the for-each loop. I know how I would've done it if my class was a LinkedList wrapper: public class MyList implements Iterabl...

Java HashMap to Matrix

Hi all gurus! I am facing a problem. I used the following hashmap to store some values HashMap<String, Object> hm = new HashMap<String, Object>(); The object is another HashMap (yeah, you can say HashMap of HashMap). String is to store book name, and Object is again another hashmap that is to store author name and number of authors....

How do I get an array of keys from a hashmap that aren't type Object?

Iterator it = myHashMap.keySet().iterator(); while (it.hasNext()) { int next = it.next(); } That doesn't work because it.next() returns Object. My hashmap uses ints for keys. All of my methods accept ints to access the hashmap. How can I actually get an int value when looping through my keys so I can pass it to my other methods? ...

Does the Boost unordered_map only work to associate items with integers?

I had a HashMap<Node, Double> in Java which I'd use later on to retrieve the double associated with a node. I've tried to do boost::unordered_map<Node*, double> but I get a "error C2108: subscript is not of integral type" when I try to put something in it, like: map[some_node] = some_double; If I interpreted the error right,...

How would I iterate through a list of [[tokens]] and replace them with textbox input?

Here is the basic code i'm trying to make work: Field fields[] = SalesLetter.class.getDeclaredFields(); String fieldName; for (int j = 0, m = fields.length; j < m; j++) { fieldName = fields[j].getName(); //example fieldname [[headline]] templateHTML = templateHTML.replace(fieldName, Letter.fieldName()); } I believe I'm ...

Data structure for efficiently returning the top-K entries of a hash table (map, dictionary)

Here's a description: It operates like a regular map with get, put, and remove methods, but has a getTopKEntries(int k) method to get the top-K elements, sorted by the key: For my specific use case, I'm adding, removing, and adjusting a lot of values in the structure, but at any one time there's approximately 500-1000 elements; I want ...