473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

put HashSets into one HashMap, why so difficult?

dlite922
1,584 Expert 1GB
I could have this done in php in three lines of code but in Java here's what I need to do:

English
Looping through list of words I call a function that returns a HashSet of movie titles that contain that particular word. I want to insert these titles into a HashMap with their value being a counter of how many times this title occured.

(if two words return the same title, that title's value (counter) would be 2)

Relative Code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i = 0; i < titleWords.length; i++ ) {
  3.             HashSet tempHash = (HashSet) hashtable.get( titleWords[i] );
  4.  
  5.             // put result into hashmap and increment counter
  6.             Iterator myIt = tempHash.iterator();
  7.             int counter;
  8.             while(myIt.hasNext()) {
  9.                 counter = 0;
  10.                 String title = (String) myIt.next();
  11.                 counter = (int) resultMap.get((String)myIt.next()); //*** ERROR: inconvertible types
  12.                 resultMap.put((String)myIt.next(), ++counter);
  13.             }
  14. }
  15.  
  16.  
  17.  
That's where it sits now, I tried a few different ways, and obviously they did not work.

Since I can't sort a HashMap, would something else work better? if so, don't bother answering my question with a HashMap, answer it with another list/array implementor that can store key/value pairs.

Thanks for any pointers,




Dan
May 19 '09 #1
3 5389
dlite922
1,584 Expert 1GB
I solved this problem for now:

Expand|Select|Wrap|Line Numbers
  1. counter = ((Number) resultMap.get(title)).intValue() + 1;
  2.  
But how am I going to sort a hashmap? find some class online that sorts by value? There's got to be a better way.




Dan
May 19 '09 #2
r035198x
13,262 8TB
Use generics. They will make your code cleaner because you won't need to have casts all over the place.
Read the API specs for TreeMap.
May 20 '09 #3
JosAH
11,448 Expert 8TB
@dlite922
TreeMaps are sorted maps and all maps can take another Map as a parameter for their constructor. It's a oneliner really.

btw, sorting a Map on their values doesn't make sense: values don't need to be unique and they are mainly a one way street data structure: key --> value.

kind regards,

Jos
May 20 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: dougjrs | last post by:
I have a HashMap that is storing form data that will later be inserted into a database. I have been able to create the HashMap just fine, but I wanted to be able to take my HashMap and just "dump"...
1
by: Christian Gollwitzer | last post by:
Hi, I'm trying to loop over the elements in a hashmap of the STL-implementation by SGI. The point is, that because the key/value pair is stored as std::pair in the COntainer, the code becomes...
4
by: David | last post by:
Hi, I have created the following HashMap class. class HashMap: public hash_map<string, string, HashString, HashStringCompare> { public: HashMap(): hash_map<string, string, HashString,...
2
by: xor | last post by:
I'm doing up a school project using java, and am a little new to it (I've worked with other languages for years though). I've seen code posted by the instructor using HashMap like this... ...
6
by: bumrag | last post by:
This is the car dealership object relating to the coursework, there is also a separate object named car that i think i need to link to. The problem is on the addcar() method. Any help would be...
4
by: panos100m | last post by:
Hi these are the conents of my hashmap printing out the entrySet. entrySet1: OrderDate=10/30/2007, entrySet2: Level_0={Item_0={ItemTotal= 3.99, ItemName=test® in, ShipDate=10/31/2007,...
15
by: lbrtchx | last post by:
Hi, ~ I have found myself in need of some code resembling a Hashmap ~ This is easily done in Java this way: ~ import java.util.*; // __ public class JMith00Test{
1
by: evelina | last post by:
Hello, I need help. I have the following hashmap: HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>(); I want to extract Dimesion from the...
0
yoda
by: yoda | last post by:
So has the title says I need to find a away to loop over a Character ArrayList and replace the characters with a values from a HasMap. This is a simple encryption but I can't figure it out. What...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.