• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mapping Question  RSS feed

 
john merritt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Teaching myself Java, very slowly due to a chronic fatigue problem, after retiring with disability (Crohn’s Disease), so would greatly appreciate some help.

I am attempting to write a spelling tutor application to improve my spelling, as I have a slight dyslexia problem.
I need to map the words in the spelling list to the byte array that holds that audio data for that word.
As I am still learning I have no experience of using any of the java collections. Can I just create a hash map <String, byte []> and serialize it?
After a Google search if I understand it correctly, it seems that I may have an encoding problem.
I have found a suggestion that it would be better to convert the byte array to a string before putting it into the hash map.
Is this the best way to map a string that holds a single word to the audio data for that word, or should I be looking at other alternatives, e.g.
Saving the audio data for each word to a file and put the path to each file in hash map<String Path>.
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem with HashMap<String,byte[]> is that byte[] is not a object but rather an array of primitives.

Just to complicate your life you may want to consider Java Speech API http://java.sun.com/products/java-media/speech/forDevelopers/jsapifaq.html. It will read the string and pronounce it.
It is synthetic sounding but it will save you having to record each word.

Back to your original Java question.

I would be inclined to make a class, let call it Word, which contained the String and sound byte array.

I would think it would have methods like.

String getSpelling();
byte[] getSound(); or better yet void playSound();

Then use a HashSet<Word> to keep your dictionary.

There are lots of ways to do this. Just an idea.

Joe
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Joe's suggestion: it's probably a good idea to create a class to hold the data, like Word or Sound or something.

One thing to consider is that if you use a Map<String, byte[]>, all your byte[] arrays must stay in memory. I don't know how many words you want to digitize, but that may require a lot of memory. Maybe too much. But with a Word class, you can write the class more flexibly. It could store the file name that you get the sound data from. The byte[] array could be populated, or it could be null - and if it's null, you can use the file name to load the sound data if and when it's needed for a particular word, rather than storing it in memory all the time. You could choose to keep the byte[] array around for certain commonly-used words, while for other words you could choose to replace it with null after you're done using it, and let it be garbage collected.

You don't have to decide how to do all of this right now. My point is just that using an extra class here can give you more flexibility to customize the behavior later, if you need to.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, using a Map<String,byte[]> may be problematic for other reasons. Generics and arrays tend to not mix as well as we might like. I don't remember all the details - see Angelika Langer's Java Generics FAQ for lots of details about this and everything else related to generics. But basically, if you use types that mix arrays with generics, you can run into trouble in unexpected places. It's often a good idea to just avoid this entirely unless you're sure of what you're doing.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any problem with using non-generic arrays (e.g. arrays of primitives, String[], etc) as values in Maps. Arrays only give you problems when you try to use arrays of generic types, like a List<String>[] or something similar.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that may be correct. Joshua Bloch expresses the general principle as "arrays and generics don't mix well", but so far all the problems I see are with arrays of generic type, not with generic types that refer to arrays. So maybe it's fine.
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I wasn't clear.

A HashMap needs something derived from Object for Key and Value.

So HashMap<String,Byte[]> works but HashMap<String, byte[]> doesn't. Just as HashMap<Sting,Integer> works but HashMap<String, int> doesn't.

It's not the array that's the problem.

Joe
 
john merritt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to say thanks Guys for the help and the links
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Areeda wrote:Sorry, I wasn't clear.

A HashMap needs something derived from Object for Key and Value.

So HashMap<String,Byte[]> works but HashMap<String, byte[]> doesn't. Just as HashMap<Sting,Integer> works but HashMap<String, int> doesn't.

It's not the array that's the problem.

Joe

Nonsense. HashMap<String,int> won't work no, because int is a primitive, as is byte. However, byte[] and int[] are not primitives but objects, and therefore HashMap<String,byte[]> will work. Remember, any array is an object, no matter the component type.
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Nonsense. HashMap<String,int> won't work no, because int is a primitive, as is byte. However, byte[] and int[] are not primitives but objects, and therefore HashMap<String,byte[]> will work. Remember, any array is an object, no matter the component type.

I would have sworn I checked that before I posted but evidently I just tried the primitives.

You are correct, I was wrong.

Joe
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!